The model is saturated — three coefficients for three contrasts from the baseline \((x_1, x_2) = (0, 0)\), one for each of the other three cells. Often only the interaction term gets attention. When the two variables are treatment and group, the interaction is the difference in treatment effects between groups, and that may be all that matters. But in other designs all four cells are substantively interesting, and then \(\beta_1\) and \(\beta_2\) matter too.
Each \(\beta\) reads off the table: \(\beta_0\) is the baseline cell mean, \(\beta_1\) the female premium among controls, \(\beta_2\) the treatment effect among males. The interaction
is the difference in differences — the treatment effect for females minus the treatment effect for males. When the research question is “does the treatment effect differ by group?”, the interaction term is the answer. Replace female/male with before/after and the same expression is the standard DiD estimator.
But sometimes the interest is in all four cells. Union membership crossed with race, for example — we may want all six pairwise comparisons, not just the interaction. Stata’s margins command computes these directly:
The margins call reports the four cell means; pwcompare gives all six pairwise comparisons. In this example, all six pairwise contrasts are significant at 95% — yet the interaction term is insignificant (\(p = 0.455\)). The two facts are compatible. Each pairwise comparison asks whether two cells differ from each other. The interaction asks whether the difference between two of those differences is non-zero — a second-order quantity with its own, larger standard error. Here the union premium is clearly positive for both groups, but the gap between the two premiums (\(0.039\)) is not distinguishable from zero.
centering both variables — subtracting each mean before forming the product — is a linear transformation that leaves fitted values, \(R^2\), and everything about \(\beta_{12}\) unchanged. What it changes is \(\beta_1\) and \(\beta_2\).
First, it reduces collinearity. When \(x_1\) and \(x_2\) are positive, \(x_1 x_2\) is mechanically correlated with both, inflating the standard errors of \(\beta_1\) and \(\beta_2\). Centering breaks most of that correlation (all of it under normality), stabilizing the main-effect estimates without altering the interaction.
Second, it fixes the interpretation. In the uncentered model, \(\beta_1\) is the effect of \(x_1\) when \(x_2 = 0\), and zero may be outside the support of \(x_2\). After centering, \(\beta_1\) is the effect of \(x_1\) evaluated at the mean of \(x_2\) — a quantity that is always interpretable. When a dummy interacts with a continuous variable, only the continuous variable should be centered.
Stata’s margins command computes the conditional predictions directly. The data are the built-in auto file: 74 1978 model cars, with price in dollars, mpg in miles per gallon, and foreign marking the 22 imports. We centre mpg at its mean, regress price on foreign interacted with centred mpg, and then ask for predicted prices by origin across a range of mpg values.
Mean mpg is 21.3, so the centred variable runs from \(-9.3\) to \(19.7\) and has mean zero to seven decimals. That is what makes the coefficients readable. The constant of $5,588 is the predicted price of a domestic car at average fuel economy. In the uncentred model it would instead be the predicted price at mpg = 0, which is nine standard deviations below the smallest car in the data and has no meaning.
The foreign coefficient is 1666.5 with a standard error of 717.2 and \(p = 0.023\): at average mpg, imports cost about $1,667 more than domestic cars. The slope on centred mpg is \(-329.3\) with a standard error of 75.0, so among domestic cars each extra mile per gallon is worth about $329 less in price — efficient cars in 1978 were the cheap ones. The interaction is 78.9 with a standard error of 112.5 and \(p = 0.485\), so the mpg slope for imports (\(-329.3 + 78.9 = -250.4\)) is not distinguishable from the domestic slope.
The graph shows predicted price for foreign and domestic cars across the range of mpg. Both lines slope down, the import line sits above the domestic one, and because the interaction is small and insignificant the two are close to parallel. The visible gap between them is the $1,667 main effect, not evidence of a differing slope.
---title: "Interpreting interaction in a regression model"date: "2017-12-07"---## Interaction with two binary variablesStart with the simplest case: two binary variables $x_1$ and $x_2$ coded 0/1.$$ E(y) = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \beta_{12} x_1x_2 $$ {#eq-interaction-regression-1}The model is saturated — three coefficients for three contrasts from the baseline $(x_1, x_2) = (0, 0)$, one for each of the other three cells. Often only the interaction term gets attention. When the two variables are treatment and group, the interaction is the difference in treatment effects between groups, and that may be all that matters. But in other designs all four cells are substantively interesting, and then $\beta_1$ and $\beta_2$ matter too.Consider the model:$$ E(y) = \beta_0 + \beta_1 female + \beta_2 treatment + \beta_{12} female*treatment $$ {#eq-interaction-regression-2}The four cell means are:|| male | female || -------------|:-----------------------:|:----------------------------------------------:|| control | $$ \beta_0 $$ {#eq-interaction-regression-3} | $$ \beta_0 + \beta_1 $$ {#eq-interaction-regression-4} || treatment | $$ \beta_0 + \beta_2 $$ {#eq-interaction-regression-5} | $$ \beta_0 + \beta_1 + \beta_2 + \beta_{12}$$ {#eq-interaction-regression-6} |Each $\beta$ reads off the table: $\beta_0$ is the baseline cell mean, $\beta_1$ the female premium among controls, $\beta_2$ the treatment effect among males. The interaction$$ \beta_{12} = (E(Y|(1,1))-E(Y|(0,1)))-(E(Y|(1,0))-E(Y|(0,0))) $$ {#eq-interaction-regression-9}is the difference in differences — the treatment effect for females minus the treatment effect for males. When the research question is "does the treatment effect differ by group?", the interaction term is the answer. Replace female/male with before/after and the same expression is the standard DiD estimator.But sometimes the interest is in all four cells. Union membership crossed with race, for example — we may want all six pairwise comparisons, not just the interaction. Stata's `margins` command computes these directly:```{r}#| label: stata-chunk#| engine: 'stata'#| engine.path: '/usr/local/bin/stata'#| cache: truewebuse union3reg ln_wage i.union##i.black, rmargins union#blackmargins union#black, pwcompare```The `margins` call reports the four cell means; `pwcompare` gives all six pairwise comparisons. In this example, all six pairwise contrasts are significant at 95% — yet the interaction term is insignificant ($p = 0.455$). The two facts are compatible. Each pairwise comparison asks whether two cells differ from each other. The interaction asks whether the *difference between two of those differences* is non-zero — a second-order quantity with its own, larger standard error. Here the union premium is clearly positive for both groups, but the gap between the two premiums ($0.039$) is not distinguishable from zero.## Interaction with continuous variablesWith continuous $x_1$ and $x_2$,$$ E(y) = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \beta_{12} x_1 x_2 $$ {#eq-interaction-regression-10}centering both variables — subtracting each mean before forming the product — is a linear transformation that leaves fitted values, $R^2$, and everything about $\beta_{12}$ unchanged. What it changes is $\beta_1$ and $\beta_2$.First, it reduces collinearity. When $x_1$ and $x_2$ are positive, $x_1 x_2$ is mechanically correlated with both, inflating the standard errors of $\beta_1$ and $\beta_2$. Centering breaks most of that correlation (all of it under normality), stabilizing the main-effect estimates without altering the interaction.Second, it fixes the interpretation. In the uncentered model, $\beta_1$ is the effect of $x_1$ when $x_2 = 0$, and zero may be outside the support of $x_2$. After centering, $\beta_1$ is the effect of $x_1$ evaluated at the mean of $x_2$ — a quantity that is always interpretable. When a dummy interacts with a continuous variable, only the continuous variable should be centered.Stata's `margins` command computes the conditional predictions directly. Thedata are the built-in `auto` file: 74 1978 model cars, with `price` in dollars,`mpg` in miles per gallon, and `foreign` marking the 22 imports. We centre `mpg`at its mean, regress price on `foreign` interacted with centred mpg, and thenask for predicted prices by origin across a range of mpg values.```{r}#| label: stata-chunk2#| engine: 'stata'#| engine.path: '/usr/local/bin/stata'#| cache: truesysuse autosum mpggen mpg_centered=mpg-r(mean)sum mpg_centeredreg price i.foreign##c.mpg_centeredmargins foreign, at(mpg_centered=(-3 (1) 3))marginsplotgraph export "marginsplot-interaction.svg", as(svg) replace```Mean mpg is 21.3, so the centred variable runs from $-9.3$ to $19.7$ and hasmean zero to seven decimals. That is what makes the coefficients readable. Theconstant of \$5,588 is the predicted price of a domestic car at *average* fueleconomy. In the uncentred model it would instead be the predicted price at`mpg = 0`, which is nine standard deviations below the smallest car in the dataand has no meaning.The `foreign` coefficient is 1666.5 with a standard error of 717.2 and$p = 0.023$: at average mpg, imports cost about \$1,667 more than domesticcars. The slope on centred mpg is $-329.3$ with a standard error of 75.0, soamong domestic cars each extra mile per gallon is worth about \$329 less inprice --- efficient cars in 1978 were the cheap ones. The interaction is 78.9with a standard error of 112.5 and $p = 0.485$, so the mpg slope for imports($-329.3 + 78.9 = -250.4$) is not distinguishable from the domestic slope.The graph shows predicted price for foreign and domestic cars across the rangeof mpg. Both lines slope down, the import line sits above the domestic one, andbecause the interaction is small and insignificant the two are close toparallel. The visible gap between them is the \$1,667 main effect, not evidenceof a differing slope.