# Sum to Zero Constraint GAM Factor Interaction

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:

set.seed(1234);
dat <- gamSim(4);

mod1<-gam(y ~ s(x1)+s(x2), data=dat)
p<-predict(mod1,dat,type="terms")
sum(p[,1]) # -8.729996e-16
sum(p[,2]) # -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)
p<-predict(mod2,dat,type="terms")

• 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. Aug 25, 2014 at 4:02
• 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?). Aug 25, 2014 at 19:49