1  Interpreting interaction in a regression model

Published

December 7, 2017

1.1 Interaction with two binary variables

Start 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 \tag{1.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 \tag{1.2}\]

The four cell means are:

male female
control \[ \beta_0 \tag{1.3}\] \[ \beta_0 + \beta_1 \tag{1.4}\]
treatment \[ \beta_0 + \beta_2 \tag{1.5}\] \[ \beta_0 + \beta_1 + \beta_2 + \beta_{12} \tag{1.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))) \tag{1.7}\]

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:

Code
webuse union3
reg ln_wage i.union##i.black, r
margins union#black
margins union#black, pwcompare

. webuse union3
(NLS Women 14-24 in 1968)

. reg ln_wage i.union##i.black, r

Linear regression                               Number of obs     =      1,244
                                                F(3, 1240)        =      34.76
                                                Prob > F          =     0.0000
                                                R-squared         =     0.0762
                                                Root MSE          =     .37699

------------------------------------------------------------------------------
             |               Robust
     ln_wage | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
     1.union |   .2045053   .0291682     7.01   0.000     .1472808    .2617298
     1.black |  -.1709034   .0308067    -5.55   0.000    -.2313425   -.1104644
             |
 union#black |
        1 1  |   .0386275   .0516609     0.75   0.455     -.062725      .13998
             |
       _cons |   1.657525   .0138278   119.87   0.000     1.630396    1.684653
------------------------------------------------------------------------------

. margins union#black

Adjusted predictions                                     Number of obs = 1,244
Model VCE: Robust

Expression: Linear prediction, predict()

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
 union#black |
        0 0  |   1.657525   .0138278   119.87   0.000     1.630396    1.684653
        0 1  |   1.486621    .027529    54.00   0.000     1.432613     1.54063
        1 0  |    1.86203   .0256822    72.50   0.000     1.811644    1.912415
        1 1  |   1.729754   .0325611    53.12   0.000     1.665873    1.793635
------------------------------------------------------------------------------

. margins union#black, pwcompare

Pairwise comparisons of adjusted predictions             Number of obs = 1,244
Model VCE: Robust

Expression: Linear prediction, predict()

-----------------------------------------------------------------
                |            Delta-method         Unadjusted
                |   Contrast   std. err.     [95% conf. interval]
----------------+------------------------------------------------
    union#black |
(0 1) vs (0 0)  |  -.1709034   .0308067     -.2313425   -.1104644
(1 0) vs (0 0)  |   .2045053   .0291682      .1472808    .2617298
(1 1) vs (0 0)  |   .0722294   .0353756      .0028268     .141632
(1 0) vs (0 1)  |   .3754087   .0376487      .3015466    .4492709
(1 1) vs (0 1)  |   .2431328   .0426388      .1594807     .326785
(1 1) vs (1 0)  |  -.1322759   .0414705     -.2136359   -.0509159
-----------------------------------------------------------------

. 

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.

1.2 Interaction with continuous variables

With continuous \(x_1\) and \(x_2\),

\[ E(y) = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \beta_{12} x_1 x_2 \tag{1.8}\]

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.

Code
sysuse auto
sum mpg
gen mpg_centered=mpg-r(mean)
sum mpg_centered
reg price i.foreign##c.mpg_centered
margins foreign, at(mpg_centered=(-3 (1) 3))
marginsplot
graph export "marginsplot-interaction.svg", as(svg) replace

. sysuse auto
(1978 automobile data)

. sum mpg

    Variable |        Obs        Mean    Std. dev.       Min        Max
-------------+---------------------------------------------------------
         mpg |         74     21.2973    5.785503         12         41

. gen mpg_centered=mpg-r(mean)

. sum mpg_centered

    Variable |        Obs        Mean    Std. dev.       Min        Max
-------------+---------------------------------------------------------
mpg_centered |         74   -4.03e-08    5.785503  -9.297297    19.7027

. reg price i.foreign##c.mpg_centered

      Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(3, 70)        =      9.48
       Model |   183435285         3  61145094.9   Prob > F        =    0.0000
    Residual |   451630112        70  6451858.74   R-squared       =    0.2888
-------------+----------------------------------   Adj R-squared   =    0.2584
       Total |   635065396        73  8699525.97   Root MSE        =    2540.1

------------------------------------------------------------------------------
       price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
     foreign |
    Foreign  |   1666.519    717.217     2.32   0.023     236.0751    3096.963
mpg_centered |  -329.2551   74.98545    -4.39   0.000    -478.8088   -179.7013
             |
     foreign#|
          c. |
mpg_centered |
    Foreign  |   78.88826   112.4812     0.70   0.485    -145.4485     303.225
             |
       _cons |   5588.295   369.0945    15.14   0.000     4852.159    6324.431
------------------------------------------------------------------------------

. margins foreign, at(mpg_centered=(-3 (1) 3))

Adjusted predictions                                        Number of obs = 74
Model VCE: OLS

Expression: Linear prediction, predict()
1._at: mpg_centered = -3
2._at: mpg_centered = -2
3._at: mpg_centered = -1
4._at: mpg_centered =  0
5._at: mpg_centered =  1
6._at: mpg_centered =  2
7._at: mpg_centered =  3

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
 _at#foreign |
 1#Domestic  |    6576.06    370.446    17.75   0.000     5837.229    7314.891
  1#Foreign  |   8005.915   766.8178    10.44   0.000     6476.545    9535.284
 2#Domestic  |   6246.805   354.4734    17.62   0.000      5539.83     6953.78
  2#Foreign  |   7755.548   709.9327    10.92   0.000     6339.632    9171.464
 3#Domestic  |    5917.55   354.0032    16.72   0.000     5211.513    6623.587
  3#Foreign  |   7505.181   658.8306    11.39   0.000     6191.185    8819.177
 4#Domestic  |   5588.295   369.0945    15.14   0.000     4852.159    6324.431
  4#Foreign  |   7254.814   614.9548    11.80   0.000     6028.325    8481.303
 5#Domestic  |    5259.04    397.981    13.21   0.000     4465.292    6052.788
  5#Foreign  |   7004.447   579.9479    12.08   0.000     5847.778    8161.117
 6#Domestic  |   4929.785   437.9413    11.26   0.000     4056.338    5803.231
  6#Foreign  |   6754.081   555.4891    12.16   0.000     5646.192    7861.969
 7#Domestic  |    4600.53    486.253     9.46   0.000     3630.729    5570.331
  7#Foreign  |   6503.714   543.0057    11.98   0.000     5420.723    7586.704
------------------------------------------------------------------------------

. marginsplot

Variables that uniquely identify margins: mpg_centered foreign

. graph export "marginsplot-interaction.svg", as(svg) replace
(file marginsplot-interaction.svg not found)
file marginsplot-interaction.svg saved as SVG format

. 

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.