2.4 Nonlinear Interaction Effects

The limits of coefficients

use "https://tdmize.github.io/data/data/cda_gss", clear
(cda_gss.dta |  GSS 1972-2021 CDA - Categorical Data Analysis | date created 202
> 3)
regress occprest c.age##c.age##i.woman i.college
      Source |       SS           df       MS      Number of obs   =    58,304
-------------+----------------------------------   F(6, 58297)     =   3180.18
       Model |  2807908.65         6  467984.775   Prob > F        =    0.0000
    Residual |  8578800.25    58,297  147.156805   R-squared       =    0.2466
-------------+----------------------------------   Adj R-squared   =    0.2465
       Total |  11386708.9    58,303  195.302281   Root MSE        =    12.131

-------------------------------------------------------------------------------
     occprest | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
--------------+----------------------------------------------------------------
          age |      0.328      0.025   13.187   0.000        0.279       0.377
              |
  c.age#c.age |     -0.003      0.000  -10.458   0.000       -0.003      -0.002
              |
        woman |
       Women  |      3.116      0.774    4.027   0.000        1.600       4.633
              |
  woman#c.age |
       Women  |     -0.095      0.033   -2.831   0.005       -0.160      -0.029
              |
  woman#c.age#|
        c.age |
       Women  |      0.000      0.000    1.359   0.174       -0.000       0.001
              |
      college |
College De..  |     16.216      0.121  134.015   0.000       15.979      16.453
        _cons |     29.749      0.568   52.352   0.000       28.635      30.862
-------------------------------------------------------------------------------
est store opintnlmod
est table opintnlmod, ///
    title("[2.4.a] Occupational prestige linear regression model" ///
          "Nonlinear interaction effects") ///
    b(%5.3f) star(.05 .01 .001)
[2.4.a] Occupational prestige linear regression model Nonlinear interaction
effects

---------------------------
    Variable | opintnlmod  
-------------+-------------
         age |   0.328***  
             |
 c.age#c.age |  -0.003***  
             |
       woman |
      Women  |   3.116***  
             |
 woman#c.age |
      Women  |  -0.095**   
             |
 woman#c.age#|
       c.age |
      Women  |   0.000     
             |
     college |
College D..  |  16.216***  
       _cons |  29.749***  
---------------------------
Legend: * p<.05; ** p<.01; *** p<.001
library(haven)           # import Stata data and labels
library(dplyr)           # data manipulation
library(ggplot2)         # graphs
library(modelsummary)    # regression tables
library(marginaleffects) # predictions and marginal effects
library(cleanplots)      # graph scheme
cleanplots_defaults()

gss <- read_dta("https://tdmize.github.io/data/data/cda_gss.dta")

# Keep only model vars and drop any missing data on them
gss <- gss[c("occprest", "age", "woman", "college")]
gss <- gss[complete.cases(gss), ]

# Treat binary variables as factors using their value labels
gss <- gss |> mutate(woman = as_factor(woman), college = as_factor(college))

int2mod <- lm(occprest ~ poly(age, 2, raw = TRUE) * woman + college, data = gss)

modelsummary(int2mod, stars = c("*" = .05, "**" = .01, "***" = .001),
  title = "[2.4.a] Occupational prestige linear regression model: Nonlinear interaction effects")
[2.4.a] Occupational prestige linear regression model: Nonlinear interaction effects
(1)
* p < 0.05, ** p < 0.01, *** p < 0.001
(Intercept) 29.749***
(0.568)
poly(age, 2, raw = TRUE)1 0.328***
(0.025)
poly(age, 2, raw = TRUE)2 -0.003***
(0.000)
womanWomen 3.116***
(0.774)
collegeCollege Degree 16.216***
(0.121)
poly(age, 2, raw = TRUE)1 × womanWomen -0.095**
(0.033)
poly(age, 2, raw = TRUE)2 × womanWomen 0.000
(0.000)
Num.Obs. 58304
R2 0.247
R2 Adj. 0.247
AIC 456492.9
BIC 456564.7
Log.Lik. -228238.463
F 3180.178
RMSE 12.13

I find the coefficients more or less useless in nonlinear interactive models.

Here, I would go straight to a plot of predictions (include CIs!). Based on what the plot shows, I would then use that to guide further investigation—specifically, what MEs are likely to be informative.

¯\_(ツ)_/¯

qui margins, at(age=(20(5)80) woman=(0 1))
marginsplot, ///
    title("[2.4.b] Predicted Occupational Prestige Based on Person's Age and G
ender", size(medium)) ///
    subtitle("Interactive and Nonlinear Effect") ///
    ytitle("Occupational Prestige") ///
    recastci(rline) ciopts(lpat(dash) col(gs14)) ylab(38(2)46)
Variables that uniquely identify margins: age woman

Predicted occupational prestige by age for men and women: two curved lines; men rise more steeply and peak higher, and the lines cross twice.

plot_predictions(int2mod, by = c("age", "woman"),
  newdata = datagrid(age = seq(20, 80, 5), woman = unique,
    grid_type = "counterfactual")) +
  scale_y_continuous(breaks = seq(38, 46, 2), limits = c(38, 46)) +
  labs(title = "[2.4.b] Predicted Occupational Prestige Based on Person's Age and Gender",
    subtitle = "Interactive and Nonlinear Effect",
    x = "Age", y = "Occupational Prestige")

Predicted occupational prestige by age for men and women: two curved lines; men rise more steeply and peak higher, and the lines cross twice.

Now, the effect of age depends both on the value of age considered and based on whether the prediction is for a man or a woman

Interpreting nonlinear interaction effects with MEs

  • It is best to provide examples and interpretations that best answer your research question
    • Let’s say we are writing an article on the transition into early adulthood, thus we are mainly interested in the effect of age for those in their twenties
      • Remember the effect will be different for men and women, so need to calculate an ME for men and an ME for women
mecompare age, start(age=20) covariates(woman=0) amount(10) models(opintnlmod)
Predicting: Linear prediction
Marginal effects (N_opintnlmod=58304)

                                 |  ME #   Estimate         SE      P>|z|
---------------------------------+---------------------------------------
age + 10 (centered)              |                                       
                      opintnlmod |     1      2.238      0.152      0.000
mecompare age, start(age=20) covariates(woman=1) amount(10) models(opintnlmod)
Predicting: Linear prediction
Marginal effects (N_opintnlmod=58304)

                                 |  ME #   Estimate         SE      P>|z|
---------------------------------+---------------------------------------
age + 10 (centered)              |                                       
                      opintnlmod |     1      1.471      0.139      0.000
# Effect of aging from 20 to 30, separately for men and women
avg_comparisons(int2mod, variables = list(age = c(20, 30)), by = "woman")

 woman Estimate Std. Error    z Pr(>|z|)     S 2.5 % 97.5 %
 Men       1.98      0.128 15.4   <0.001 176.2  1.73   2.23
 Women     1.26      0.118 10.6   <0.001  85.0  1.02   1.49

Term: age
Type: response
Comparison: 30 - 20

A 10-year increase in age for 20-year-old men leads to about 2 points higher job prestige on average. For women, the same increase in age leads to about 1.26 points higher job prestige.

OR

On average, 30-year-old men are predicted to hold jobs that are about 2 points higher in job prestige than those held by 20-year-old men. 30-year-old women’s job prestige is higher than 20-year-old women’s by 1.26 points.

*See Appendix A2.5 for more on how to calculate these MEs

Equality of marginal effects: second differences

  • Sometimes (depending on pattern of nonlinearities), can still be interesting / useful to examine if the effect, on average, differs across groups
    • Does aging, on average, have a different effect for men and women?
      • The test of the equality of MEs (sometimes called 1st differences) is called a 2nd difference
est restore opintnlmod
(results opintnlmod are active now)
mecompare age, by(woman) amount(sd)
Predicting: Linear prediction
Marginal effects (N_m1=58304)

                                 |  ME #   Estimate         SE      P>|z|
---------------------------------+---------------------------------------
age + SD (centered)              |                                       
                             Men |     1      1.503      0.080      0.000
                           Women |     2      0.591      0.074      0.000
metest 2 - 1
                                 |  estimate         se     pvalue 
---------------------------------+--------------------------------
       age_woman_1 - age_woman_0 |    -0.912      0.109      0.000 
avg_comparisons(int2mod, variables = list(age = "sd"), by = "woman")

 woman Estimate Std. Error     z Pr(>|z|)     S 2.5 % 97.5 %
 Men      1.503     0.0798 18.84   <0.001 260.6 1.347  1.660
 Women    0.591     0.0743  7.96   <0.001  49.0 0.446  0.737

Term: age
Type: response
Comparison: (x + sd/2) - (x - sd/2)
avg_comparisons(int2mod, variables = list(age = "sd"), by = "woman",
  hypothesis = ~ pairwise)

      Hypothesis Estimate Std. Error     z Pr(>|z|)    S 2.5 % 97.5 %
 (Women) - (Men)   -0.912      0.109 -8.37   <0.001 53.9 -1.13 -0.698

Type: response

On average, a standard deviation increase in age for men is associated with a 1.50 point increase in occupational prestige. For women, aging shows a smaller, but still significant increase in job prestige (ME+SD | woman = 1 = 0.59; 2nd difference p < .001).

A caution about overlapping CIs

  • An important (and often overlooked) part of interaction effects is the marginal effect of the secondary variable. E.g., The interpretations above focus on the effect of age—gender is a secondary consideration. However, what about the effect of gender? There is an interesting story there too
    • Importantly, when confidence intervals do not overlap this indicates that the group difference (i.e., men vs women in our plot) is significant. When the confidence interval does overlap this may or may not indicate the group difference is non-significant*: you must test for this directly. As Belia et al. (2005) gently put it:

Abstract of Belia et al. (2005): many researchers misunderstand how confidence intervals and standard error bars relate to statistical significance.

*NOTE: These general rules are only true for independent non-overlapping samples. It is even more complicated with clustered data (e.g., multilevel or longitudinal data)

Visualizing significant group differences

  • Fortunately, there are fairly straightforward ways to calculate and visualize significance information for group differences
  • The first method I recommend is to calculate and then plot the marginal effect of the binary/nominal IV across the range of the continuous IV (here, the ME of gender across the range of age)
    • Go back to the last graph. What is the ME of gender? It is simply the gap between the red and blue lines. That shows you the difference between men and women at each age
  • Compare the last graph to the one below
est restore opintnlmod
(results opintnlmod are active now)
qui margins, dydx(woman) at(age=(20(5)80))
marginsplot, ///
    recastci(rline) ciopts(lpat(dash) col(gs14)) yline(0) ylab(-2(1)2) ///
    title("[2.4.c] Marginal Effect of Gender across Range of Age", size(medlar
ge)) ///
    subtitle("Effects above zero indicate women have higher job prestige") ///
    ytitle("Effect of Gender: Women - Men")
Variables that uniquely identify margins: age

Marginal effect of gender (women minus men) across age with confidence intervals: positive at young ages, crossing zero around age 40, and negative at older ages.

plot_comparisons(int2mod, variables = "woman", by = "age",
  newdata = datagrid(age = seq(20, 80, 5), grid_type = "counterfactual")) +
  geom_hline(yintercept = 0) +
  scale_y_continuous(breaks = seq(-2, 2, 1)) +
  labs(title = "[2.4.c] Marginal Effect of Gender across Range of Age",
    subtitle = "Effects above zero indicate women have higher job prestige",
    x = "Age", y = "Effect of Gender: Women - Men")

Marginal effect of gender (women minus men) across age with confidence intervals: positive at young ages, crossing zero around age 40, and negative at older ages.

*This is a direct test of the effect of gender: whenever the confidence interval overlaps zero = no significant group difference (see interpretation below)

Example interpretation of the plot above

As Figure # shows, at younger ages, women have higher occupational prestige than men (from age 20 to about age 37). From ages 37 to 45, men and women have similar occupational prestige scores. A gender gap reappears after age 45; however, at these older ages, the gender gap reverses, with men having higher occupational prestige than do women.

To see at exactly what ages the group difference is or is not significant:

mtable, dydx(woman) at(age=(20(1)80)) stat(est se p)
Expression: Linear prediction, predict()

           |      age      d xb        se         p
 ----------+---------------------------------------
         1 |       20     1.401     0.265     0.000
         2 |       21     1.325     0.248     0.000
         3 |       22     1.250     0.232     0.000
         4 |       23     1.175     0.217     0.000
         5 |       24     1.102     0.202     0.000
         6 |       25     1.029     0.189     0.000
         7 |       26     0.957     0.177     0.000
         8 |       27     0.886     0.166     0.000
         9 |       28     0.816     0.157     0.000
        10 |       29     0.747     0.148     0.000
        11 |       30     0.679     0.141     0.000
        12 |       31     0.612     0.135     0.000
        13 |       32     0.545     0.131     0.000
        14 |       33     0.480     0.127     0.000
        15 |       34     0.415     0.125     0.001
        16 |       35     0.352     0.124     0.005
        17 |       36     0.289     0.123     0.019
        18 |       37     0.227     0.124     0.067
        19 |       38     0.166     0.125     0.183
        20 |       39     0.106     0.126     0.401
        21 |       40     0.047     0.128     0.714
        22 |       41    -0.011     0.130     0.930
        23 |       42    -0.069     0.132     0.602
        24 |       43    -0.125     0.134     0.350
        25 |       44    -0.181     0.136     0.184
        26 |       45    -0.235     0.138     0.088
        27 |       46    -0.289     0.140     0.039
        28 |       47    -0.342     0.141     0.016
        29 |       48    -0.394     0.143     0.006
        30 |       49    -0.445     0.144     0.002
        31 |       50    -0.495     0.145     0.001
        32 |       51    -0.545     0.146     0.000
        33 |       52    -0.593     0.146     0.000
        34 |       53    -0.640     0.146     0.000
        35 |       54    -0.687     0.146     0.000
        36 |       55    -0.733     0.146     0.000
        37 |       56    -0.777     0.146     0.000
        38 |       57    -0.821     0.146     0.000
        39 |       58    -0.864     0.146     0.000
        40 |       59    -0.906     0.146     0.000
        41 |       60    -0.947     0.146     0.000
        42 |       61    -0.988     0.147     0.000
        43 |       62    -1.027     0.147     0.000
        44 |       63    -1.065     0.149     0.000
        45 |       64    -1.103     0.150     0.000
        46 |       65    -1.139     0.153     0.000
        47 |       66    -1.175     0.156     0.000
        48 |       67    -1.210     0.160     0.000
        49 |       68    -1.244     0.165     0.000
        50 |       69    -1.277     0.171     0.000
        51 |       70    -1.309     0.178     0.000
        52 |       71    -1.340     0.186     0.000
        53 |       72    -1.371     0.195     0.000
        54 |       73    -1.400     0.205     0.000
        55 |       74    -1.429     0.216     0.000
        56 |       75    -1.456     0.228     0.000
        57 |       76    -1.483     0.241     0.000
        58 |       77    -1.509     0.255     0.000
        59 |       78    -1.534     0.270     0.000
        60 |       79    -1.558     0.286     0.000
        61 |       80    -1.581     0.303     0.000

Specified values where .n indicates no values specified with at()

           |  No at()
 ----------+---------
   Current |       .n
avg_comparisons(int2mod, variables = "woman", by = "age",
  newdata = datagrid(age = 20:80, grid_type = "counterfactual")) |>
  print(nrows = 61)

 age Estimate Std. Error       z Pr(>|z|)    S   2.5 %  97.5 %
  20   1.4014      0.265  5.2844  < 0.001 22.9  0.8816  1.9212
  21   1.3251      0.248  5.3422  < 0.001 23.4  0.8390  1.8113
  22   1.2497      0.232  5.3905  < 0.001 23.8  0.7953  1.7042
  23   1.1753      0.217  5.4258  < 0.001 24.0  0.7507  1.5998
  24   1.1017      0.202  5.4433  < 0.001 24.2  0.7050  1.4984
  25   1.0290      0.189  5.4376  < 0.001 24.1  0.6581  1.3999
  26   0.9572      0.177  5.4019  < 0.001 23.9  0.6099  1.3045
  27   0.8863      0.166  5.3288  < 0.001 23.3  0.5603  1.2123
  28   0.8164      0.157  5.2107  < 0.001 22.3  0.5093  1.1234
  29   0.7473      0.148  5.0403  < 0.001 21.0  0.4567  1.0379
  30   0.6791      0.141  4.8119  < 0.001 19.4  0.4025  0.9557
  31   0.6118      0.135  4.5226  < 0.001 17.3  0.3467  0.8770
  32   0.5454      0.131  4.1735  < 0.001 15.0  0.2893  0.8016
  33   0.4800      0.127  3.7700  < 0.001 12.6  0.2304  0.7295
  34   0.4154      0.125  3.3216  < 0.001 10.1  0.1703  0.6605
  35   0.3517      0.124  2.8406  0.00450  7.8  0.1090  0.5944
  36   0.2889      0.123  2.3403  0.01927  5.7  0.0470  0.5309
  37   0.2271      0.124  1.8335  0.06673  3.9 -0.0157  0.4698
  38   0.1661      0.125  1.3307  0.18328  2.4 -0.0785  0.4107
  39   0.1060      0.126  0.8399  0.40098  1.3 -0.1414  0.3535
  40   0.0469      0.128  0.3661  0.71426  0.5 -0.2040  0.2977
  41  -0.0114      0.130 -0.0877  0.93010  0.1 -0.2661  0.2433
  42  -0.0688      0.132 -0.5209  0.60245  0.7 -0.3275  0.1900
  43  -0.1252      0.134 -0.9339  0.35037  1.5 -0.3881  0.1376
  44  -0.1808      0.136 -1.3281  0.18414  2.4 -0.4476  0.0860
  45  -0.2355      0.138 -1.7056  0.08809  3.5 -0.5060  0.0351
  46  -0.2892      0.140 -2.0685  0.03859  4.7 -0.5633 -0.0152
  47  -0.3421      0.141 -2.4192  0.01556  6.0 -0.6192 -0.0649
  48  -0.3940      0.143 -2.7598  0.00578  7.4 -0.6739 -0.1142
  49  -0.4451      0.144 -3.0924  0.00199  9.0 -0.7272 -0.1630
  50  -0.4953      0.145 -3.4190  < 0.001 10.6 -0.7792 -0.2113
  51  -0.5445      0.146 -3.7410  < 0.001 12.4 -0.8298 -0.2592
  52  -0.5929      0.146 -4.0597  < 0.001 14.3 -0.8791 -0.3066
  53  -0.6403      0.146 -4.3760  < 0.001 16.3 -0.9271 -0.3535
  54  -0.6869      0.146 -4.6904  < 0.001 18.5 -0.9739 -0.3999
  55  -0.7325      0.146 -5.0028  < 0.001 20.8 -1.0195 -0.4455
  56  -0.7773      0.146 -5.3123  < 0.001 23.1 -1.0640 -0.4905
  57  -0.8211      0.146 -5.6177  < 0.001 25.6 -1.1076 -0.5346
  58  -0.8641      0.146 -5.9167  < 0.001 28.2 -1.1503 -0.5778
  59  -0.9061      0.146 -6.2060  < 0.001 30.8 -1.1923 -0.6200
  60  -0.9473      0.146 -6.4816  < 0.001 33.4 -1.2337 -0.6608
  61  -0.9875      0.147 -6.7385  < 0.001 35.9 -1.2747 -0.7003
  62  -1.0269      0.147 -6.9709  < 0.001 38.2 -1.3156 -0.7381
  63  -1.0653      0.149 -7.1729  < 0.001 40.3 -1.3564 -0.7742
  64  -1.1028      0.150 -7.3383  < 0.001 42.1 -1.3974 -0.8083
  65  -1.1395      0.153 -7.4617  < 0.001 43.4 -1.4388 -0.8402
  66  -1.1752      0.156 -7.5389  < 0.001 44.3 -1.4808 -0.8697
  67  -1.2101      0.160 -7.5674  < 0.001 44.6 -1.5235 -0.8967
  68  -1.2440      0.165 -7.5472  < 0.001 44.4 -1.5671 -0.9209
  69  -1.2770      0.171 -7.4800  < 0.001 43.6 -1.6117 -0.9424
  70  -1.3092      0.178 -7.3700  < 0.001 42.4 -1.6573 -0.9610
  71  -1.3404      0.186 -7.2226  < 0.001 40.8 -1.7042 -0.9767
  72  -1.3707      0.195 -7.0442  < 0.001 39.0 -1.7521 -0.9894
  73  -1.4002      0.205 -6.8417  < 0.001 36.9 -1.8013 -0.9991
  74  -1.4287      0.216 -6.6217  < 0.001 34.7 -1.8516 -1.0058
  75  -1.4563      0.228 -6.3902  < 0.001 32.5 -1.9030 -1.0097
  76  -1.4831      0.241 -6.1524  < 0.001 30.3 -1.9555 -1.0106
  77  -1.5089      0.255 -5.9126  < 0.001 28.1 -2.0091 -1.0087
  78  -1.5338      0.270 -5.6743  < 0.001 26.1 -2.0636 -1.0040
  79  -1.5578      0.286 -5.4402  < 0.001 24.2 -2.1191 -0.9966
  80  -1.5810      0.303 -5.2122  < 0.001 22.4 -2.1755 -0.9865

Term: woman
Type: response
Comparison: Women - Men

Presenting nonlinear interaction effects

  • I would always include the plot of predictions in an article for a nonlinear interaction effect (figure 2.4.b above)
    • These are familiar to readers and easily understood
  • The graph of MEs (2.4.c above) is harder to understand, especially out of context
    • I usually present these second in my article; when possible, alongside the graph of predictions (see below)

Figure 1 of Petts, Kaufman, and Mize (2023): predicted perceptions of a good parent by weeks of leave taken, for fathers and mothers.

Figure 3 of Petts, Kaufman, and Mize (2023): marginal effect of parental gender across weeks of leave taken.

*I like including both graphs in a single paper (often as separate panels) to help the reader follow what the second graph shows. Example from Petts, Kaufman, and Mize 2023

Example write-up of occupational prestige findings

est restore opintnlmod
(results opintnlmod are active now)
mecompare age woman, amount(sd) models(opintnlmod)
Predicting: Linear prediction
Marginal effects (N_opintnlmod=58304)

                                 |  ME #   Estimate         SE      P>|z|
---------------------------------+---------------------------------------
age + SD (centered)              |                                       
                      opintnlmod |     1      1.013      0.054      0.000
---------------------------------+---------------------------------------
woman                            |                                       
                 Women - Men     |                                       
                      opintnlmod |     2     -0.165      0.101      0.101
avg_comparisons(int2mod, variables = list(age = "sd", woman = "reference"))

  Term                Contrast Estimate Std. Error     z Pr(>|z|)     S  2.5 % 97.5 %
 age   (x + sd/2) - (x - sd/2)    1.009     0.0544 18.55   <0.001 252.8  0.902 1.1154
 woman Women - Men               -0.165     0.1010 -1.64    0.101   3.3 -0.363 0.0325

Type: response

overall average effects of age and gender

Overall, individuals tend to hold more prestigious jobs when they are older compared to when they were younger—but the relationship is nonlinear. On average, a standard deviation increase in age (about 17.2 years) is associated with a 1.01 increase in occupational prestige (p < 0.001). However, Figure # illustrates the nonlinearities: aging is associated with increased occupational prestige until the late 50s, but the effect then reverses, with aging associated with lower occupational prestige late in life. In addition to the nonlinear effect of age, there are important differences in the effect of age based on a person’s gender. Women have higher occupational prestige than men from age 20 to about 37, but the effect then reverses, with men having higher occupational prestige after the age of about 45. Overall, aging shows a stronger association for men than women (2nd diff p < .001).

Back to top