It is unsuccessful to see the wrong answer ... Although tuning kis the right way, it is fx = TRUEdefinitely wrong: it will force the use of a pure regression spline without penalty.
node locations
For a fined regression spline, exact locations are not important if:
:
bs = 'cr' ;- B- (
bs = 'bs', bs = 'ps', bs = 'ad') .
:
library(mgcv)
set.seed(0); x <- sort(rnorm(400, 0, pi))
set.seed(1); e <- rnorm(400, 0, 0.4)
y0 <- sin(x) + 0.2 * x + cos(abs(x))
y <- y0 + e
cr_fit <- gam(y ~ s(x, bs = 'cr', k = 20))
cr_knots <- cr_fit$smooth[[1]]$xp
bs_fit <- gam(y ~ s(x, bs = 'bs', k = 20))
bs_knots <- bs_fit$smooth[[1]]$knots
par(mfrow = c(1,2))
plot(x, y, col= "grey", main = "natural cubic spline");
lines(x, cr_fit$linear.predictors, col = 2, lwd = 2)
abline(v = cr_knots, lty = 2)
plot(x, y, col= "grey", main = "B-spline");
lines(x, bs_fit$linear.predictors, col = 2, lwd = 2)
abline(v = bs_knots, lty = 2)

.
:
knots gam() (, s(), gam()). , cr:
xlim <- range(x)
myfit <- gam(y ~ s(x, bs = 'cr', k =20),
knots = list(x = seq(xlim[1], xlim[2], length = 20)))
, :
my_knots <- myfit$smooth[[1]]$xp
plot(x, y, col= "grey", main = "my knots");
lines(x, myfit$linear.predictors, col = 2, lwd = 2)
abline(v = my_knots, lty = 2)

, , . , , . , , , k s().