I was under the impression that the smooths fit with mgcv were made identifiable through a sum to zero constraint - i.e. if one was to sum the smooth over the values of its covariates, it would equal zero. This question gives some background, with an excellent answer from @Gavin Simpson.

With single smooths, this seems to be the case:

dat <- gamSim(4); 

mod1<-gam(y ~ s(x1)+s(x2), data=dat) 
sum(p[,1]) #[1] -8.729996e-16
sum(p[,2]) #[1] -4.956868e-12

But when a smooth is interacted with a factor, this is no longer the case. Is this an error in my understanding, or is something else going on?

mod2<-gam(y ~ s(x2, by=fac), data=dat) 
sum(p[,1]) #[1] 1.923649
sum(p[,2]) #[1] 6.496321
sum(p[,3]) #[1] 45.36179
  • 1
    $\begingroup$ Will add a fuller answer tomorrow when i recall the details, but the missing info in your by smoother is that you need to add fac as a fixed effects term to the model as well as the by variable. I will guess that adding that will improve things. $\endgroup$ Aug 25, 2014 at 4:02
  • $\begingroup$ But wasn't the reason for adding the fac fixed effect because the smoothers without it (or with it for that matter) summed to zero? $\endgroup$
    – B_Miner
    Aug 25, 2014 at 18:49
  • $\begingroup$ Read section 4.3 in Simon's book (p169-170). It discusses the sum-to-zero constraint and explicitly references the model matrix, not the "terms" component. The constraint is $\mathbf{1^{T}\tilde{X}_j\tilde{\beta}_j} = 0$ and some re-parameterisation is done to absorb this constraint into the model matrix. So when looking at this you need to include the model matrix and the coefficient vector. But I am struggling to get the correct bits out of mod2 to illustrate this. You may want to email Simon again and post an Answer here if he replies (or ask him if he wants to do it himself?). $\endgroup$ Aug 25, 2014 at 19:49


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.