Learn R Programming

MSTest

This package implements hypothesis testing procedures that can be used to identify the number of regimes in a Markov switching model. It includes the Monte Carlo moment-based test of Dufour & Luger (2017), the parametric bootstrap test described in Qu & Zhuo (2021) and Kasahara & Shimotsu (2018), the Monte Carlo Likelihood ratio tests of Rodriguez-Rondon & Dufour (2025), the optimal test for regime switching of Carrasco, Hu, & Ploberger (2014), and the likelihood ratio test described in Hansen (1992).

In addition to testing procedures, the package also includes datasets and functions that can be used to simulate: autoregressive, vector autoregressive, Markov switching autoregressive, and Markov switching vector autoregressive processes among others. Model estimation procedures are also available.

For a more detailed description of this package see Rodriguez-Rondon & Dufour (2024).

Installation

To install the package, use the following line:

install.packages("MSTest")

Load Package

Once package has been installed it can be loaded.

library(MSTest)

Data

The MSTest package includes 3 datasets that can be used as examples. The three datasets are:

  • hamilton84GNP: US GNP data from 1951Q2 - 1984Q4 used in Hamilton (1989) and Hansen (1992; 1996)
  • chp10GNP: US GNP data from 1951Q2 - 2010Q4 used by Carrasco, Hu and Ploberger (2014)
  • USGNP: US GNP data from 1947Q2 - 2022Q2 from FRED

They can be loaded using the following code.

GNPdata <- USGNP # this can be hamilton82GNP, chp10GNP or USGNP
Y       <- as.matrix(GNPdata$GNP_gr)
date    <- as.Date(GNPdata$Date)
plot(date,Y,xlab='Time',ylab='GNP - log difference',type = 'l')

You an also learn more about these datasets and their sources from their description in the help tab.

?hamilton84GNP

Model Estimation & Process simulation

This first example uses the US GNP growth data from 1951Q2-1984Q4 considered in Hamilton (1989). The data is made available as 'hamilton84GNP' through this package. In Hamilton (1989), the model is estimated with four autoregressive lags and only the mean is allowed to change between two (i.e., expansionary and recessionary) regimes and it is estimated by MLE and so we begin by estimating that model. Estimation results can be compared with those found in Hamilton (1994) p. 698. Note however, that standard errors here were obtained using a different approximation method and hence these may differ slightly.

set.seed(123) # for initial values
y_gnp_gw_84 <- as.matrix(hamilton84GNP$GNP_gr)


# Set options for model estimation
control <- list(msmu   = TRUE, 
                msvar  = FALSE, 
                method = "MLE",
                use_diff_init = 5)

# Estimate model with p=4 and switch in mean only as in Hamilton (1989)
hamilton89_mdl <- MSARmdl(y_gnp_gw_84, p = 4, k = 2, control)
summary(hamilton89_mdl)

# plot smoothed probability of recessionary state
plot(hamilton89_mdl)

This package also provides functions to simulate Markov switching processes among others. To do this, we use the 'simuMSAR' function to simulate a Markov switching process and then uses 'MSARmdl' to estimate the model. Estimated coefficients may be compared with the true parameters used to generate the data. A plot also shows the fit of the smoothed probabilities.

set.seed(123)
# Define DGP of MS AR process
mdl_ms2 <- list(n     = 500, 
                mu    = c(5,10),
                sigma = c(1,2),
                phi   = c(0.5),
                k     = 2,
                P     = rbind(c(0.90, 0.10),
                              c(0.10, 0.90)))

# Simulate process using simuMSAR() function
y_ms_simu <- simuMSAR(mdl_ms2)

# Set options for model estimation
control <- list(msmu   = TRUE, 
                msvar  = TRUE, 
                method = "EM",
                use_diff_init = 10)

# Estimate model
y_ms_mdl <- MSARmdl(y_ms_simu$y, p = 1, k = 2, control)

summary(y_ms_mdl)

plot(y_ms_mdl)

This third example, the 'simuMSVAR' function to simulate a bivariate Markov switching vector autoregressive process and then uses 'MSVARmdl' to estimate the model. Estimated coefficients may be compared with the true parameters used to generate the data. A plot also shows the fit of the smoothed probabilities.

set.seed(1234)
# Define DGP of MS VAR process
mdl_msvar2 <- list(n     = 1000, 
                   p     = 1,
                   q     = 2,
                   mu    = rbind(c(5,-2),
                                 c(10,2)),
                   sigma = list(rbind(c(5.0, 1.5),
                                      c(1.5, 1.0)),
                                rbind(c(7.0, 3.0),
                                      c(3.0, 2.0))),
                   phi   = rbind(c(0.50, 0.30),
                                 c(0.20, 0.70)),
                   k     = 2,
                   P     = rbind(c(0.90, 0.10),
                                 c(0.10, 0.90)))

# Simulate process using simuMSVAR() function
y_msvar_simu <- simuMSVAR(mdl_msvar2)

# Set options for model estimation
control <- list(msmu   = TRUE, 
                msvar  = TRUE,
                method = "EM",
                use_diff_init = 10)
                
# Estimate model
y_msvar_mdl <- MSVARmdl(y_msvar_simu$y, p = 1, k = 2, control)

summary(y_msvar_mdl)

plot(y_msvar_mdl)

Hypothesis Testing

The main contribution of this r package is the hypothesis testing procedures it makes available.

Here we use The LMC-LRT procedure proposed in Rodriguez Rondon & Dufour (2022a; 2022b).

set.seed(123)

# Define DGP of MS AR process
mdl_ms2 <- list(n     = 500, 
                mu    = c(5,10),
                sigma = c(1,2),
                phi   = c(0.5),
                k     = 2,
                P     = rbind(c(0.90, 0.10),
                              c(0.10, 0.90)))

# Simulate process using simuMSAR() function
y_ms_simu <- simuMSAR(mdl_ms2)

# Set test procedure options
lmc_control = list(N = 99,
                  converge_check = NULL,
                   mdl_h0_control = list(const  = TRUE, 
                                         getSE  = TRUE),
                   mdl_h1_control = list(msmu   = TRUE, 
                                         msvar  = TRUE,
                                         getSE  = TRUE,
                                         method = "EM",
                                         maxit  = 500,
                                         use_diff_init = 1))


lmc_lrt <- LMCLRTest(y_ms_simu$y, p = 1 , k0 = 1 , k1 = 2, control = lmc_control)
summary(lmc_lrt)

We can also use the moment-based test procedure proposed by Dufour & Luger (2017)

set.seed(123)

# Set test procedure options
lmc_control = list(N = 99,
                   simdist_N = 10000,
                   getSE = TRUE)

# perform test on Hamilton 1989 data
lmc_mb <- DLMCTest(y_ms_simu$y, p = 1, control = lmc_control)
summary(lmc_mb)

The package also makes available the Maximized Monte Carlo versions of both these tests and the standardized likelihood ratio test proposed by Hansen (1992) (see HLRTest()) and the parameter stability test of Carrasco, Hu, & Ploberger (2014) (see CHPTest()).

References

Carrasco, Marine, Liang Hu, and Werner Ploberger. (2014). Optimal test for Markov switching parameters, Econometrica, 82 (2): 765–784. https://doi.org/10.3982/ECTA8609

Dempster, A. P., N. M. Laird, and D. B. Rubin. (1977). Maximum Likelihood from Incomplete Data via the EM Algorithm, Journal of the Royal Statistical Society, Series B 39 (1): 1–38.https://doi.org/10.1111/j.2517-6161.1977.tb01600.x

Dufour, Jean-Marie, and Richard Luger. (2017). Identification-robust moment-based tests for Markov switching in autoregressive models, Econometric Reviews 36 (6-9): 713–727. https://doi.org/10.1080/07474938.2017.1307548

Kasahara, Hiroyuk, and Katsum Shimotsu. (2018). Testing the number of regimes in Markov regime switching models, arXiv preprint arXiv:1801.06862.

Krolzig, Hans-Martin. (1997). The Markov-Switching Vector Autoregressive Model. In: Markov-Switching Vector Autoregressions. Lecture Notes in Economics and Mathematical Systems, Springer, vol 454. https://doi.org/10.1007/978-3-642-51684-9_2

Hamilton, James D. (1989). A new approach to the economic analysis of nonstationary time series and the business cycle, Econometrica 57 (2): 357–384. https://doi.org/10.2307/1912559

Hamilton, James D. (1994). Time series analysis, Princeton university press. https://doi.org/10.2307/j.ctv14jx6sm

Hansen, Bruce E. (1992). The likelihood ratio test under nonstandard conditions: testing the Markov switching model of GNP, Journal of applied Econometrics 7 (S1): S61–S82. https://doi.org/10.1002/jae.3950070506

Rodriguez-Rondon, Gabriel and Jean-Marie Dufour (2022). Simulation-Based Inference for Markov Switching Models, JSM Proceedings, Business and Economic Statistics Section: American Statistical Association.

Rodriguez-Rondon, Gabriel and Jean-Marie Dufour (2025). Monte Carlo Likelihood Ratio Tests for Markov Switching Models, Manuscript, McGill University Economics Department.

Rodriguez-Rondon, Gabriel and Jean-Marie Dufour. (2024). MSTest: An R-package for Testing Markov-Switching Models, Manuscript, McGill University Economics Department.

Qu, Zhongjun, and Fan Zhuo. (2021). Likelihood Ratio-Based Tests for Markov Regime Switching, The Review of Economic Studies 88 (2): 937–968. https://doi.org/10.1093/restud/rdaa035

Copy Link

Version

Install

install.packages('MSTest')

Monthly Downloads

198

Version

0.1.6

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Gabriel Rodriguez Rondon

Last Published

October 23rd, 2025

Functions in MSTest (0.1.6)

BIC.MSVARmdl

BIC of a MSVARmdl object
BIC.MSARmdl

BIC of a MSARmdl object
DLMMC_bounds

MMC nuisance parameter bounds for Moment-based test
DLMMCpval_fun

Moment-based MMC test p-value
AIC.HMmdl

AIC of a HMmdl object
CHPTest

Carrasco, Hu, and Ploberger (2014) parameter stability test
CHPbootCV

Bootstrap critical values for CHP 2014 parameter stability test
DLMMCpval_fun_min

Moment-based MMC test (negative) p-value
EMaximization_HMmdl

Maximization step of EM algorithm for Hidden Markov model
EMiter_MSARXmdl

EM algorithm iteration for Markov-switching ARX model
EMiter_HMmdl

EM algorithm iteration for Hidden Markov model
DLMMCTest

Maximized Monte Carlo moment-based test for Markov switching model
DLMCTest

Monte Carlo moment-based test for Markov switching model
ExpectationM_MSVARXmdl

Markov-switching VARX log-likelihood function
ExpectationM_MSVARmdl

Markov-switching vector autoregressive log-likelihood function
HLRTest

Hansen (1992) likelihood ratio test
EMaximization_MSVARmdl

Maximization step of EM algorithm for Markov-switching vector autoregressive model
BIC.VARmdl

BIC of a VARmdl object
BIC.Nmdl

BIC of a Nmdl object
HLRparamSearch

HLR param search
EMaximization_MSVARXmdl

Maximization step of EM algorithm for Markov-switching VARX model
AIC.VARmdl

AIC of a VARmdl object
HMmdl

Hidden Markov model
EMiter_MSVARmdl

EM algorithm iteration for Markov-switching vector autoregressive model
ExpectationM_HMmdl

Hidden Markov model log-likelihood function
LR_samp_dist

Likelihood Ratio Test Statistic Sample Distribution
HMmdl_em

Estimation of Hidden Markov model by EM Algorithm
EMiter_MSARmdl

EM algorithm iteration for Markov-switching autoregressive model
MMCLRpval_fun

Monte Carlo Likelihood Ratio Test P-value Function
MMCLRpval_fun_min

Monte Carlo Likelihood Ratio Test P-value Function
ExpectationM_MSARmdl

Markov-switching autoregressive log-likelihood function
EMiter_MSVARXmdl

EM algorithm iteration for Markov-switching VARX model
ExpectationM_MSARXmdl

Markov-switching ARX log-likelihood function
EMaximization_MSARmdl

Maximization step of EM algorithm for Markov-switching autoregressive model
EMaximization_MSARXmdl

Maximization step of EM algorithm for Markov-switching ARX model
MSVARXmdl_em

Estimation of Markov-switching VARX model by EM Algorithm
MSVARmdl

Markov-switching vector autoregressive model
LMCLRTest

Monte Carlo Likelihood Ratio Test
MSARmdl

Markov-switching autoregressive model
MSARXmdl_em

Estimation of Markov-switching ARX model by EM Algorithm
HMmdl_mle

Hidden Markov model maximum likelihood estimation
MCpval

Monte Carlo P-value
argrid_MSVARmdl

Vector autoregressive moment grid
argrid_MSARmdl

Autoregressive moment grid
MSARXmdl

Markov-switching autoregressive model
VARmdl

Vector autoregressive model
MSVARmdl_mle

Markov-switching vector autoregressive maximum likelihood estimation
MMC_bounds

MMC nuisance parameter bounds
MSVARmdl_em

Estimation of Markov-switching vector autoregressive model by EM Algorithm
calcResid_MSARXmdl

Markov-switching autoregressive model residuals
USRGDP

US Real GDP data 1947Q2 - 2024Q2
chpStat

Test statistic for CHP 2014 parameter stability test
calcResid_MSARmdl

Markov-switching autoregressive model residuals
chpDmat

Derivative matrix
clike

Parameter vector & likelihood function used by HLRTest()
approxDistDL

Approximate CDF distribution
MSVARXmdl

Markov-switching vector autoregressive model
VARXmdl

Vector X autoregressive model
calcResid_MSVARXmdl

Markov-switching VARX model residuals
calc_DLmoments

Moment-based test statistics
MSTest-package

Testing Markov Switching Models
Nmdl

Normal distribution model
MMCLRTest

Maximized Monte Carlo Likelihood Ratio Test
getHessian.MSVARmdl

Hessian matrix of Markov-switching vector autoregressive model
calcResid_MSVARmdl

Markov-switching vector autoregressive model residuals
fitted.ARmdl

fitted values of a ARmdl object
fitted.MSVARmdl

fitted values of a MSVARmdl object
coef.VARmdl

coef of a VARmdl object
coef.ARmdl

coef of a ARmdl object
estimMdl

Estimate model for likelihood ratio test
USGNP

US GNP data 1947Q2 - 2024Q2
calc_mu2t

Test statistic for switch in mean only
fitted.Nmdl

fitted values of a Nmdl object
getHessian.HMmdl

Hessian matrix of Hidden Markov model
getHessian.Nmdl

Hessian matrix of normal model
coef.HMmdl

coef of a HMmdl object
logLik.VARmdl

Log likelihood for vector autoregressive model
logLik.HMmdl

Log likelihood for Hidden Markov model
logLike_ARmdl

Autoregressive log-likelihood objective function
getHessian.MSARmdl

Hessian matrix of Markov-switching autoregressive model
compu_tstat

Computes test stat using new parameter vectors
getHessian

Hessian matrix
logLik.ARmdl

Log likelihood for autoregressive model
limP

Ergodic (limiting) probabilities of states
companionMat

Companion Matrix
fitted.VARmdl

fitted values of a VARmdl object
logLike_VARmdl

Vector autoregressive log-likelihood objective function
coef.MSARmdl

coef of a MSARmdl object
logLike_VARXmdl

VARX log-likelihood objective function
fitted.MSARmdl

fitted values of a MSARmdl object
logLik.MSARmdl

Log likelihood for Markov-switching autoregressive model
getHessian.VARmdl

Hessian matrix of vector autoregressive model
logLike_ARXmdl

ARX log-likelihood objective function
getHessian.ARmdl

Hessian matrix of autoregressive model
LR_samp_dist_par

Monte Carlo Likelihood Ratio Test sample distribution (parallel version)
fitted.HMmdl

fitted values of a HMmdl object
initVals_MSVARXmdl

Initial values for Markov-switching VARX model
initVals_MSVARmdl

Initial values for Markov-switching vector autoregressive model
logLike_MSVARmdl

Markov-switching vector autoregressive log-likelihood objective function
logLike_MSVARXmdl_min

Markov-switching VARX log-likelihood objective function (minimization version)
logLike_HMmdl

Hidden Markov model log-likelihood function
covar_vech

Covariance vech function
hamilton84GNP

Hamilton 1984 & Hansen 1992 GNP data
dmclike

Gradient of likelihood function.
nobs.HMmdl

Nobs of a HMmdl object
combine_stat

Combine p-values
logLike_HMmdl_min

Hidden Markov model log-likelihood function (minimization version)
MSARmdl_mle

Markov-switching autoregressive maximum likelihood estimation
arP

Autoregressive transition matrix
approx_dist_loop

Loop for approxDistDL
calc_mu2t_mv

Test statistic for switch in mean and variance
chp10GNP

Carrasco, Hu, & Ploberger 2010 GNP data
MSARmdl_em

Estimation of Markov-switching autoregressive model by EM Algorithm
nobs.MSARmdl

Nobs of a MSARmdl object
marklike

Likelihood function used by HLRTest()
logLike_MSARXmdl

Markov-switching ARX log-likelihood objective function
plot.simuVARX

Plot of a simuVARX object
coef.MSVARmdl

coef of a MSVARmdl object
cov2corr

Covariance to correlation matrix
coef.Nmdl

coef of a Nmdl object
plot.MSARmdl

Plot of a MSARmdl object
plot.Hmmdl

Plot of a HMmdl object
nobs.VARmdl

Nobs of a VARmdl object
covar_unvech

Covariance vech to matrix
interMSVARmdl

Intercept from mu for MSVARmdl
interMSARmdl

Intercept from mu for MSARmdl
logLike_MSARXmdl_min

Markov-switching ARX log-likelihood objective function (minimization version)
logLike_MSARmdl

Markov-switching autoregressive log-likelihood objective function
mdledit

Change model List with new parameters
nobs.MSVARmdl

Nobs of a MSVARmdl object
mclike

Sum of likelihood used by HLRTest()
nobs.ARmdl

Nobs of a ARmdl object
plot.simuHMM

Plot of a simuHMM object
plot.simuARX

Plot of a simuARX object
print.HLRTest

Print summary of a CHPTest object
predict.ARmdl

Predict for a ARmdl object
predict.MSVARmdl

Predict for a MSVARmdl object
initVals_HMmdl

Initial values for Hidden Markov model
logLike_MSVARmdl_min

Markov-switching vector autoregressive log-likelihood objective function (minimization version)
logLike_Nmdl

Normal log-likelihood objective function
nobs.Nmdl

Nobs of a Nmdl object
paramList_MSARmdl

Parameter list for Markov-switching autoregressive model
logLik.Nmdl

Log likelihood for Normal model
initVals_MSARXmdl

Initial values for Markov-switching ARX model
predict.Nmdl

Predict for a Nmdl object
initVals_MSARmdl

Initial values for Markov-switching autoregressive model
plot.simuAR

Plot of a simuAR object
logLik.MSVARmdl

Log likelihood for Markov-switching vector autoregressive model
plot.VARmdl

Plot of a VARmdl object
plot.simuNorm

Plot of a simuNorm object
paramList_MSVARmdl

Parameter list for Markov-switching vector autoregressive model
paramList_MSARXmdl

Parameter list for Markov-switching ARX model
paramList_MSVARXmdl

Parameter list for Markov-switching VARX model
plot.ARmdl

Plot of a ARmdl object
plot.MSVARmdl

Plot of a MSVARmdl object
plot.simuVAR

Plot of a simuVAR object
residuals.ARmdl

residuals of a ARmdl object
print.BootLRTest

Print summary of a BootLRTest object
logLike_MSVARXmdl

Markov-switching VARX log-likelihood objective function
plot.simuMSARX

Plot of a simuMSARX object
logLike_MSARmdl_min

Markov-switching autoregressive log-likelihood objective function (minimization version)
print.CHPTest

Print summary of a CHPTest object
print.MSARmdl

Print summary of a MSARmdl object
print.DLMCTest

Print summary of a DLMCTest object
plot.simuMSAR

Plot of a simuMSAR object
sim_DLmoments

Simulated moment-based test statistics
plot.Nmdl

Plot of a Nmdl object
print.MSVARmdl

Print summary of a MSVARmdl object
plot.simuMSVAR

Plot of a simuMSVAR object
predict.HMmdl

Predict for a HMmdl object
predict.MSARmdl

Predict for a MSARmdl object
plot.simuMSVARX

Plot of a simuMSVARX object
print.ARmdl

Print summary of an ARmdl object
print.Nmdl

Print summary of a Nmdl object
randP

Random Transition Matrix
predict.VARmdl

Predict for a VARmdl object
randSN

Standard normal errors using box Muller
print.HMmdl

Print summary of a HMmdl object
simuARX

Simulate autoregressive X process
simuHMM

Simulate Hidden Markov model with normally distributed errors
simuMSVAR_cpp

Simulate Markov-switching vector autoregressive process
simuARX_cpp

Simulate autoregressive process with exogenous regressors
simuAR_cpp

Simulate autoregressive process
simuMSVAR

Simulate Markov-switching vector autoregressive process
summary.BootLRTest

Summary of a BootLRTest object
summary.ARmdl

Summary of an ARmdl object
simuMSAR_cpp

Simulate Markov-switching autoregressive process
summary.MSARmdl

Summary of a MSARmdl object
simuMdl

Likelihood ratio test statistic sample distribution
summary.MMCLRTest

Summary of a MMCLRTest object
simuVARX_cpp

Simulate VARX process
simuMSARX

Simulate Markov-switching ARX process
simuAR

Simulate autoregressive process
residuals.Nmdl

residuals of a Nmdl object
print.VARmdl

Print summary of an VARmdl object
print.LMCLRTest

Print summary of a LMCLRTest object
print.DLMMCTest

Print summary of a DLMMCTest object
summary.HMmdl

Summary of a HMmdl object
summary.LMCLRTest

Summary of a LMCLRTest object
print.MMCLRTest

Print summary of a MMCLRTest object
residuals.MSVARmdl

residuals of a MSVARmdl object
simuVAR_cpp

Simulate VAR process
summary.DLMCTest

summaryummary of a DLMCTest object
residuals.MSARmdl

residuals of a MSARmdl object
simuMSVARX

Simulate Markov-switching VARX process
residuals.HMmdl

residuals of a HMmdl object
summary.CHPTest

Summary of a CHPTest object
simuMSVARX_cpp

Simulate Markov-switching VARX process
simuHMM_cpp

Simulate Hidden Markov model with normally distributed errors
residuals.VARmdl

residuals of a VARmdl object
simuNorm

Simulate normally distributed process
simuMSARX_cpp

Simulate Markov-switching ARX process
simuNorm_cpp

Simulate normally distributed process
summary.VARmdl

Summary of an VARmdl object
thetaSE

Theta standard errors
simuMSAR

Simulate Markov-switching autoregressive process
simuVAR

Simulate VAR process
simuVARX

Simulate VAR process
ts_lagged

Lagged Time Series Data
summary.HLRTest

Summary of a CHPTest object
summary.DLMMCTest

Summary of a DLMMCTest object
summary.Nmdl

Summary of a Nmdl object
summary.MSVARmdl

Summary of a MSVARmdl object
ARmdl

Autoregressive Model
AIC.Nmdl

AIC of a Nmdl object
ARXmdl

Autoregressive X Model
AIC.ARmdl

AIC of a ARmdl object
AIC.MSARmdl

AIC of a MSARmdl object
BIC.HMmdl

BIC of a HMmdl object
BIC.ARmdl

BIC of a ARmdl object
AIC.MSVARmdl

AIC of a MSVARmdl object