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.575  -313.  642.  673.  672.  683.
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.539  -290.  604.  651.  626.  653.
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.566  -307.  632.  667.  660.  676.
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.538  -289.  603.  654.  623.  656.
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.537  -288.  609.  675.  621.  670.

# Empty changepoint sets are allowed
fit_lmshift(CET, tau = NULL)
#> List of 6
#>  $ data         : Time-Series [1:362] from 1 to 362: 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 "[0,362]"
#>   ..$ param_mu: num 9.28
#>  $ model_params : Named num 0.461
#>   ..- attr(*, "names")= chr "sigma_hatsq"
#>  $ fitted_values: Named num [1:362] 9.28 9.28 9.28 9.28 9.28 ...
#>   ..- attr(*, "names")= chr [1:362] "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:362] from 1 to 362: 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] "[0,42)" "[42,362]"
#>   ..$ param_mu: num [1:2] 8.68 9.35
#>  $ model_params : Named num 0.415
#>   ..- attr(*, "names")= chr "sigma_hatsq"
#>  $ fitted_values: Named num [1:362] 8.68 8.68 8.68 8.68 8.68 ...
#>   ..- attr(*, "names")= chr [1:362] "1" "2" "3" "4" ...
#>  $ model_name   : chr "meanshift"
#>  - attr(*, "class")= chr "mod_cpt"