Installing and Loading MSTest R package

Install Latest Version of MSTest R Package

The follwoing block of code can be used to download and install the latest version of the MSTest R package from its github repository (https://github.com/roga11/MSTest). Installing packages directly from github requires using (devtools). The code below will check if devtools is already installed and install it if it is not.

The package does not need to be re-installed every time, just when new updates are avaiable. If the latest version is already installed, proceed to the next section.

rm(list=ls())
if (is.element('devtools',installed.packages()[,1])==FALSE){
  install.packages('devtools')
}
devtools::install_github('roga11/MSTest')

Load Package

Once package has been installed it can be loaded.

library(MSTest)

Data

Load Data

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

They can be loaded using the following code.

GNPdata <- hamilton84GNP # this can be hamilton82GNP, carhuplo10GNP or kasshi14GDP
Y       <- GNPdata$GNP_logdiff
date    <- GNPdata$DATE 
plot(as.Date(date),Y,xlab='Time',ylab='GNP - log difference',type = 'l')

You an also learn more about these dataset and their sources by using the following line(s) and reading its description in the help tab.

?hamilton84GNP

Simulated Data

The function (simuARMS) can be used to simulate an Auto-Regressive Markov-Switching process. It requires the user to specify the sample size (T), the mean in each regime (\mu_1,\mu_2), the standard deviation in each regime (\sigma_1,\sigma_2), the limiting probabilities of being in each regime (p) and (q) and the auto-regressive coefficients (\phi_i). The parameters should be put together in a single vector in the follwoing order: (\theta = [\mu_i,p,q,\sigma_i,\phi_i]). The function itself takes the sample size (T) and the vector (\theta) as inputs. The following code shows and example of simulating an AR MS process and using the function (ARMSmdl) to estimating a MS model using this process.

n = 500
mu = c(0,2)
std = c(1,2.5)
p = 0.9
q = 0.5
phi = c(0.5,0.25)
th = c(mu,p,q,std,phi)
Y_simu = simuARMS(n,th)
plot(Y_simu, type = 'l')


arms_mdl = ARMSmdl(Y_simu,ar = 2,k=2)
print("coefficients:")
arms_mdl$coef
print("mu:")
arms_mdl$coef[,1]/(1-rowSums(arms_mdl$coef[,2:ncol(arms_mdl$coef)]))
print("standard dev.:")
arms_mdl$stdev

The function (simuAR) can be used to simulate an AR process. the inputs are sample size (T), mean (\mu), standard deviation (\sigma) and vector of autogressive coefficients (\phi_i). The following code shows and example of simulating an AR process and using the function (ARmdl) to estimating an AR model using this process.

Y_simu = simuAR(n, mu = 5, std = 1, phi = c(0.5,0.25))
plot(Y_simu, type = 'l')

ar_mdl = ARmdl(Y_simu,ar=2)

print("coefficients:")
ar_mdl$coef
print("mu:")
ar_mdl$coef[1]/(1-sum(ar_mdl$coef[2:length(ar_mdl$coef)]))
print("standard dev.:")
ar_mdl$stdev

Hypothesis Testing

Moment-Based Test

This test is developed by Dufour & Luger (2017)

Optional Arguments are:

Local Monte-Carlo moment-based test.

This takes a few seconds only.

start <- Sys.time()
DLMCtest(Y, p = 4)
end   <- Sys.time()
print(end - start)

Use the following command to learn more from the help tab.

?DLMCtest

Maximized Monte-Carlo moment-based test.

In addition to parameters above, available optimization methods can be set using 'searchType'. Allowed options are:

using (searchType = 'randSearch_paramCI') produces results coparable to Table 5 of Dufour & Luger (2017) if hamilton82GNP or carhuplo10GNP datasets are used. For this reason, (searchType = 'randSearch_paramCI') is the default. When using (GenSa), (GA) or (PSO), optimization is performed over entire ar coefficient parameter space (i.e. [-1,1]), but it is constrained to only coefficients for a stationary process.

start <- Sys.time()
DLMMCtest(Y, p = 4, searchType = 'randSearch_paramCI')
end   <- Sys.time()
print(end - start)

start <- Sys.time()
DLMMCtest(Y, p = 4, searchType = 'GenSA')
end   <- Sys.time()
print(end - start)

Use the following command to learn more from the help tab.

?DLMMCtest

Optimal Test for Markov Switching Parameters

This test is developed by Carrasco Hu Ploberger (2014).

Optional Arguments are:

Main CHP test

This version of the test requires performing a parametric bootstrap to get empirical critical values of the test and can thus take some time. To get results that are similar to those in the CHP (2014) paper set (N = 3000). This should currently take about 2 hours when considering a switch in variance. It is much quicker when only considering switch in mean. For convinience, the example below has (var_switch = 0) and only takes a few (~(30) seconds for (N =3000)) seconds.

start <- Sys.time()
chp <- CHPtest(Y, p = 4, N = 3000, var_switch = 0)
chp
end   <- Sys.time()
print(end - start)

Use the following command to learn more from the help tab.

?CHPtest

Monte-Carlo version of CHP Test

This method makes use of asymptotic distribution derived by CHP (2014) of the test under the null and is therefore much faster and takes only a few seconds regadless of the number of iterations (N).

start <- Sys.time()
CHPMCtest(Y, p = 4, N = 3000)  
end   <- Sys.time()
print(end - start)

Use the following command to learn more from the help tab.

?CHPMCtest

Likelihood-Ratio Test

Hensen Test

This test was developed by Hansen (1992; 1996).

Optional Arguments are:

This takes about 15-20 mins.

start <- Sys.time()
HLRtest(Y,p = 4, iv = 1)
end   <- Sys.time()
print(end - start)

LR Parametric Bootstrap Test

Parametric bootstrap of Likelihood-Ratio Test.

Optional Arguments are:

This takes only a few seconds

start <- Sys.time()
LR_test <-  LRBoot_ARMS(Y, ar = 4, k = 2, N = 100)
print(paste0('p-value: ',toString(round(LR_test$pval,2))))
print('coefficients')
LR_test$msmdl_h1$coef
print(paste0('standard deviation: ',toString(round(LR_test$msmdl_h1$stdev,4))))
end   <- Sys.time()
print(end - start)


roga11/MSTest_v1 documentation built on Dec. 22, 2021, 5:16 p.m.