Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 28 ยท Linear Regression in Depth

Categorical Variables and Interactions

Add categories to regression with C(), choose the reference level, read dummy coefficients and model interactions between a category and a number, with runnable examples.

Regression needs numbers, but real data is full of categories: city, plan type, product color. The solution is dummy coding (also called one-hot with a dropped level). A category with k levels becomes k - 1 columns of 0 and 1, and one level, the reference, is left out and absorbed into the intercept.

In a formula, wrap the column in C(). For string and category columns Patsy does this automatically, but being explicit is good habit, especially for integer codes such as month numbers.

Python A categorical predictor

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output shows C(plan)[T.plus] and C(plan)[T.pro]. The T. stands for treatment coding. The reference level (basic, first alphabetically) has no row: the intercept describes it. Each coefficient is the difference from the reference, holding tenure fixed. So plus customers spend about 15 more than basic, and pro about 40 more.

Choosing the reference level

Alphabetical order is rarely the best baseline. Pick the level that makes the comparison natural, often the largest group or a control:

Python
"spend ~ C(plan, Treatment(reference='pro')) + tenure"

The fitted values do not change; only the way coefficients are expressed does. Other codings exist, such as Sum (each level compared with the grand mean), which is common in ANOVA designs (ANOVA and Multiple Testing).

Python Change the reference level

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Interactions: when effects depend on each other

Without an interaction the model assumes tenure adds the same amount for every plan: parallel lines. An interaction lets each plan have its own slope. Write tenure * C(plan) (or the longer tenure + C(plan) + tenure:C(plan)).

Python Different slopes per group

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

How to read the interaction output:

  • x is the slope for the reference group (A).
  • C(group)[T.B] is the difference in intercepts at x = 0.
  • x:C(group)[T.B] is the extra slope for group B. Group B's slope is x + x:C(group)[T.B].

A significant interaction term tells you the effect of x genuinely differs between groups.

Numeric by numeric interactions

Two numbers can interact too: y ~ x1 * x2. Here the effect of x1 changes linearly with x2. A famous trap: when an interaction is in the model, the "main effect" coefficient of x1 is its effect at x2 = 0. If zero is outside your data, that number is not meaningful. Center the variables first (subtract the mean) to make main effects interpretable.

Python Centering numeric interactions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The interaction coefficient is the same in both fits, but the centered main effects now describe the effect at average temperature and advertising, which is what readers want.

Compare models with and without an interaction using compare_f_test or AIC (see Reading the Summary Table). Add interactions because you have a reason, not because you can.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A categorical variable with 4 levels creates how many dummy columns (treatment coding)?

    One level is the reference and gets no column, so k levels give k - 1 columns.
  2. 2. What does the coefficient C(plan)[T.pro] mean?

    Dummy coefficients are contrasts against the reference level.
  3. 3. Which formula allows each group its own slope for x?

    The star adds the x by group interaction terms, which change the slope per group.
  4. 4. Why center variables before fitting a numeric interaction?

    With an interaction, each main effect is evaluated where the other variable equals zero; centering moves that point to the mean.

Summary

  • Wrap categories in C(); k levels become k - 1 dummy columns relative to a reference level.
  • Change the baseline with Treatment(reference=...); the fit itself does not change.
  • x * C(g) gives group-specific slopes; read the interaction as an extra slope.
  • Center numeric variables so main effects stay interpretable.

Next: Weighted and Robust Regression