I made the R package mcp
exactly because there is a lack of packages quantifying the uncertainty (e.g., SE) about the inferred change point locations. Change point problems are conceptually simple in a Bayesian framework, and computationally accessible using variants of Gibbs sampling (read more in this preprint).
mcp
includes a dataset with three linear segments:
> head(ex_demo)
time response
1 68.35820 32.842651
2 87.29038 -1.160003
3 69.01173 27.564248
4 11.59361 10.062971
5 19.50091 14.056859
6 46.12009 18.292640
Let's fit a piecewise linear regression with three segments. In mcp
you do this as a list one formula per segment:
library(mcp)
# Define the model
model = list(
response ~ 1, # plateau
~ 0 + time, # joined slope
~ 1 + time # disjoined slope
)
# Fit it.
fit = mcp(model, data = ex_demo)
Let's visualize it first:
plot(fit)
The blue curves on the x-axis are the posteriors of the change points. You can see them more directly using plot_pars(fit)
. Note that they rarely conform to any "clean" known density like the normal distribution.
See summaries using summary(fit)
. mcp
includes functions to test parameter values, model comparison, etc. Read more on the mcp website.
