# Fitting splines using mgcv

I have the following example, where I fit a spline to a simple data set using the mgcv-package in R and plot it afterwards:

dev.off()

dfOne <- data.frame(1*(1:20),
c(1,6,2,8,7,4,9,8,5,4, c(1,6,2,8,7,4,3,8,5,4)/2),
3*(1:20))
colnames(dfOne)<-c("one", "two", "three")

fml <- as.formula(three ~ s(one, by=two, k=8,pc=10) )
GAM1 <- mgcv::gam(formula = fml, data = dfOne)

require(ggplot2)
data_plot<-qplot(dfOne$one, dfOne$two,colour="blue") +
geom_line(colour="red",aes(y=fitted(GAM1))) +
theme_bw()
print(data_plot)


Here is a plot of the spline to dfOne$one, dfOne$three and the data points dfOne$one, dfOne$two:

1. Note how I fit the spline to dfOne$three using dfOne$two. In other words, I expect the spline to follow dfOne$three and the trend of dfOne$two. Indeed it follows the linear increase of dfOne$three. However, look at the horizontal axis between values 1-5. There the spline is anti-correlated with dfOne$two. Why is that?
2. As a test I used k=8 to only use 8 knots. But as far as I can count, there are 18 knots/bends in the spline. Why is that?
3. I also use pc=10 to force the spline to pass through zero at 10. But this doesn't work. Why is that?
• 1. I find your plot strange. Consider plotting ggplot(dfOne, aes(x = one, y = three)) + geom_point(colour="blue") + geom_line(colour="red",aes(y=fitted(GAM1))) + theme_bw() as well as ggplot(dfOne, aes(x = two, y = three)) + geom_point(colour="blue") + geom_line(colour="red",aes(y=fitted(GAM1))) + theme_bw(). Apr 28, 2017 at 14:32
• 2. You have not understood what a knot is. Reread the relevant chapter in your textbook. You can't see the knots in this plot. 3. plot(GAM1) might be elucidating. Apr 28, 2017 at 14:34

library("ggplot2")
theme_set(theme_bw())
library("mgcv")

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),
three = 3*(1:20))
m <- gam(three ~ s(one, by=two, k = 8, pc=10), data = dfOne)


### Q1

What your model is fitting is a centred smooth effect of one multiplied by two. The spline itself is quite smooth:

Next note that the estimates of the spline for the first n elements are all negative and quite similar in magnitude, esp the first few where you see the anticorrelated behaviour:

> head(fitted(m))
[1] 25.936853  4.209230 23.373273  5.188732 12.415398 22.525351
1          2          3          4          5          6
-5.118848 -26.846471  -7.682427 -25.866969 -18.640302  -8.530349


What you see here is the relatively similar contribution from the smooth being multiplied by the quite different value of two for these first two elements. As the spline effects are negative, the resulting contribution is somewhat negative for the first value and much more strongly negative for the second value, and so on.

This is all due to the model you specified; estimate a spline ,evaluate the spline at the ith location and multiply that effect by the ith row of the numeric by variable.

### Q2

Knots are not inflexions of the resulting fitted values. In fact, there are as many knots as data points as you used a thin plate spline here. There k represents the dimension of the basis expansion (in this the dimension is 7 because you loose 1 due to the centring constraints imposed on the spline), and mgcv employs a neat trick to select 7 basis functions from the n basis functions in a traditional thin plate spline basis. (The basis functions are subject to an eigen decomposition and the eigenvectors associated with k-1 largest eigenvalues are used as a new basis. This concentrates most of the information in the full thin plate spline basis into a smaller set of orthogonal basis functions for model fitting.)

The estimated coefficients clearly indicate that only 7 basis functions were used in the model:

> coef(m)
(Intercept) s(one):two.1 s(one):two.2 s(one):two.3 s(one):two.4 s(one):two.5
31.05570043   0.70385580  -0.65193244  -0.01680226  -0.21851339   0.14293203
s(one):two.6 s(one):two.7
-0.52163338   5.27573517


### Q3

As the plot of the spline shows, the spline does pass through x = 10 as you requested.

You are confusing the fitted values of the model with the estimated spline.