Tutorials › Regression Analysis (Undergraduate) › Cubic Regression Splines and Smoothness Constraints

Piecewise Regression and Splines · Tutorial 454 of 991

Cubic Regression Splines and Smoothness Constraints

See how piecewise cubic regression curves are joined by matching function values and derivatives at their knots.

Advanced 9 min read

What You'll Learn

  • Define a cubic spline as a function made from cubic polynomials on adjoining intervals.
  • Distinguish continuity of the curve from continuity of its slope and curvature.
  • State the smoothness conditions for a twice-continuously-differentiable cubic spline.
  • Check whether concrete piecewise polynomials satisfy those conditions at a knot.
  • Explain why a cubic spline can have a jump in its third derivative and still be smooth in the required sense.

Why Join Cubic Pieces?

A single polynomial can describe a curved relationship between a predictor and a response, but its shape is constrained across the whole range of the predictor. Piecewise regression offers another option: use different polynomial pieces over different ranges, then join them at selected predictor values called knots. The challenge is to avoid a fitted curve that makes an abrupt jump or changes direction unnaturally at a join.

For cubic pieces, the joins can be controlled by matching the pieces’ values and derivatives at each knot. This tutorial defines that construction and shows what the matching conditions mean. It focuses on the shape of the fitted function, rather than on how to count its parameters or choose a particular polynomial basis.

Definition: A cubic spline is a function defined by polynomial pieces of degree at most 3 on adjoining intervals of the predictor. An interior point where adjacent pieces meet is a knot. A cubic spline that is twice continuously differentiable at each interior knot has matching function values, first derivatives, and second derivatives there. These conditions are often called C² continuity.

The word “cubic” describes the degree allowed for each piece; it does not mean the entire spline is one cubic polynomial. Different intervals can have different polynomial coefficients. The first derivative describes the local slope of the curve, and the second derivative describes how that slope changes. Thus, matching values joins the pieces without a jump, matching first derivatives avoids a sharp corner, and matching second derivatives avoids a jump in curvature.

The Regression Model and the Joining Conditions

Let \(x_i\) be a predictor value and \(Y_i\) the corresponding response for observation \(i\). A regression spline specifies a function \(f\) for the conditional mean of the response:

$$ Y_i=f(x_i)+\varepsilon_i,\qquad E(\varepsilon_i\mid x_i)=0. $$

For the usual least-squares regression model, the errors are taken to be independent with constant variance, so \(\operatorname{Var}(\varepsilon_i\mid x_i)=\sigma^2\). These assumptions concern the observations around the curve; they do not themselves guarantee that the curve is a spline. The spline requirement is a separate condition on the form of \(f\). Gaussian errors are not needed to define a spline or to describe its smoothness.

Suppose a knot is located at \(x=\kappa\), with a polynomial \(p_L\) to its left and a polynomial \(p_R\) to its right. A C² join requires:

$$ p_L(\kappa)=p_R(\kappa),\qquad p_L'(\kappa)=p_R'(\kappa),\qquad p_L''(\kappa)=p_R''(\kappa). $$

The first equality makes the curve continuous: both pieces give the same fitted response at the knot. The second makes the slope agree as the curve passes through the knot. The third makes the curvature agree there. A continuous curve with a matching slope but mismatched second derivatives is smoother than a curve with a corner, but it does not meet the C² condition.

Worked Example: A C² Join With Different Cubic Pieces

Consider two hypothetical polynomial pieces joined at \(\kappa=2\):

$$ p_L(x)=(x-2)^3\quad (x\leq 2),\qquad p_R(x)=2(x-2)^3\quad (x\geq 2). $$

At the knot, both pieces have value \(0\):

$$ p_L(2)=0^3=0,\qquad p_R(2)=2(0^3)=0. $$

Their first derivatives are \(p_L'(x)=3(x-2)^2\) and \(p_R'(x)=6(x-2)^2\), so both derivatives equal \(0\) at \(x=2\). Their second derivatives are \(p_L''(x)=6(x-2)\) and \(p_R''(x)=12(x-2)\), which also both equal \(0\) at the knot. The pieces therefore satisfy all three C² conditions.

They are not the same polynomial: at \(x=1\), the left piece gives \((-1)^3=-1\), while at \(x=3\), the right piece gives \(2(1)^3=2\). Their third derivatives are \(6\) and \(12\), respectively, so the third derivative does not match. That does not violate C² continuity; matching the third derivatives is not required for a cubic spline to be twice continuously differentiable.

Why the Conditions Match the Local Polynomial Shape

The joining rule can be justified by writing each cubic piece in terms of its value and derivatives at the knot. Any polynomial of degree at most 3 can be expressed around \(x=\kappa\) as a constant, a linear term, a quadratic term, and a cubic term. The coefficients of the first three terms are determined by the value, first derivative, and second derivative at \(\kappa\).

$$ p(x)=p(\kappa)+p'(\kappa)(x-\kappa) +\frac{p''(\kappa)}{2}(x-\kappa)^2+c(x-\kappa)^3. $$

Here \(c\) is the coefficient of the cubic term in this centered expression. To see why the formula applies, write a general cubic as \(a+b(x-\kappa)+d(x-\kappa)^2+c(x-\kappa)^3\). Substituting \(x=\kappa\) gives \(p(\kappa)=a\); differentiating and substituting gives \(p'(\kappa)=b\); differentiating twice gives \(p''(\kappa)=2d\). Replacing \(a\), \(b\), and \(d\) yields the displayed expression.

Apply this expression to the left and right pieces at the same knot. If their values, first derivatives, and second derivatives match, all terms through degree 2 are identical on both sides. Their difference is then just a multiple of \((x-\kappa)^3\):

$$ p_R(x)-p_L(x)=(c_R-c_L)(x-\kappa)^3. $$

At \(x=\kappa\), this difference and its first two derivatives are zero. Thus, even if the cubic coefficients differ, the pieces meet with equal value, slope, and curvature. This also explains why the third derivative can differ: differentiating the difference three times gives \(6(c_R-c_L)\), which need not be zero.

Worked Example: Checking a Cubic Join From Its Coefficients

Suppose the knot is \(\kappa=1\), and a hypothetical curve uses the following pieces:

$$ p_L(x)=4+3(x-1)+2(x-1)^2+(x-1)^3, $$ $$ p_R(x)=4+3(x-1)+2(x-1)^2-\frac{1}{2}(x-1)^3. $$

At \(x=1\), every term containing \(x-1\) is zero, so \(p_L(1)=4\) and \(p_R(1)=4\). Differentiating gives \(p_L'(x)=3+4(x-1)+3(x-1)^2\) and \(p_R'(x)=3+4(x-1)-\frac{3}{2}(x-1)^2\). Each derivative equals \(3\) at \(x=1\).

The second derivatives are \(p_L''(x)=4+6(x-1)\) and \(p_R''(x)=4-3(x-1)\), so each equals \(4\) at the knot. The value, slope, and second derivative therefore match: this is a C² join. The cubic coefficients differ, so the third derivatives are \(6\) on the left and \(-3\) on the right.

For a numerical check away from the knot, at \(x=2\) the right piece gives \(4+3+2-\frac{1}{2}=8.5\). Its slope there is \(3+4-\frac{3}{2}=5.5\), in response units per predictor unit. These values describe the right-hand piece at \(x=2\); the matching conditions apply at the knot \(x=1\).

Distinguishing Smoothness From Continuity Alone

A curve can be continuous without having a continuous slope. It can also have a continuous slope without having a continuous second derivative. These are different levels of smoothness, so checking only that the two pieces share the same value at the knot is not enough to establish a C² join.

Worked Example: A Join With Matching Slope but Mismatched Curvature

At the hypothetical knot \(\kappa=1\), take

$$ p_L(x)=(x-1)^2\quad (x\leq 1),\qquad p_R(x)=2(x-1)^2\quad (x\geq 1). $$

Both pieces have value \(0\) at \(x=1\). Their first derivatives are \(p_L'(x)=2(x-1)\) and \(p_R'(x)=4(x-1)\), so both slopes are \(0\) at the knot. However, \(p_L''(x)=2\) and \(p_R''(x)=4\). The second derivatives do not match, so the pieces form a C¹ join, not a C² join.

For example, at \(x=2\), the right piece has value \(2(1)^2=2\), whereas extending the left polynomial to \(x=2\) would give \(1\). This difference away from the knot is allowed: the two polynomials are used on separate intervals. The issue is specifically whether their values and derivatives agree at \(x=1\).

Interpreting a Fitted Cubic Spline

A fitted cubic spline represents a flexible mean relationship using polynomial pieces, while its joining conditions keep the curve from jumping or acquiring a sharp corner at an interior knot. The curve’s local slope and curvature can still vary across the predictor range, because each interval has its own polynomial piece. The result is not a claim that the underlying relationship is exactly cubic everywhere; it is a chosen regression form for describing the mean response.

The derivatives also carry units. If the response is measured in milligrams and the predictor in hours, the first derivative is measured in milligrams per hour and the second derivative in milligrams per hour squared. Matching a derivative means that its left- and right-hand values agree in those units at the knot. It does not mean the derivative is zero.

The C² requirement concerns interior knots where two modeled pieces meet. It does not by itself specify behavior beyond the ends of the observed predictor range. Nor does a smooth join guarantee that the overall fitted curve is scientifically sensible: the choice of knots and the fit to the observed data still matter.

Common Mistake

  • Wrong reasoning: “The pieces have the same value at the knot, so the spline is smooth.” Matching values establishes continuity only; the curve can still have a corner or a curvature jump.
  • Correction: For a C² cubic spline, check the value, first derivative, and second derivative from both sides at every interior knot. The third derivatives may differ.
  • Another mistake: “A cubic spline must be one cubic polynomial over the entire predictor range.” Each interval has its own polynomial piece; “cubic” limits the degree of those pieces, not the number of pieces.
Key takeaway: At every interior knot of a twice-continuously-differentiable cubic regression spline, the pieces must agree in fitted value, slope, and second derivative. This gives a continuous curve with no jump in slope or curvature, while allowing the cubic terms to differ across intervals.

Check Your Understanding

Use the definitions and joining conditions to answer these questions.

  1. What are the three quantities that must agree on the two sides of a C² knot?
  2. Two cubic pieces have equal values and equal first derivatives at a knot, but unequal second derivatives. What level of continuity do they have, and what condition fails for C² continuity?
  3. Can two pieces form a C² join if their third derivatives differ? Explain using the joining conditions.
  4. For \(p_L(x)=2+(x-3)+(x-3)^2\) and \(p_R(x)=2+(x-3)+4(x-3)^2\), check their values and first derivatives at \(x=3\). Do they satisfy the C² condition?
  5. If a response is measured in grams and a predictor in days, what are the units of the first and second derivatives of the fitted curve?