I'm trying to fit a model using the mgcv package and I need to include interactions between categorical terms. I know how to implement interactions between continuous and categorical variables as
s(x1, by=c1)
but I don't know how to change this for two categorical terms and I couldn't find anything.
You can add parametric terms to the formula just as you would in a linear model or a GLM.
gam(y ~ f1 + f2 + f1:f2 + s(x1), data = df, method = "REML")
or via the shorthand
gam(y ~ f1 * f2 + s(x1), data = df, method = "REML")
would be a model with main effects plus interactions of the two categorical variables f1
and f2
plus a smooth effect of continuous variable x1
.
Anything you can do with parametric terms in lm()
or glm()
can be used in the formula for gam()
, gamm()
, bam()
, and gamm4::gamm4()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.