Convert, retrieve, or verify a model object
as.model.Rd
Convert, retrieve, or verify a model object
Usage
as.model(object, ...)
# Default S3 method
as.model(object, ...)
# S3 method for class 'tidycpt'
as.model(object, ...)
is_model(x, ...)
Details
tidycpt objects have a model
component.
The functions documented here are convenience utility functions
for working with the model
components.
as.model()
is especially useful in pipelines to avoid having to use
the $
or [
notation for subsetting.
When applied to a tidycpt object, as.model()
simply returns the
model
component of that object.
However, when applied to a segmenter
object, as.model()
attempts to
converts that object into a mod_cpt model object.
is_model()
checks to see if a model object implements all of the
S3 methods necessary to be considered a model.
See also
Other tidycpt-generics:
as.segmenter()
,
changepoints()
,
diagnose()
,
fitness()
,
model_name()
Examples
# Segment a time series using PELT
x <- segment(CET, method = "pelt")
# Retrieve the model component
x |>
as.model()
#> 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 : int [1:5] 55 57 309 311 330
#> $ region_params: tibble [6 × 3] (S3: tbl_df/tbl/data.frame)
#> ..$ region : chr [1:6] "[0,55)" "[55,57)" "[57,309)" "[309,311)" ...
#> ..$ param_mu : num [1:6] 8.8 9.04 9.23 9.48 9.49 ...
#> ..$ param_sigma_hatsq: Named num [1:6] 0.3744 0.16 0.3575 0.0256 0.1518 ...
#> .. ..- attr(*, "names")= chr [1:6] "[0,55)" "[55,57)" "[57,309)" "[309,311)" ...
#> $ model_params : NULL
#> $ fitted_values: num [1:362] 8.8 8.8 8.8 8.8 8.8 ...
#> $ model_name : chr "meanvar"
#> - attr(*, "class")= chr "mod_cpt"
# Explicitly convert the segmenter to a model
x |>
as.segmenter() |>
as.model()
#> 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 : int [1:5] 55 57 309 311 330
#> $ region_params: tibble [6 × 3] (S3: tbl_df/tbl/data.frame)
#> ..$ region : chr [1:6] "[0,55)" "[55,57)" "[57,309)" "[309,311)" ...
#> ..$ param_mu : num [1:6] 8.8 9.04 9.23 9.48 9.49 ...
#> ..$ param_sigma_hatsq: Named num [1:6] 0.3744 0.16 0.3575 0.0256 0.1518 ...
#> .. ..- attr(*, "names")= chr [1:6] "[0,55)" "[55,57)" "[57,309)" "[309,311)" ...
#> $ model_params : NULL
#> $ fitted_values: num [1:362] 8.8 8.8 8.8 8.8 8.8 ...
#> $ model_name : chr "meanvar"
#> - attr(*, "class")= chr "mod_cpt"
# Is that model valid?
x |>
as.model() |>
is_model()
#> [1] TRUE
# Fit a model directly, without using [segment()]
x <- fit_nhpp(CET, tau = 330)
is_model(x)
#> [1] TRUE