Tutorials › Regression Analysis (Undergraduate) › Multiple Knots and Segment-Specific Slopes

Piecewise Regression and Splines · Tutorial 448 of 991

Multiple Knots and Segment-Specific Slopes

Build and interpret a multiple-knot hinge model by translating its coefficients into segment slopes and fitted values.

Advanced 10 min read

What You'll Learn

  • Define a continuous piecewise linear model with several prespecified knots
  • Express each segment slope as the baseline slope plus accumulated hinge coefficients
  • Interpret a hinge coefficient as the change in slope at its knot
  • Expand the hinge model into equations for each segment
  • Calculate fitted values at predictor values on different segments
  • Check continuity at each knot and avoid confusing slope changes with segment slopes

Why Use More Than One Knot?

A straight-line regression describes a constant rate of change across the entire predictor range. Sometimes a relationship is better represented by straight-line segments with different slopes in different ranges. For example, a hypothetical process may show a steady increase at first, a slower increase after a threshold, and a faster increase after a second threshold. Multiple knots let a model represent these changing rates without requiring a jump in the fitted relationship.

The key accounting idea is simple: the first slope is the baseline slope, and each knot adds a change to the slope for all segments to its right. Keeping track of those accumulated changes makes it possible to interpret the model and calculate predictions across several segments.

The Multiple-Knot Hinge Model

Suppose \(x\) is a quantitative predictor and there are \(K\) fixed, ordered knots, \(\kappa_1<\kappa_2<\cdots<\kappa_K\). A hinge function at knot \(\kappa_j\) is zero to the left of the knot and increases linearly to its right.

Definition: The hinge function at knot \(\kappa_j\) is \((x-\kappa_j)_+=\max(0,x-\kappa_j)\). A continuous piecewise linear regression model with \(K\) prespecified knots is \[ Y_i=\beta_0+\beta_1x_i+\sum_{j=1}^{K}\gamma_j(x_i-\kappa_j)_++\varepsilon_i. \] Here, \(\beta_0\) is the intercept, \(\beta_1\) is the slope before the first knot, and \(\gamma_j\) is the change in slope at knot \(\kappa_j\). The fitted value is obtained by replacing the unknown coefficients by their estimates: \(\hat{y}(x)=\hat{\beta}_0+\hat{\beta}_1x+\sum_{j=1}^{K}\hat{\gamma}_j(x-\kappa_j)_+\).

The knots are treated as fixed in this model. Its hinge terms are zero before their respective knots, so they do not affect the fitted line on the left of a knot. After the knot, a hinge term is linear in \(x\); it changes the slope but does not introduce a jump in the fitted value.

As in a standard linear regression model, the response is modeled as a mean function plus an error. For the usual least-squares interpretation, assume the errors have conditional mean zero given the predictor values, are independent, and have a common variance. The piecewise form specifies the shape of the conditional mean: it is linear within each interval and continuous at the knots. These assumptions support the regression model; the calculations below focus on its fitted mean, not on uncertainty or significance tests.

How the Segment Slopes Accumulate

To find the slope on a segment, include the baseline slope and every hinge coefficient whose knot lies to the left of that segment. A hinge term to the right contributes zero there. This gives the first segment slope \(\beta_1\), the next slope \(\beta_1+\gamma_1\), and so on.

$$ \begin{aligned} \text{For } x<\kappa_1:\quad &\text{slope}=\beta_1,\\ \text{For } \kappa_r<x<\kappa_{r+1}:\quad &\text{slope}=\beta_1+\sum_{j=1}^{r}\gamma_j,\quad r=1,\ldots,K-1,\\ \text{For } x>\kappa_K:\quad &\text{slope}=\beta_1+\sum_{j=1}^{K}\gamma_j. \end{aligned} $$

For fitted segment slopes, use the same sums with hats on the coefficients. A hinge coefficient is therefore not generally the slope of the segment after its knot. It is the amount by which that segment's slope changes relative to the segment immediately before the knot. A positive \(\gamma_j\) makes the slope larger; a negative \(\gamma_j\) makes it smaller.

Theorem: In the hinge model, the fitted relationship is continuous at every knot. At \(x=\kappa_r\), the new hinge term is \((\kappa_r-\kappa_r)_+=0\); all later hinge terms are also zero. Thus the fitted value at the knot is the same value approached from the segment on its left and the segment on its right. The slope may change there, but the fitted value does not jump.

This follows directly from the definition of the hinge. Just to the left of a knot, its hinge value is zero. At the knot, it is still zero. Just to the right, it begins increasing from zero. The contribution changes continuously, even though its rate of change changes at the knot.

Worked Example: Two Knots and Three Segment Slopes

Consider a hypothetical fitted model for a response measured in points, with predictor \(x\) measured in hours. The two fixed knots are at 4 and 8 hours, and the estimated coefficients are \(\hat{\beta}_0=10\), \(\hat{\beta}_1=2\), \(\hat{\gamma}_1=-0.5\), and \(\hat{\gamma}_2=1.5\). Find the three segment slopes and fitted values at \(x=2,6,\) and \(10\).

The fitted model is \[ \hat{y}(x)=10+2x-0.5(x-4)_+ +1.5(x-8)_+. \] For \(x<4\), both hinges are zero, so the slope is \(2\) points per hour. For \(4<x<8\), the first hinge is active and the second is zero: \[ 2+(-0.5)=1.5\text{ points per hour}. \] For \(x>8\), both hinges are active, so the slope is \[ 2+(-0.5)+1.5=3\text{ points per hour}. \]

At \(x=2\), both hinge terms are zero: \[ \hat{y}(2)=10+2(2)-0.5(0)+1.5(0)=14\text{ points}. \] At \(x=6\), the first hinge is \(6-4=2\) and the second is zero: \[ \hat{y}(6)=10+2(6)-0.5(2)+1.5(0)=22-1=21\text{ points}. \] At \(x=10\), the hinges are \(10-4=6\) and \(10-8=2\): \[ \hat{y}(10)=10+2(10)-0.5(6)+1.5(2)=30-3+3=30\text{ points}. \] The predictions use the appropriate active hinges at each predictor value; the slopes describe rates of change, not predicted response values.

Writing an Equation for Each Segment

A useful way to check the slope bookkeeping is to expand the hinge model separately on each interval. On the first interval every hinge is zero. On each later interval, replace every active hinge by \(x-\kappa_j\) and simplify. The resulting equation gives both the segment slope and its intercept. The intercept can differ between segments even though the fitted values meet at the knots.

Worked Example: Expanding a Model with Three Knots

Suppose a hypothetical fitted model has response measured in units and predictor \(x\) measured in days. Its knots are 2, 5, and 7 days, with \(\hat{\beta}_0=12\), \(\hat{\beta}_1=-1\), \(\hat{\gamma}_1=2\), \(\hat{\gamma}_2=-3\), and \(\hat{\gamma}_3=1.5\). Expand the model by segment and calculate fitted values at \(x=1,4,6,\) and \(9\).

The hinge form is \[ \hat{y}(x)=12-x+2(x-2)_+-3(x-5)_++1.5(x-7)_+. \] Before 2 days, all hinges are zero, giving \(\hat{y}(x)=12-x\), with slope \(-1\) unit per day. Between 2 and 5 days, only the first hinge is active: \[ 12-x+2(x-2)=12-x+2x-4=8+x. \] Its slope is \(-1+2=1\) unit per day. Between 5 and 7 days, the first two hinges are active: \[ 12-x+2(x-2)-3(x-5)=12-x+2x-4-3x+15=23-2x. \] Its slope is \(-1+2-3=-2\) units per day. After 7 days, all three are active: \[ 23-2x+1.5(x-7)=23-2x+1.5x-10.5=12.5-0.5x. \] Its slope is \(-1+2-3+1.5=-0.5\) units per day.

Now calculate the requested predictions from the applicable segment equations: \[ \hat{y}(1)=12-1=11,\quad \hat{y}(4)=8+4=12,\quad \hat{y}(6)=23-2(6)=11,\quad \hat{y}(9)=12.5-0.5(9)=8. \] The fitted values are 11, 12, 11, and 8 response units, respectively. As a continuity check, the left and right equations both give 10 at \(x=2\), both give 13 at \(x=5\), and both give 9 at \(x=7\).

Recovering Hinge Coefficients from Slopes

The cumulative-sum rule also works in reverse. If segment slopes are specified, the baseline coefficient \(\beta_1\) is the first slope. Each hinge coefficient is the difference between the slope just after its knot and the slope just before it. This difference tells you how much the line's rate changes at that knot.

$$ \gamma_j=(\text{slope just right of }\kappa_j)-(\text{slope just left of }\kappa_j). $$

Worked Example: Turning Segment Slopes into Hinge Coefficients

A hypothetical fitted relationship has knots at 10 and 20 milligrams. Its desired segment slopes are 3 response units per milligram before 10, \(-1\) between 10 and 20, and 2 after 20. Let the fitted value at \(x=0\) be 5 response units. Write its hinge model and predict at \(x=5,15,\) and \(25\).

The baseline slope is the first segment slope, so \(\hat{\beta}_1=3\), and the intercept is \(\hat{\beta}_0=5\). At the first knot, the slope changes from 3 to \(-1\), so \[ \hat{\gamma}_1=-1-3=-4. \] At the second knot, it changes from \(-1\) to 2, so \[ \hat{\gamma}_2=2-(-1)=3. \] The model is \[ \hat{y}(x)=5+3x-4(x-10)_++3(x-20)_+. \]

For \(x=5\), neither hinge is active: \[ \hat{y}(5)=5+3(5)=20\text{ response units}. \] For \(x=15\), only the first hinge is active: \[ \hat{y}(15)=5+3(15)-4(15-10)=5+45-20=30\text{ response units}. \] For \(x=25\), both hinges are active: \[ \hat{y}(25)=5+3(25)-4(25-10)+3(25-20)=5+75-60+15=35\text{ response units}. \] The slopes are \(3\), \(3-4=-1\), and \(3-4+3=2\), as required. The hinge coefficients encode slope changes, while the intercept anchors the fitted line's level.

A Practical Calculation Sequence

For a model with several knots, a consistent sequence reduces sign and segment errors. First order the knots. Then identify which hinge terms are active at the predictor value of interest: a term is active when \(x>\kappa_j\), and it equals zero when \(x\leq\kappa_j\). At a knot itself, the corresponding hinge is zero. Finally, evaluate the fitted-value formula or use the expanded equation for that segment.

1
List knots and coefficients.
Keep knots in increasing order and record the baseline slope and every hinge coefficient.
2
Accumulate slope changes.
Start with the baseline slope; add each \(\gamma_j\) when moving past its knot.
3
Locate the predictor value.
Determine which interval contains \(x\), or whether it lies exactly at a knot.
4
Calculate and check.
Evaluate the active hinges, show the arithmetic, and check the units and continuity at boundaries if useful.

Common Mistakes and Why the Model Matters

The hinge representation is useful because it expresses a flexible shape using a single regression equation, while keeping the fitted relationship continuous. It also makes the parameters interpretable: the baseline slope describes the first segment, and the later coefficients describe changes in slope. Predictions at different ranges can then be calculated without accidentally applying a single global slope everywhere.

  • Treating a hinge coefficient as the next segment's slope. If the baseline slope is 3 and the first hinge coefficient is \(-4\), the next slope is \(3-4=-1\), not \(-4\).
  • Adding only the most recent hinge coefficient. Segment slopes accumulate all hinge coefficients to the left. After three knots, the slope includes the baseline and all three changes.
  • Activating a hinge too early. At or before its knot, \((x-\kappa_j)_+=0\). For a predictor value to the left of the knot, that term contributes nothing.
  • Assuming a slope change creates a jump. The hinge is zero at its knot, so the fitted value remains continuous. This model changes slope without adding a separate jump term.
  • Confusing a fitted value with a slope. A fitted value has the units of the response; a slope has response units per predictor unit.

A final check is to evaluate the equations on either side of each knot. If those fitted values do not agree at the knot, either a hinge was expanded incorrectly or the calculation used the wrong active terms. The hinge form itself guarantees agreement when used correctly.

Check Your Understanding

Use the hinge definition and cumulative-slope rule to answer these questions.

  1. A model has knots at 3 and 6, baseline slope 2, and hinge coefficients \(-1\) and 4. What are its three segment slopes?
  2. In a model with knots at 5 and 9, what is the value of the first hinge term at \(x=5\), and why?
  3. Segment slopes are 4 before the first knot, 1 between two knots, and \(-2\) after the second. What are the two hinge coefficients?
  4. For \(\hat{y}(x)=7+2x-3(x-4)_++2(x-8)_+\), calculate \(\hat{y}(6)\). Show which hinges are active.
  5. Explain why adding a hinge term changes the slope after its knot without creating a jump in the fitted value.