Piecewise Polynomials
Instead of fitting a high-degree polynomial over the entire range of , piecewise polynomial regression involves fitting separate low-degree polynomials over different regions of .
A piecewise cubic polynomial with a single knot at a point takes the form: Using more knots leads to a more flexible piecewise polynomial.But the function is discontinuous and looks ridiculous! Since each polynomial has four parameters, we are using a total of eight degree of freedom in fitting this piecewise polynomial model.
Constraints and Splines
We can fit a piecewise polynomial under the constraint that the fitted curve must be continuous.
In the lowest left top, we have added two additional constraints: now both the first and second derivatives of the piecewise polynomials are continuous at age=50
(not only continuous but also very smooth).
Each constraint that we impose on the piecewise cubic polynomials effectively frees up one degree of freedom, by reducing the complexity of the resulting piecewise polynomial fit. (我们对分段三项式施加的每个约束,通过降低分段多项式拟合的复杂度,能够有效的释放一个自由度). So in the top left plot, we are using eight degrees of freedom, but in the bottom left plot we imposed three constraints(continuity, continuity of the first derivative, and continuity of the second derivative) and so are left with five degrees of freedom.
The curve in the bottom left plot is called a cubic spline. In general, a cubic spline with knots uses a total of degrees of freedom.
The Spline Basis Representation
We fit a piecewise degree-d polynomial under the constraint that it be continuous? It turns out that we can use the basis model to represent a regression spline. A cubic spline with knots can be modeled as: for an appropriate choice of basis functions .The model can then be fit using least squares.
In other words, in order to fit a cubic spline to a data set with knots, we perform least squares regression with an intercept and predictors, for this reason, fitting a cubic spline with knots uses degrees of freedom.
Unfortunately, spline can have high variance at the outer range of the predictors—that is, when takes on either a very small or very large value. A natural spline is a regression spline with additional boundary constraints: the function is required to be linear at the boundary .
Choosing the Number and Locations of the Knots
When we fit a spline, where should we place the knots? The regression spline is most flexible in regions that contain a lot of knots, hence,
- One option is to place more knots in places where we feel the function might vary most rapidly, and to place fewer knots where it seems more stable.
- One way to do this is to specify the desired degrees of freedom, and then have the software automatically place the corresponding number of knots at uniform quantiles of the data.
How many knots should we use, or equivalently how many degrees of freedom should our spline contain?
- One option is to try out different numbers of knots and see which produces the best looking curve.
- A somewhat more objective approach is to use cross-validation. This procedure can be repeated for different numbers of knots , then the value of giving the smallest RSS is chosen.
In Section 7.7, we fit additive spline models simultaneously on several variables at a time. This could potentially require the selection of degrees of freedom for each variables. In cases like this we typically adopt a more pragmatic approach and set the degrees of freedom to a fixed number, say four, for all terms.
Comparison to Polynomial Regression
Regression splines often give superior results to polynomial regression. This is because unlike polynomials, which must use a high degree to produce flexible fits, splines introduce flexibility by increasing the number of knots but keeping the degree fixed.
Splines also allow us to place more knots, and hence flexibility, over regions where the function seems to be changing rapidly, and fewer knots where appears more stable.
The extra flexibility in the polynomial produces undesirable results at the boundaries, while the nature cubic spline still provides a reasonable fit to the data.