Mgcv: how to determine the interaction between smoothness and coefficient?

In R, I would like to fit a gamma model with categorical variables. I thought I could do this as with (cat is a categorical variable).

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

But I can not 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 to add a categorical variable to only one of the splines?

+4
source share
1 answer

One of the comments more or less told you how to do this. Use the variable by:

s(x1, by = cat)

" " fs, x1. , , .

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

"".

, , - . , :

s(x1, by = cat) + cat
+4

Source: https://habr.com/ru/post/1674499/


All Articles