Fit an Additive OLS Formula With a Categorical Term in statsmodels

Quick Overview

Fit an additive linear regression with one numeric predictor and a three-level categorical factor using the statsmodels formula API, and return the fitted parameters. It tests how the formula adds the intercept, encodes the factor as treatment dummies, and orders and names the coefficients.

Fit an Additive OLS Formula With a Categorical Term in statsmodels

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def formula_params(df: pd.DataFrame) -> pd.Series`. The DataFrame `df` has columns `x` (float), `g` (object, with levels `'a'`, `'b'` and `'c'`) and `y` (float). Fit the formula `'y ~ x + C(g)'` with the statsmodels formula API and return the fitted `.params`. Requirements: - Fit `smf.ols('y ~ x + C(g)', data=df).fit()`, with `import statsmodels.formula.api as smf`. - Do not call `add_constant`. The formula adds the intercept. - Return `.params` unchanged: no renaming, reordering or sorting. - The index must be exactly `['Intercept', 'C(g)[T.b]', 'C(g)[T.c]', 'x']`, with `float64` values. Level `'a'` is the reference level. ```hint What the formula builds Before reaching for any helper, work out which columns the formula already puts in the design matrix, and in what order statsmodels lists them. ``` ### Constraints & Assumptions - `df` has the three columns `x`, `g` and `y` with the dtypes above. - All three levels of `g` appear in `df`. ### What a Strong Answer Covers - A single `smf.ols(...).fit()` call whose `.params` are returned untouched - Why the formula already contains the intercept and why `add_constant` would be wrong here - How `C(g)` becomes treatment dummies with `'a'` as the reference level, and why those columns come before `x` - What each of the four coefficients means ### Follow-up Questions - How would you add an interaction between `x` and `g`? - How do you change the reference level for `C(g)`, and what happens to the parameter names and to the fit? - How would you interpret `Intercept`, `C(g)[T.b]` and `x` in words?

Overview: Fit an additive linear regression with one numeric predictor and a three-level categorical factor using the statsmodels formula API, and return the fitted parameters. It tests how the formula adds the intercept, encodes the factor as treatment dummies, and orders and names the coefficients.

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

Implement def formula_params(df: pd.DataFrame) -> pd.Series. The DataFrame df has columns x (float), g (object, with levels 'a', 'b' and 'c') and y (float). Fit the formula 'y ~ x + C(g)' with the statsmodels formula API and return the fitted .params.

Requirements:

  • Fit smf.ols('y ~ x + C(g)', data=df).fit() , with import statsmodels.formula.api as smf .
  • Do not call add_constant . The formula adds the intercept.
  • Return .params unchanged: no renaming, reordering or sorting.
  • The index must be exactly ['Intercept', 'C(g)[T.b]', 'C(g)[T.c]', 'x'] , with float64 values. Level 'a' is the reference level.

Constraints & Assumptions

  • df has the three columns x , g and y with the dtypes above.
  • All three levels of g appear in df .

What a Strong Answer Covers Guidance

  • A single smf.ols(...).fit() call whose .params are returned untouched
  • Why the formula already contains the intercept and why add_constant would be wrong here
  • How C(g) becomes treatment dummies with 'a' as the reference level, and why those columns come before x
  • What each of the four coefficients means

Follow-up Questions Guidance

  • How would you add an interaction between x and g ?
  • How do you change the reference level for C(g) , and what happens to the parameter names and to the fit?
  • How would you interpret Intercept , C(g)[T.b] and x in words?