P.Mean: How do I fit a piecewise linear regression (created 2008-10-07).
I was asked to look at some data that involved monitoring glucose and potassium levels before, during, and after a special infusion. You would expect, perhaps, that there would be a flat trend before, and upward or downward trend (possibly linear, possibly not) during administration, and a different trend (possibly linear, possibly not) after infusion. There's a simple regression model for this, which is sometimes called a piecewise linear regression, segmented regression, join point regression, or elbow regression.
Fitting these models is fairly easy if there is one transition point (break point, join point, change point) and it is specified in advance. Multiple change points make the problem more tedious, but not substantially harder. If you know that there will be a change, but you don't know when, then the problem is substantially harder. Let me outline the simplest case, a linear regression model where the join point is specified in advance.
First, you need to adjust the independent variable so that the join point occurs at zero. So if you know that the join point is at 20, subtract 20 from all the values of the independent variable.
Then compute an indicator variable which is equal to one if the shifted independent variable is positive and zero otherwise. Then fit a model with the shifted independent variable, the indicator, and an interaction between the independent variable and the indicator.
Here's a data set on firearm deaths in Australia from the OzDASL site.
The first column is the year, and the second column is the number of firearm deaths per 100,000 population.
Suppose there is a belief that the rate behaves differently before and after 1988. I'm not an expert on Australia or on crime statistics and just chose the value of 1988 based on a graph of the data. In practice, choosing a break point in such an uninformed way is very bad. Since I am using this as a teaching example, I can get away with such folly.
Here's what the adjusted data would look like.
1983 4.31 -5 0 0
1984 4.42 -4 0 0
1985 4.52 -3 0 0
1986 4.35 -2 0 0
1987 4.39 -1 0 0
1988 4.21 0 0 0
1989 3.40 1 1 1
1990 3.61 2 1 2
1991 3.67 3 1 3
1992 3.61 4 1 4
1993 2.98 5 1 5
1994 2.95 6 1 6
1995 2.72 7 1 7
1996 2.96 8 1 8
1997 2.30 9 1 9
The first new column is the adjusted year (year-1988). The second new column is an indicator variable for the post 1988 data. The third new column is an interaction term for the previous two columns, computed by simply multiplying the two columns together.
A model that predicts firearm deaths using the adjusted year, the post-1988 indicator, and an interaction term would produce the following prediction equation:
Here's a graph of this equation.
Notice that there is a sharp vertical drop at adj.year=0 (year=1988). The size of this drop (-0.4) is equal to the estimated coefficient for the post.1988 variable. The intercept (4.3) represents the estimated firearm deaths just prior to 1988. The sum of these two coefficients is 3.9, which represents the estimated firearm death just immediately beyond 1988. The coefficient for adj.year (-0.02) represents the estimated decline in firearm deaths during the time period prior to 1988. The interaction term (-0.13) represents how much the slope changes. The slope of the line after 1988 is the sum of the adj.year and interact coefficients The sum of the adj.year coefficient and the interaction coefficient (-0.02-0.13 = -0.15) is much more negative, showing a sharper decline in estimated firearm deaths after 1988.
You can force the two lines to join by omitting the post 1988 indicator. The coefficients are
This produces the following graph
The estimated firearm deaths at 1988 is 4.1. The decline in estimated firearm deaths is -0.08 prior to 1988 -0.08-0.10 = -0.18 after 1988. Like the earlier model, this shows a sharper decline after 1988 than before.
There are some other interesting variations on this model. If you include just the interaction, but not the adjusted year, that effectively makes the left side of the segmented regression line flat. The coefficients are
The estimated firearm deaths at 1988 and all years prior to the change point is 4.2. The firearm deaths decline by 0.2 units per year after 1988. The graph below shows this relationship.
Toss back in the indicator variable to allow a discontinuous jump at the change point.
The estimated firearm deaths is 4.4 prior to 1988. This estimate drops 0.5 units at 1988 and continues to decline at 0.14 units per year after 1988. The graph below shows this relationship.
If you include the adjusted year and the indicator in a model (but no interaction), you get a linear trend that is consistent on both sides of the change point, but a discontinuous shift in predicted firearm deaths at the change point.
The estimated decline is 0.3 units per year and there is also an instantaneous drop from 4.1 to 3.8 (4.1-0.3) in 1988. The graph below shows this relationship.
Finally a model that is flat both before and after the change point, but at different levels can be fit my including just the indicator variable.
The estimated firearm deaths are 4.4 up to 1988 and 3.2 (4.4-1.2) after 1988.
A model with just the adjusted time (no indicator and no interaction) fits a simple linear trend. You already knew how to do this, but I'm putting this here for the sake of completeness.
Firearm deaths are estimated to be 3.9 in 1988 and they decline 0.15 units throughout the entire time range.
In practice, you would not fit every single one of these models. Instead, you would select a model that makes sense based on your understanding of the process that produced these data. Ideally the model should be selected prior to looking at the data. If you base your model on the general appearance of the data, then your results should be considered more exploratory and less confirmatory.
This work is licensed under a Creative Commons Attribution 3.0 United States License. This page was written by Steve Simon and was last modified on 2010-04-01. Need more information? I have a page with general help resources. You can also browse for pages similar to this one at Category: Linear regression.