In order to instead fit a natural spline, we use the ns() function. Here we fit a natural spline with four degrees of freedom.

fit2 <- lm(wage ~ ns(age, df = 4), data = Wage)
pred2 <- predict(fit2, newdata = list(age = age.grid), se = TRUE)
lines(age.grid, pred2$fit, col = "red", lwd = 2)

plot

As with the bs() function, we could instead specify the knots directly using the knots option.

Questions

Assume that: