Learn R Programming

tidychangepoint (version 1.0.0)

fit_lmshift: Regression-based model fitting

Description

Regression-based model fitting

Usage

fit_lmshift(x, tau, deg_poly = 0, ...)

fit_lmshift_ar1(x, tau, ...)

fit_trendshift(x, tau, ...)

fit_trendshift_ar1(x, tau, ...)

Value

A mod_cpt object

Arguments

x

A time series

tau

a set of indices representing a changepoint set

deg_poly

integer indicating the degree of the polynomial spline to be fit. Passed to stats::poly().

...

arguments passed to stats::lm()

Details

These model-fitting functions use stats::lm() to fit the corresponding regression model to a time series, using the changepoints specified by the tau argument. Each changepoint is treated as a categorical fixed-effect, while the deg_poly argument controls the degree of the polynomial that interacts with those fixed-effects. For example, setting deg_poly equal to 0 will return the same model as calling fit_meanshift_norm(), but the latter is faster for larger changepoint sets because it doesn't have to fit all of the regression models.

Setting deg_poly equal to 1 fits the trendshift model.

  • fit_lmshift_ar1(): will apply auto-regressive lag 1 errors

  • fit_trendshift(): will fit a line in each region

  • fit_trendshift_ar1(): will fit a line in each region and autoregress lag 1 errors

See Also

Other model-fitting: fit_meanshift(), fit_meanvar(), fit_nhpp(), model_args(), model_name(), new_fun_cpt(), whomademe()

Examples

Run this code
# Manually specify a changepoint set
tau <- c(365, 826)

# Fit the model
mod <- fit_lmshift(DataCPSim, tau)

# Retrieve model parameters
logLik(mod)
deg_free(mod)

# Manually specify a changepoint set
cpts <- c(1700, 1739, 1988)
ids <- time2tau(cpts, as_year(time(CET)))

# Fit the model
mod <- fit_lmshift(CET, tau = ids)

# View model parameters
glance(mod)
glance(fit_lmshift(CET, tau = ids, deg_poly = 1))
glance(fit_lmshift_ar1(CET, tau = ids))
glance(fit_lmshift_ar1(CET, tau = ids, deg_poly = 1))
glance(fit_lmshift_ar1(CET, tau = ids, deg_poly = 2))

# Empty changepoint sets are allowed
fit_lmshift(CET, tau = NULL)

# Duplicate changepoints are removed
fit_lmshift(CET, tau = c(42, 42))

Run the code above in your browser using DataLab