# ISLR splines - degrees of freedom confusion?

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 predict nox using dis. 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 # 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"?

There's several possibilities:

1. They intend you to fit an ordinary cubic regression spline with 4 df total. (In the context of this chapter, I think this is probably what they meant.)

The answer to "how did you choose the knots" is just "I didn't -- there's no d.f. left for any".

2. They meant "with 4 knots" rather than with 4df.

In this case you can proceed without difficulty. This may have been the intent but I doubt it.

3. They didn't intend you to count the intercept, leaving you with one knot.

This presents no difficulty. In this case (given the context of the chapter and the question) I doubt this was the intent.

4. They meant ns() not bs()

In this case you can proceed without difficulty. This might have been the intent but I'm not convinced of this either.

5. They meant bs and 4 df but intend you to use a lower order of polynomial.

In this case I'd tend to expect to see a linear spline rather than a quadratic, but they didn't mention either one. I don't think this was the intent; if it was I'd really expect them to mention it explicitly.

If you want to use the book as a personal learning tool, rather than just answer questions for some other purpose (say for an assignment), you would perhaps be better to consider each possibility in turn and try to fit them. That's usually my approach with self learning -- if it's not clear what the intent was, just do everything it might have been. With the data already there and so forth, the extra time to try each thing is pretty small.

• Thanks a lot for the detailed answer! In part 3 about not including the intercept, is whether the intercept 'counts' as a degree of freedom something the varies from text to text? I was having trouble answering this question online, but I just stumbled across this resource, slides 19 & 20, that seem to imply that this is the case. This isn't an assignment so I will follow your advice here and try all methods! Sep 2, 2020 at 8:53
• Some books do omit it in regression discussion (counting the predictors rather than the parameters), but the ISLR discussion of bs doesn't. Nevertheless it's possible for them to forget that when writing the question. Sep 2, 2020 at 10:28