6

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.)

generic_user
  • 3,430
  • 3
  • 32
  • 56
  • 3
    What about `te(x1, x3, by = interaction(x2, as.factor(fac2))` – mnel Apr 19 '13 at 00:18
  • @mnel, no -- the `interaction()` operator makes thousands of factor variables. In mgcv, `by` on a continuous variable multiplies the whole model matrix by a continuous variable, or makes different smooths for a factor. – generic_user Apr 19 '13 at 00:30
  • I'll delete my comment. It was not intended to offend. – IRTFM Apr 19 '13 at 01:07

0 Answers0