Search code examples
rpredictiongam

Scenario development with GAM models


I'm working with a mgcv::gam model in R to generate predictions in which the relationship between time (year) and the outcome variable (out) varies. For example, in one scenario, I'd like to force time to affect the outcome variable in a linear manner, in another a marginally decreasing manner, and in another, I'd like to specify specific slopes of the time-outcome interaction. I'm unsure how to force the prediction to treat the interaction between time and the outcome variable in a specific manner:

res <- gam(out ~ s(time) + s(GEOID, bs='re'), data = df, method = "REML")
pred <- predict(gam, newdata = ndf, type="response", se=T)

Solution

  • There isn't an interaction betweentime and out; here time has a potentially non-linear effect on out.

    Are we talking about trying to force certain shapes for the function of time? If so, you will need to estimate different models; use time if you want a linear effect:

    res_lin <- gam(out ~ time + s(GEOID, bs='re'), data = df, method = "REML")
    

    and look at shape constrained p splines to enforce montonicity or concave/convex relationships.

    The scam package has these sorts of constraints and uses mgcv with GCV smoothness selection to fit the shape constrained models.

    As for specifying a specific slope for the linear effect of time, I think you'll need to include time as an offset in the model. So say the slope you want is 0.5 I think you need to do + offset(I(0.5*time)) because an offset has by definition a coefficient of 1. I would double check this though as I might have messed up my thinking here.