Please look at the following simple example of how to fit a GAM to a data set using one spline,


df <- data.frame(one   = 1*(1:20),
                 two   = c(1,6,2,8,7,4,9,8,5,4, c(1,6,2,8,7,4,3,8,5,4)/2))
m <- gam(two ~ s(one, k = 8, pc=5), data = dfOne)

ggplot(dfOne, aes(x = one, y = two)) + geom_point(colour="blue") + geom_line(colour="red",aes(y=fitted(m))) + theme_bw()

The data and fit has the following form

enter image description here

while the spline takes the form

enter image description here

My question is very basic: What is the relationship between the estimated spline s(one) and the model in this simple example? Obviously they have the same shape, but when the estimated spline has value -3.78 at one=15, how is that related to the model's predicted value 2.51 at one=15? Is there a function that linkes these to values together?


The only difference is in an intercept term. This is standard with smooth terms in models of this kind.

Taking your two plots and resizing the red one to be on the same scale as the other one, then shifting the y-axis to align the two:

superimposed version of the two plots

we can see that they are otherwise identical -- one is just a shift of the other (well, that and the fact that the red one is only evaluated at data points while the black one has been evaluated on a fine grid).

| cite | improve this answer | |
  • $\begingroup$ + 1 for spending time to reproduce OP's experiment ! $\endgroup$ – Haitao Du May 1 '17 at 2:14
  • $\begingroup$ @hxd I didn't (though in retrospect that may have been easier); I simply took the two images, resized the first to about the same scaling (~same pixels per unit of x and y for both images) and laid it over the second (with a transparent background so you could see what it was on top of). I didn't use R for that. $\endgroup$ – Glen_b May 1 '17 at 2:29

To add a little to @Glen_b's answer, the standard splines in mgcv are subject to constraints to enable their identification as they are confounded with the model intercept term.

The constraint mgcv uses is

$$\sum_i f_j(x_{ij}) = 0 ~~ \forall ~ j$$

This is the sum-to-zero constraint, where $f_j$ is a spline function and $x_{ij}$ is the $i$th observation of the $j$th variable.

This constraint results in the splines being centred around zero. It also results in better behaved confidence intervals on the estimated smooth functions than other identifiability constraints.

If you have a single smooth, you can use the shift argument to plot.gam() to add on the intercept to scale the y-axis in response units (assuming family = gaussian); for non-Gaussian models you'd also need to use the trans argument to post apply the inverse of the link functions once shift had been added.

| cite | improve this answer | |

Your Answer

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

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