An example:
library(mgcv)
N=1000
x1 = seq(1:N)
x2 = log(x1)
x3 = sqrt(x1)
fac1 = ceiling(rnorm(N)*3)
fac2 = ceiling(runif(N)*3)
y = fac1*x2 + x1*x2 + x2 + x3*x2 + x2*(x1/x3)^(.8+fac2/10) + rnorm(N)*x2
mod = gam(y~
s(as.factor(fac1),bs="re",by=x2)
+ s(x2)
+ s(x1,by=x2)
+ s(x3,by=x2)
+ te(x1,x3,by=x2, by=as.factor(fac2))
)
The last tensor doesn't want to let me interact it twice. The first is a continuous variable that multiplies every term in the model matrix, and the second is a factor -- it makes a different surface for each factor level.
EDIT: the last term would be equivalent to te(x1*x2,x1*x3,by=as.factor(fac2))
. But if I invent a new variable x1x2 = x1*x2
, I lose the ability to call predict.gam
How do I program this? Do I need to appeal to SmoothCon
or some such? If so, an example of how to implement would be really helpful.
Thanks!
(PS: I am aware of the heteroskedastic nature of the model. Feature, not bug.)