Compute per-group slopes from a pooled interaction regression (y ~ x + g + x:g)

Quick Overview

Implement a pandas function that fits one pooled linear regression with an interaction between a numeric predictor and a two-level group, then returns each group's slope. It tests treatment coding, how interaction coefficients combine into group slopes, and careful handling of fitted parameter names.

Compute per-group slopes from a pooled interaction regression (y ~ x + g + x:g)

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def group_slopes(df: pd.DataFrame) -> pd.Series`. The DataFrame `df` has columns `x` (float), `g` (object, with levels `'ctrl'` and `'trt'`) and `y` (float). Fit the pooled model `y ~ x + g + x:g` with an intercept and `'ctrl'` as the reference level, and return each group's slope on `x`. - Fit one pooled interaction model, not two per-group regressions. - The `'ctrl'` slope is the main-effect coefficient on `x`. - The `'trt'` slope is the main effect plus the `x:g` interaction coefficient. - Return a `pd.Series` with index exactly `['ctrl', 'trt']` in that order, `float64` values, and name `'slope'`. For example, the control rows below lie on the line $y = 1 + 2x$ and the treatment rows lie on $y = 0.5 + 3.5x$, so the slopes are 2.0 and 3.5: ```python df = pd.DataFrame({ "x": [0.0, 1.0, 2.0, 0.0, 1.0, 2.0], "g": ["ctrl", "ctrl", "ctrl", "trt", "trt", "trt"], "y": [1.0, 3.0, 5.0, 0.5, 4.0, 7.5], }) group_slopes(df) # ctrl 2.0 # trt 3.5 # Name: slope, dtype: float64 ``` ```hint Write out each group's line Replace `g` with a 0/1 indicator for `'trt'`, then write the fitted equation once with the indicator at 0 and once at 1. Collect the terms that multiply `x` in each case. ``` ```hint Check the parameter names Before indexing into the fitted parameters, check which level your formula library uses as the reference by default and how it names the interaction column. ``` ### Constraints & Assumptions - `g` takes only the values `'ctrl'` and `'trt'`. - Each group has at least two distinct `x` values, so both slopes are identified. - You may use pandas, NumPy and statsmodels. ### What a Strong Answer Covers - A single OLS fit with an intercept, `x`, the group indicator and the interaction, with `'ctrl'` fixed as the reference level. - Which coefficients combine to give each group's slope, and why. - A returned Series with the exact index order, dtype and name the task asks for. - Edge cases such as a missing group, a group with one distinct `x` value, unexpected labels, and missing values. - How the pooled slopes relate to the slopes from separate per-group fits. ### Follow-up Questions - How would the coefficient meaning change if you dropped the intercept? - How would you test whether the two slopes differ significantly? - How would you get a standard error and confidence interval for the `'trt'` slope itself?

Overview: Implement a pandas function that fits one pooled linear regression with an interaction between a numeric predictor and a two-level group, then returns each group's slope. It tests treatment coding, how interaction coefficients combine into group slopes, and careful handling of fitted parameter names.

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

Implement def group_slopes(df: pd.DataFrame) -> pd.Series. The DataFrame df has columns x (float), g (object, with levels 'ctrl' and 'trt') and y (float). Fit the pooled model y ~ x + g + x:g with an intercept and 'ctrl' as the reference level, and return each group's slope on x.

  • Fit one pooled interaction model, not two per-group regressions.
  • The 'ctrl' slope is the main-effect coefficient on x .
  • The 'trt' slope is the main effect plus the x:g interaction coefficient.
  • Return a pd.Series with index exactly ['ctrl', 'trt'] in that order, float64 values, and name 'slope' .

For example, the control rows below lie on the line y=1+2xy = 1 + 2x and the treatment rows lie on y=0.5+3.5xy = 0.5 + 3.5x, so the slopes are 2.0 and 3.5:

df = pd.DataFrame({
    "x": [0.0, 1.0, 2.0, 0.0, 1.0, 2.0],
    "g": ["ctrl", "ctrl", "ctrl", "trt", "trt", "trt"],
    "y": [1.0, 3.0, 5.0, 0.5, 4.0, 7.5],
})
group_slopes(df)
# ctrl    2.0
# trt     3.5
# Name: slope, dtype: float64

Constraints & Assumptions

  • g takes only the values 'ctrl' and 'trt' .
  • Each group has at least two distinct x values, so both slopes are identified.
  • You may use pandas, NumPy and statsmodels.

What a Strong Answer Covers Guidance

  • A single OLS fit with an intercept, x , the group indicator and the interaction, with 'ctrl' fixed as the reference level.
  • Which coefficients combine to give each group's slope, and why.
  • A returned Series with the exact index order, dtype and name the task asks for.
  • Edge cases such as a missing group, a group with one distinct x value, unexpected labels, and missing values.
  • How the pooled slopes relate to the slopes from separate per-group fits.

Follow-up Questions Guidance

  • How would the coefficient meaning change if you dropped the intercept?
  • How would you test whether the two slopes differ significantly?
  • How would you get a standard error and confidence interval for the 'trt' slope itself?