I do not understand the relationship between knots and basis functions in Generalized Additive Models (hereafter GAM).
In Chapter 4 of S. Wood's book "Generalized Additive Models - An Introduction with R", Wood uses polynomials to explain basis functions. If I understand correctly, polynomial models qualify as GAM, but they tend to perform poorly due to their excessive "wiggliness". To obviate excessive "wiggliness", tent functions can be used, which are only allowed to assume non-zero values nearby the knots. A GAM based on tent basis functions is a piecewise linear regression.
One way to achieve a GAM that is not too "stiff" (like a piecewise linear regression) and not too "wiggly" (like a polynomial) is to use cubic splines as basis functions.
Am I correct in saying that, in such scenario, the n-th basis function is a cubic spline defined so that it can assume non-zero values in proximity of the n-th knot, while it should tend to zero everywhere else, similarly to how a tent function behaves?
If the statement above is true, how comes that in the following example, a GAM fitted using five knots is based on only four basis functions, and some of the basis functions assume values that are far from zero in regions that are not near any knots?
# Example in R
# generate data with a sinusoid dependence from the explanatory variable x:
set.seed(1)
x <- seq(0, pi * 2, 0.1)
sin_x <- 3 + sin(x)
y <- sin_x + rnorm(n = length(x), mean = 0, sd = sd(sin_x / 2))
Sample_data <- data.frame(y,x)
# fit GAM
gam_y <- mgcv::gam(y ~ s(x, bs="cr", k=5),
knots = list(x = c(1:5)),
method = "REML")
# diagnostics look good:
# par(mfrow = c(2,2)); mgcv::gam.check(gam_y)
# plot data
plot(y~x, ylim=c(-2,5))
# plot GAM predictions as a
gam_pred <- predict(gam_y, newdata = data.frame(x = Sample_data[,2]))
lines(x, gam_pred, col="red", lwd=2)
# display basis functions
model_matrix <- predict(gam_y, type = "lpmatrix")
matplot(x, model_matrix[,-1], type = "l", lty = 2, add = T)
# mark the x coordinates of the knots:
abline(v=c(1:5), col="grey", lty="dotted")