The situation
I have a dataset with one dependent $y$ and one independent variable $x$. I want to fit a continuous piecewise linear regression with $k$ known/fixed breakpoints occurring at $(a_{1}, a_{2}, \ldots, a_{k})$. The breakpoins are known without uncertainty, so I don't want to estimate them. Then I fit a regression (OLS) of the form
$$
y_{i} = \beta_{0} + \beta_{1}x_{i} + \beta_{2}\operatorname{max}(x_{i}-a_{1},0) + \beta_{3}\operatorname{max}(x_{i}-a_{2},0) +\ldots+ \beta_{k+1}\operatorname{max}(x_{i}-a_{k},0) +\epsilon_{i}
$$
Here is an example in R
set.seed(123)
x <- c(1:10, 13:22)
y <- numeric(20)
y[1:10] <- 20:11 + rnorm(10, 0, 1.5)
y[11:20] <- seq(11, 15, len=10) + rnorm(10, 0, 2)
Let's assume that the breakpoint $k_1$ occurs at $9.6$:
mod <- lm(y~x+I(pmax(x-9.6, 0)))
summary(mod)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 21.7057 1.1726 18.511 1.06e-12 ***
x -1.1003 0.1788 -6.155 1.06e-05 ***
I(pmax(x - 9.6, 0)) 1.3760 0.2688 5.120 8.54e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
The intercept and slope of the two segments are: $21.7$ and $-1.1$ for the first and $8.5$ and $0.27$ for the second, respectively.
Questions
- How to easily calculate the intercept and slope of each segment? Can the model be reparemetrized to do this in one calculation?
- How to calculate the standard error of each slope of each segment?
- How to test whether two adjacent slopes have the same slopes (i.e. whether the breakpoint can be omitted)?