Solved – Interactions between non-linear predictors

generalized-additive-modelinteractionmixed modelrsplines

I have data on 70,000 students, nested in 120 schools. I'm starting with fixed effects for the schools, but at some point I might start letting intercepts and slopes vary.

Some key predictors (e.g. gpa, test scores) have non-linear relationships with some outcomes. I definitely want to interact these predictors with categorical variables, and maybe with continuous ones as well. Natural cubic splines might be the right approach, but I haven't seen any examples with interactions or nested data, so I could use some pointers. These non-linear relationships aren't the sole focus of my research, so I don't want to focus on them to the neglect of other aspects of the model.

I'd appreciate suggestions of R packages, examples of related work, and general advice on how to approach this. Thanks!

Best Answer

You could try generalized additive mixed models, handily implemented in the gamm4 package. The way I've used them, you can do something like:

fit1 = gamm4(
    formula = V1 ~ V2 + s(V3)
    , random = ~ (1|V4)
)
fit2 = gamm4(
    formula = V1 ~ V2 + s(V3,by=V2)
    , random = ~ (1|V4)
)

fit1 seeks to predict V1 using V4 as a random effect and V2 and V3 as fixed effects, but where V3 is spline-smoothed. fit2 seeks the same, except with the addition of permitting the smooth of V3 to vary within levels of V2, thus implementing an interaction. Comparison of fit1 to fit2 evaluates the necessity of permitting the interaction.