I'm working through the ISLR book and I am incredibly confused by question 9)d) of chapter 7. It uses the MASS::Boston
dataset, and the question is as follows:
Use the
bs()
function to fit a regression spline to predictnox
usingdis
. Report the output for the fit using four degrees of freedom. How did you choose the knots? Plot the resulting fit.
My understanding from the book is that a cubic spline with $K$ knots uses $4+K$ degrees of freedom. This makes sense to me, because if you have a cubic spline with $K = 1$ knot $\xi$, it can be represented by a cubic plus an additional truncated power basis function: $$y = \beta_0 + \beta_1 x + \beta_2 x^2 + \beta_3 x^3 + \beta_4(x - \xi)_{+}^3$$
so there are $5$ parameters to estimate. But if a cubic spline with one knot has 5 degrees of freedom, how am I supposed to select knots that achieve a "fit with 4 degrees of freedom"?
I thought it could possibly be a typo or perhaps the authors want me to use a quadratic spline instead so I have 'room' for a knot. If I just pass df = 4
into the bs()
function in my linear model, I get a cubic spline with 1 knot, but since the model also has an intercept, my final fit has 5!
library(MASS)
library(gam)
spline_fit <- lm(nox ~ bs(dis, df = 4), data = Boston)
attr(bs(Boston$dis, df = 4), "knots") # 1 knot at 50%
summary(spline_fit)$df[1] # 5 degrees of freedom in total
I feel like the only way to reduce this to 4 degrees of freedom and still have at least 1 knot is to move from a cubic spline to quadratic/linear splines, but there the book almost exclusively uses cubic splines in the questions and examples so I am unsure if that's really what is expected.
There is something i'm not grasping here, but I don't know what! What model are the authors expecting when they say "Report the output for the fit using four degrees of freedom"?