Skip to contents

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, ...)

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()

Value

A mod_cpt object

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

Examples

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

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

# Retrieve model parameters
logLik(mod)
#> 'log Lik.' -5250.548 (df=6)
deg_free(mod)
#> [1] 6

# 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)
#> # A tibble: 1 × 11
#>   pkg     version algorithm params num_cpts  rmse logLik   AIC   BIC  MBIC   MDL
#>   <chr>   <pckg_> <chr>     <list>    <int> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 tidych… 0.0.1.… meanshift <dbl>         3 0.576  -317.  651.  682.  681.  692.
glance(fit_lmshift(CET, tau = ids, deg_poly = 1))
#> # A tibble: 1 × 11
#>   pkg     version algorithm params num_cpts  rmse logLik   AIC   BIC  MBIC   MDL
#>   <chr>   <pckg_> <chr>     <list>    <int> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 tidych… 0.0.1.… trendshi… <dbl>         3 0.538  -292.  608.  655.  630.  658.
glance(fit_lmshift_ar1(CET, tau = ids))
#> # A tibble: 1 × 11
#>   pkg     version algorithm params num_cpts  rmse logLik   AIC   BIC  MBIC   MDL
#>   <chr>   <pckg_> <chr>     <list>    <int> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 tidych… 0.0.1.… meanshif… <dbl>         3 0.567  -311.  640.  675.  668.  684.
glance(fit_lmshift_ar1(CET, tau = ids, deg_poly = 1))
#> # A tibble: 1 × 11
#>   pkg     version algorithm params num_cpts  rmse logLik   AIC   BIC  MBIC   MDL
#>   <chr>   <pckg_> <chr>     <list>    <int> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 tidych… 0.0.1.… trendshi… <dbl>         3 0.537  -291.  608.  658.  628.  661.
glance(fit_lmshift_ar1(CET, tau = ids, deg_poly = 2))
#> # A tibble: 1 × 11
#>   pkg     version algorithm params num_cpts  rmse logLik   AIC   BIC  MBIC   MDL
#>   <chr>   <pckg_> <chr>     <list>    <int> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 tidych… 0.0.1.… splinesh… <dbl>         3 0.535  -290.  613.  680.  625.  675.

# Empty changepoint sets are allowed
fit_lmshift(CET, tau = NULL)
#> List of 6
#>  $ data         : Time-Series [1:366] from 1 to 366: 8.87 9.1 9.78 9.52 8.63 9.34 8.29 9.86 8.52 9.51 ...
#>  $ tau          : NULL
#>  $ region_params: tibble [1 × 2] (S3: tbl_df/tbl/data.frame)
#>   ..$ region  : chr "[1,367)"
#>   ..$ param_mu: num 9.29
#>  $ model_params : Named num 0.489
#>   ..- attr(*, "names")= chr "sigma_hatsq"
#>  $ fitted_values: Named num [1:366] 9.29 9.29 9.29 9.29 9.29 ...
#>   ..- attr(*, "names")= chr [1:366] "1" "2" "3" "4" ...
#>  $ model_name   : chr "null"
#>  - attr(*, "class")= chr "mod_cpt"

# Duplicate changepoints are removed
fit_lmshift(CET, tau = c(42, 42))
#> List of 6
#>  $ data         : Time-Series [1:366] from 1 to 366: 8.87 9.1 9.78 9.52 8.63 9.34 8.29 9.86 8.52 9.51 ...
#>  $ tau          : num 42
#>  $ region_params: tibble [2 × 2] (S3: tbl_df/tbl/data.frame)
#>   ..$ region  : chr [1:2] "[1,42)" "[42,367)"
#>   ..$ param_mu: num [1:2] 8.68 9.37
#>  $ model_params : Named num 0.442
#>   ..- attr(*, "names")= chr "sigma_hatsq"
#>  $ fitted_values: Named num [1:366] 8.68 8.68 8.68 8.68 8.68 ...
#>   ..- attr(*, "names")= chr [1:366] "1" "2" "3" "4" ...
#>  $ model_name   : chr "meanshift"
#>  - attr(*, "class")= chr "mod_cpt"