Dummy coefficient as a contrast: fit OLS on a two-level plan dummy and return its coefficient

Quick Overview

Fit an OLS regression of an outcome on one 0/1 plan dummy with an intercept and return the dummy's coefficient as a Python float. It tests dummy encoding with a reference level, the meaning of the intercept and slope, and the pitfalls of missing levels and the dummy variable trap.

Dummy coefficient as a contrast: fit OLS on a two-level plan dummy and return its coefficient

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def dummy_contrast(df: pd.DataFrame) -> float`. `df` has columns `y` (float) and `plan` (object, `'basic'` or `'premium'`). Fit OLS of `y` on a single `plan` dummy and return the dummy's coefficient (mean `y` for `'premium'` minus mean `y` for `'basic'`). Requirements: - Encode `'premium'` as `1` and `'basic'` as `0` (the reference level). - Fit OLS with an intercept. - Return only the dummy's coefficient (not the intercept) as a Python `float`. ```hint Two fitted values With an intercept and one 0/1 column, the model can predict only two distinct values. Ask which constant minimizes squared error within each group. ``` ### Constraints & Assumptions - `plan` takes only the values `'basic'` and `'premium'`. - `statsmodels` or `sklearn` may be used. ### What a Strong Answer Covers - A 0/1 dummy with `'basic'` as the reference, plus an intercept column in the design. - The dummy's coefficient returned as a Python `float`, with the intercept left out. - Why the coefficient equals the difference in group means, and what the intercept equals. - How the code behaves when a level is missing or a label is misspelled. ### Follow-up Questions - What would the dummy's coefficient equal if you fit the model without an intercept? - How does the t-statistic on the dummy relate to a two-sample t-test of the group means? - If `plan` gained a third level, how would you encode it, and what would each coefficient mean? - How does the interpretation change if you add a numeric covariate such as account tenure?

Overview: Fit an OLS regression of an outcome on one 0/1 plan dummy with an intercept and return the dummy's coefficient as a Python float. It tests dummy encoding with a reference level, the meaning of the intercept and slope, and the pitfalls of missing levels and the dummy variable trap.

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

Implement def dummy_contrast(df: pd.DataFrame) -> float. df has columns y (float) and plan (object, 'basic' or 'premium'). Fit OLS of y on a single plan dummy and return the dummy's coefficient (mean y for 'premium' minus mean y for 'basic').

Requirements:

  • Encode 'premium' as 1 and 'basic' as 0 (the reference level).
  • Fit OLS with an intercept.
  • Return only the dummy's coefficient (not the intercept) as a Python float .

Constraints & Assumptions

  • plan takes only the values 'basic' and 'premium' .
  • statsmodels or sklearn may be used.

What a Strong Answer Covers Guidance

  • A 0/1 dummy with 'basic' as the reference, plus an intercept column in the design.
  • The dummy's coefficient returned as a Python float , with the intercept left out.
  • Why the coefficient equals the difference in group means, and what the intercept equals.
  • How the code behaves when a level is missing or a label is misspelled.

Follow-up Questions Guidance

  • What would the dummy's coefficient equal if you fit the model without an intercept?
  • How does the t-statistic on the dummy relate to a two-sample t-test of the group means?
  • If plan gained a third level, how would you encode it, and what would each coefficient mean?
  • How does the interpretation change if you add a numeric covariate such as account tenure?