mgcv: how to specify interaction between smooth and factor?

与世无争的帅哥 提交于 2019-12-18 17:03:29

问题


In R, I would like to fit a gam model with categorical variables. I thought I could do it like with (cat is the categorical variable).

lm(data = df, formula = y ~ x1*cat + x2 + x3);

But I can't do things like :

gam(data = df, formula = y ~ s(x1)*cat + s(x2) + x3)

but the following works:

gam(data = df, formula = y ~ cat + s(x1) + s(x2) + x3)

How do I add a categorical variable to just one of the splines?


回答1:


One of the comments has more or less told you how. Use by variable:

s(x1, by = cat)

This creates the "factor smooth" smoothing class fs, where a smooth function of x1 is created for each factor level. Smoothing parameters are also duplicated but not linked, so they are estimated indecently. You can set

s(x1, by = cat, id = 0)

to use a single smoothing parameter for all "sub smooths".

Also note that contrast does not apply to factor but smooth function is still subject to centering constraint. What this means is that you need to specify factor variable as a fixed effect, too:

s(x1, by = cat) + cat


来源:https://stackoverflow.com/questions/43330346/mgcv-how-to-specify-interaction-between-smooth-and-factor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!