Skip to content

Fits a Markov-Switching regression on a 'tidyFit' R6 class. The function can be used with regress.

Usage

# S3 method for mslm
.fit(self, data = NULL)

Arguments

self

a 'tidyFit' R6 class.

data

a data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr).

Value

A fitted 'tidyFit' class model.

Details

Hyperparameters:

None. Cross validation not applicable.

Important method arguments (passed to m)

  • k (the number of regimes)

  • sw (logical vector indicating which coefficients switch)

  • control (additional fitting parameters)

The function provides a wrapper for MSwM::msmFit. See ?msmFit for more details.

Implementation

Note that only the regression method with 'lm' is implemented at this stage.

An argument index_col can be passed, which allows a custom index to be added to coef(m("mslm")) (e.g. a date index).

If no sw argument is passed, all coefficients are permitted to switch between regimes.``

References

Sanchez-Espigares JA, Lopez-Moreno A (2021). MSwM: Fitting Markov Switching Models. R package version 1.5, https://CRAN.R-project.org/package=MSwM.

See also

.fit.tvp and m methods

Author

Johann Pfitzinger

Examples

# Load data
data <- tidyfit::Factor_Industry_Returns
data <- dplyr::filter(data, Industry == "HiTec", Date >= 201801)
data <- dplyr::select(data, -Industry)

ctr <- list(maxiter = 100, parallelization = FALSE)

# Stand-alone function
fit <- m("mslm", Return ~ HML, data, index_col = "Date", k = 2, control = ctr)
fit
#> # A tibble: 1 × 5
#>   estimator_fct `size (MB)` grid_id  model_object settings        
#>   <chr>               <dbl> <chr>    <list>       <list>          
#> 1 MSwM::msmFit        0.102 #0010000 <tidyFit>    <tibble [1 × 3]>

# Within 'regress' function
fit <- regress(data, Return ~ HML,
               m("mslm", index_col = "Date", k = 2, control = ctr))
tidyr::unnest(coef(fit), model_info)
#> # A tibble: 108 × 9
#> # Groups:   model [1]
#>    model term    estimate  index std.error `Regime 1 Prob` `Regime 2 Prob`
#>    <chr> <chr>      <dbl>  <dbl>     <dbl>           <dbl>           <dbl>
#>  1 mslm  (Inter…    3.65  201801     0.762          1.00     0.00000000387
#>  2 mslm  (Inter…    3.56  201802     0.768          0.992    0.00832      
#>  3 mslm  (Inter…   -0.504 201803     0.980          0.632    0.368        
#>  4 mslm  (Inter…    3.58  201804     0.766          0.994    0.00620      
#>  5 mslm  (Inter…    3.65  201805     0.762          1.00     0.0000000586 
#>  6 mslm  (Inter…    3.43  201806     0.775          0.980    0.0198       
#>  7 mslm  (Inter…    3.65  201807     0.762          1.00     0.000229     
#>  8 mslm  (Inter…    3.65  201808     0.762          1.00     0.0000000219 
#>  9 mslm  (Inter…    3.57  201809     0.767          0.992    0.00772      
#> 10 mslm  (Inter…   -7.04  201810     1.25           0.0541   0.946        
#> # ℹ 98 more rows
#> # ℹ 2 more variables: `Regime 1 Beta` <dbl>, `Regime 2 Beta` <dbl>