Hi,
Can someone explain me three questions related to smooth.spline().
1°) Why the knots created within smooth.spline are no evenly spread?
2°) What type of spline is used by smooth.spline ?
When I compare a smooth.spline with no constraint (spar=0) and compare it with a linear model using a B-spline (bSpline function) I have very close curves but identical and the parameters differs.
3°) How can I retrieve the spline matrix from smooth.spline as the one I have from bSpline?
Here is the code I have used :
y18 <- c(1:3, 5, 4, 7:3, 2*(2:5), rep(10, 4))
data <- data.frame(y18, x=seq(0,1,length=18))
xx <- seq(0, 1, len = 201)
s2 <- smooth.spline(y=data$y18, x=data$x, nknots = 9, spar=0 )
p <- predict(s2, xx)
bsx <- data.frame(bSpline(x=data$x , knots=1:7/8, degree = 3)) #--- 7 internes + 2 externes => 9 noeuds
bsx <- data.frame(bSpline(x=data$x , knots=s2$fit$knot[5:11], degree=3, intercept = FALSE))
data <- data.frame(data, bsx)
bs100 <- data.frame(bSpline(x=0:100/100, knots=s2$fit$knot[5:11], degree=3, intercept = FALSE))
bs100 <- data.frame(x=0:100/100, bs100)
lm_y18 <- lm(data=data[,-2], y18 ~ .)
bs100$y <- predict(lm_y18, bs100)
#---- question 1 ----
s2$fit$knot
diff(s2$fit$knot)
#---- question 2 ----
ggplot() + geom_point(data=data, aes(x=x, y=y18)) +
geom_line(aes(x=p$x, y=p$y)) +
geom_line(aes(x=p02$x, y=p02$y)) +
geom_line(aes(x=bs100$x, y=bs100$y), color=2) +
scale_x_continuous(labels = scales::percent) +
geom_vline(xintercept=s2$fit$knot, lty=2, color="grey")
cbind(s2$fit$coef,lm_y18$coefficients, s2$fit$coef-lm_y18$coefficients)
s02 <- smooth.spline(y=data$y18, x=data$x, nknots = 9, spar=0.2 )
p02 <- predict(s02, xx)