When fitting GAMs in mgcv package in R using smooth function, an identifiability constraint is typically imposed, such that smooth function should sum to zero (thus, addition of the main effect is also required). There is one specific point, which is not fully clear to me: is this summation to zero over total range of given continuous predictor or only over range or points specific to given factor level?

For example, if we wanted to model height as dependent on sex and age using model:

height~sex+s(age, by=sex)

and imagine that range of female ages is wider than males in our sample, then interpretation of the main effects would be only meaningful if summation to zero constraint considers whole range of continuous predictor (age, in our pet example).


1 Answer 1


A single basis for both smooths implied by your model definition is created from all observations of age. As the identifiability constraint is applied to the basis, and as there is only one basis here, the constraint applies over the entire range of the covariate.

In a practical sense, if there are k basis functions after application of identifiability constraints, then 2k columns are added to the model matrix $\mathbf{X}$. The first k of those columns will contain the values of the basis functions evaluated at the observed values of age or 0s, where a non-zero is used for observations for the reference level of your factor. This is reversed for the last k of those columns; where there was a 0 in the first k columns, we now have the values of the basis functions evaluated at the values of age for the observations for the other level of sex, and 0s elsewhere.

Hence, while we get two entirely separate splines (in this case) from the factor by setup, those smooths share a basis, the identifiability constraint is applied to the full range of age, and it is the 0s in the columns of the model matrix which end up determining which rows of the data are affected by a given smooth.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.