Fit a Multiple Linear Regression With the statsmodels Formula API

Quick Overview

Fit a multiple linear regression on three numeric predictors with the statsmodels formula API and return the fitted coefficients. It tests how the formula adds and names the intercept, how the parameter index is ordered, and how this differs from building the design matrix for sm.OLS by hand.

Fit a Multiple Linear Regression With the statsmodels Formula API

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `fit_formula_ols(df)` that fits a multiple linear regression with `statsmodels.formula.api.ols` using the formula `'y ~ a + b + c'`, and returns the fitted `params` exactly as statsmodels produces them. Requirements: - Fit with `ols('y ~ a + b + c', data=df).fit()`. - Return a `pd.Series` of dtype `float64` indexed `['Intercept', 'a', 'b', 'c']`, in that order. - Do not call `add_constant`. The formula API adds the intercept and names it `'Intercept'`, not `'const'`. - Use the plain additive `+` form only: no interactions, no `C()`, no transformations. ```hint Where the intercept comes from Compare what the formula API puts in the design matrix with what you would have to build yourself for `sm.OLS`, and check what name the constant column gets in each case. ``` ### Constraints & Assumptions - `df` has float columns `['a', 'b', 'c', 'y']` and 40 rows. ### What a Strong Answer Covers - A single `ols(...).fit()` call whose `.params` are returned unchanged - Why the formula already includes an intercept, and why it is named `Intercept` - The index order of the returned Series and its `float64` dtype - How the formula API differs from building the design matrix for `sm.OLS` by hand ### Follow-up Questions - How does the formula API build the design matrix compared to calling `sm.OLS` directly? - How would you add an interaction term between `a` and `b`? - What does the formula API do with rows that contain missing values?

Overview: Fit a multiple linear regression on three numeric predictors with the statsmodels formula API and return the fitted coefficients. It tests how the formula adds and names the intercept, how the parameter index is ordered, and how this differs from building the design matrix for sm.OLS by hand.

General logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement fit_formula_ols(df) that fits a multiple linear regression with statsmodels.formula.api.ols using the formula 'y ~ a + b + c', and returns the fitted params exactly as statsmodels produces them.

Requirements:

  • Fit with ols('y ~ a + b + c', data=df).fit() .
  • Return a pd.Series of dtype float64 indexed ['Intercept', 'a', 'b', 'c'] , in that order.
  • Do not call add_constant . The formula API adds the intercept and names it 'Intercept' , not 'const' .
  • Use the plain additive + form only: no interactions, no C() , no transformations.

Constraints & Assumptions

  • df has float columns ['a', 'b', 'c', 'y'] and 40 rows.

What a Strong Answer Covers Guidance

  • A single ols(...).fit() call whose .params are returned unchanged
  • Why the formula already includes an intercept, and why it is named Intercept
  • The index order of the returned Series and its float64 dtype
  • How the formula API differs from building the design matrix for sm.OLS by hand

Follow-up Questions Guidance

  • How does the formula API build the design matrix compared to calling sm.OLS directly?
  • How would you add an interaction term between a and b ?
  • What does the formula API do with rows that contain missing values?