Title: | Fitting Markov Switching Models |
---|---|
Description: | Estimation, inference and diagnostics for Univariate Autoregressive Markov Switching Models for Linear and Generalized Models. Distributions for the series include gaussian, Poisson, binomial and gamma cases. The EM algorithm is used for estimation (see Perlin (2012) <doi:10.2139/ssrn.1714016>). |
Authors: | Josep A. Sanchez-Espigares, Alberto Lopez-Moreno |
Maintainer: | Josep A. Sanchez-Espigares <[email protected]> |
License: | GPL (>= 2.0) |
Version: | 1.5 |
Built: | 2025-01-19 03:17:15 UTC |
Source: | https://github.com/cran/MSwM |
Univariate Autoregressive Markov Switching Models for Linear and Generalized Models by using the EM algorithm.
Package: | MSwM |
Type: | Package |
Version: | 1.0 |
Date: | 2012-11-13 |
License: | GPL (>=2.0) |
LazyLoad: | yes |
Depends: | methods, nlme, graphics, parallel |
Josep Anton Sanchez Espigares, Alberto Lopez-Moreno
Maintainer: Josep Anton Sanchez Espigares <[email protected]>
Hamilton J.D. (1989). A New Approach to the Economic Analysis of Nonstionary Time Series and the Business Cycle. Econometrica 57: 357-384
Hamilton, J.D. (1994). Time Series Analysis. Princeton University Press.
Goldfeld, S., Quantd, R. (2005). 'A Markov model for switching Regression',Journal of Econometrics 135, 349-376.
Perlin, M. (2007). 'Estimation, Simulation and Forecasting of a Markov Switching Regression', (General case in Matlab).
Overview: MSwM-package
Classes : MSM.lm
, MSM.glm
, MSM.fitted
Methods : msmFit
,summary
,AIC
,intervals
,msmResid
Plot : plot
,plotProb
,plotReg
,plotDiag
Calculates the Akaike information criterion for one Markov Switching model.
AIC(object, ..., k = 2)
AIC(object, ..., k = 2)
object |
an object of class "MSM.lm" or "MSM.glm". |
k |
an optional numeric value for the penalty per parameter to be used. The default |
... |
currently not used. |
Returns a numeric value with the corresponding AIC (or BIC, or ..., depending on k).
Jose A. Sanchez-Espigares, Alberto Lopez-Moreno
Overview: MSwM-package
Classes : MSM.lm
, MSM.glm
, MSM.fitted
Methods : msmFit
,summary
,AIC
,intervals
,msmResid
Plot : plot
,plotProb
,plotReg
,plotDiag
Price of the energy in Spain with other economic data. Data from January 1, 2002 to October 31, 2008 (daily data, working days: Monday to Friday)
data(energy)
data(energy)
A data frame with 1785 observations on the following 7 variables.
Price
Average price of energy (Cent/kwh)
Oil
Oil price (Euro/barril)
Gas
Gas price (Euro/MWh)
Coal
Coal price (Euro/T)
EurDol
Exchange rate between Dolar-Euro (USD-Euro)
Ibex35
Ibex 35 index divided by one thousand
Demand
Daily demand of energy (GWh)
The data were obtained from the Spanish Market Operator of Energy (OMEL), the Bank of Spain and the U.S. Energy Information Administrationa
S. Fontdecaba, M. P. Munyoz , J. A. Sanchez (2009). Estimating Markovian Switching Regression Models in R. An application to model energy price in Spain. The R UseR Conference 2009. Rennes, France.
The example
data is a generated data set to show how msmFit
can detect the presence of two regimes in a data with autocorrelative periods and other periods that are correlated with the covariate x
.
data(example)
data(example)
A data frame with 300 observations on the following 2 variables.
x
an uniform distribution with min=0 and max=1.
y
a variable generated through two models. For further information, see the vignette 'example' on this package.
data(example) plot(x~y,data=example) ##See the vignette #vignette("examples")
data(example) plot(x~y,data=example) ##See the vignette #vignette("examples")
Confidence intervals are obtained for the parameters associated with the model represented by the object.
intervals(object, level = 0.95, ...)
intervals(object, level = 0.95, ...)
object |
a fitted Markov Switching model object from which parameter estimates can be extracted. |
level |
an optional numeric value for the interval confidence level. Defaults to 0.95. |
... |
currently not used. |
Print the coefficients with their intervals for each regime.
Jose A. Sanchez-Espigares, Alberto Lopez-Moreno
Overview: MSwM-package
Classes : MSM.lm
, MSM.glm
, MSM.fitted
Methods : msmFit
,summary
,AIC
,intervals
,msmResid
Plot : plot
,plotProb
,plotReg
,plotDiag
MSM.fitted
contains the values of fitting the EM algorithm for Markov Switching Models. It is an internal class and is stored in objects of class MSM.lm
and MSM.glm
CondMean
:Object of class "matrix"
, contains the conditional means for each state.
error
:Object of class "matrix"
, are the conditional residuals of the model for each state.
Likel
:Object of class "matrix"
, contains de likelihood of the parameters for each state.
margLik
:Object of class "matrix"
, contains the marginal likelihood for each observation.
filtProb
:Object of class "matrix"
, contains the filter probabilities for each state.
smoProb
:Object of class "matrix"
, contains the smoothed probabilities for each state.
smoTransMat
:Object of class "list"
, contains the smoothed transition probabilities in a "matrix"
for each observation between all the states.
logLikel
:Object of class "numeric"
, contains the global loglikelihood of the model.
signature(x = "MSM.fitted", i = "character", j = "missing", drop = "missing")
: extract the componentes of the model.
Jose A. Sanchez-Espigares, Alberto Lopez-Moreno
Overview: MSwM-package
Classes : MSM.lm
, MSM.glm
, MSM.fitted
Methods : msmFit
,summary
,AIC
,intervals
,msmResid
Plot : plot
,plotProb
,plotReg
,plotDiag
MSM.glm
is an object containing Markov Switching model information for general linear models.
msmFit
is an algorithm that builds a MSM.glm
when the original model class is glm
.
family
:Object of class "ANY"
, contains the family of the object glm
.
Likelihood
:Object of class "function"
, is the function that allows calculation of the conditional density of the response.
model
:Object of class "glm"
, contains the original model glm
.
Coef
:Object of class "data.frame"
, contains the coefficientes of the model MSM where each row indicates the state.
seCoef
:Object of class "data.frame"
, contains the standard errors of the coefficients.
transMat
:Object of class "matrix"
, contains the transition probabilities of the states.
iniProb
:Object of class "numeric"
, contains initial values of the parameters.
call
:Object of class "call"
, contains the object call
which msmFit
has been executed.
k
:Object of class "numeric"
, the numbers of states that the model has.
switch
:Object of class "logical"
, contains which coefficients have switching.
Fit
:Object of class "MSM.fitted"
, contains the values obtained for fitting an MSM model with EM algorithm.
Class "MSM.linear"
, directly.
Class "MSM"
, by class "MSM.linear", distance 2.
signature(x = "MSM.glm", i = "character", j = "missing", drop = "missing")
: extract the componentes of the model.
Jose A. Sanchez-Espigares, Alberto Lopez-Moreno
Overview: MSwM-package
Classes : MSM.lm
, MSM.glm
, MSM.fitted
Methods : msmFit
,summary
,AIC
,intervals
,msmResid
Plot : plot
,plotProb
,plotReg
,plotDiag
MSM.lm
is an object containing Markov Switching model information for linear models.
msmFit
is an algorithm that builds a MSM.lm
when the original model class is lm
.
std
:Object of class "numeric"
, contains the standard deviation for each state.
model
:Object of class "glm"
, contains the original model glm
Coef
:Object of class "data.frame"
, contains the coefficientes of the model MSM, where each row indicates the state.
seCoef
:Object of class "data.frame"
, contains the standard errors of the coefficients.
transMat
:Object of class "matrix"
, contains the transition probabilities of the states.
iniProb
:Object of class "numeric"
, contains initial values of the parameters.
call
:Object of class "call"
, contains the object call
which msmFit
has been executed.
k
:Object of class "numeric"
, the numbers of states that the model has.
switch
:Object of class "logical"
, contains which coefficients have switching.
Fit
:Object of class "MSM.fitted"
, contains the values obtained for fitting a MSM model with EM algorithm.
Class "MSM.linear"
, directly.
Class "MSM"
, by class "MSM.linear", distance 2.
signature(x = "MSM.lm", i = "character", j = "missing", drop = "missing")
: extract the componentes of the model.
Jose A. Sanchez-Espigares, Alberto Lopez-Moreno
Overview: MSwM-package
Classes : MSM.lm
, MSM.glm
, MSM.fitted
Methods : msmFit
,summary
,AIC
,intervals
,msmResid
Plot : plot
,plotProb
,plotReg
,plotDiag
msmFit
is an implementation for modeling Markov Switching Models using the EM algorithm
msmFit(object, k, sw, p, data, family, control)
msmFit(object, k, sw, p, data, family, control)
object |
an object of class "lm" or "glm", or "formula" with a symbolic description of the model to be fitted. |
k |
numeric, the estimated number of regimes that the model has. |
sw |
a logical vector indicatig which coefficients have switching. |
p |
integer, the number of AR coefficients that the MS model has to have. The default value is zero. If |
data |
an optional data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model. If not found in data, the variables are taken from environment(formula), typically the environment from which "glm" is called. |
family |
a character value indicating what family belongs to the model. It is only required when the object is a "General linear formula". |
control |
a list of control parameters. See "Details". |
The control
argument is a list that can supply any of the following components:
-trace
: A logical value. If it is TRUE, tracing information on the progress of the optimization is produced.
-maxiter
: The maximum number of iterations in the EM method. Default is 100.
-tol
: Tolerance. The algorithm stops if it is unable to reduce the value by a factor of tol
at a step. Default is 1e-8.
-maxiterOuter
: The number of short runs of the EM method to stablish the initial values. Default is 5
-maxiterInner
: The number of iterations in the EM method in each short run to stablish the initial values. Default is 10
-parallelization
: A logical value. Whether the process is done by using parallelization or not. Default is TRUE.
msm.fit
returns an object of class MSM.lm
or MSM.glm
, depending on the input model.
Jose A. Sanchez-Espigares, Alberto Lopez-Moreno
Hamilton J.D. (1989). A New Approach to the Economic Analysis of Nonstionary Time Series and the Business Cycle. Econometrica 57: 357-384
Hamilton, J.D. (1994). Time Series Analysis. Princeton University Press.
Goldfeld, S., Quantd, R. (2005). 'A Markov model for switching Regression',Journal of Econometrics 135, 349-376.
Perlin, M. (2007). 'Estimation, Simulation and Forecasting of a Markov Switching Regression', (General case in Matlab).
Overview: MSwM-package
Classes : MSM.lm
, MSM.glm
, MSM.fitted
Methods : msmFit
,summary
,AIC
,intervals
,msmResid
Plot : plot
,plotProb
,plotReg
,plotDiag
## Not run ## data(energy) ## model=lm(Price~Oil+Gas+Coal+EurDol+Ibex35+Demand,energy) ## mod=msmFit(model,k=2,sw=rep(TRUE,8)) ## summary(mod) ## End(Not run)
## Not run ## data(energy) ## model=lm(Price~Oil+Gas+Coal+EurDol+Ibex35+Demand,energy) ## mod=msmFit(model,k=2,sw=rep(TRUE,8)) ## summary(mod) ## End(Not run)
msmResid
is a function which extracts model residuals from objects returned by Markov Switching modeling functions.
msmResid(object, regime)
msmResid(object, regime)
object |
an object of class "MSM.lm" or "MSM.glm". |
regime |
a number or a vector indicating which regimes are selected to get its residuals. If is missing, the function calculates the conditional residuals of the model. |
A numeric vector with the pooled residuals or a matrix, when more than one regime is indicated, containing the residuals for each observation. When the attributed object is a glm, it returns the Pearson residuals.
Jose A. Sanchez-Espigares, Alberto Lopez-Moreno
Overview: MSwM-package
Classes : MSM.lm
, MSM.glm
, MSM.fitted
Methods : msmFit
,summary
,AIC
,intervals
,msmResid
Plot : plot
,plotProb
,plotReg
,plotDiag
Plot that shows the rediduals for each regime with the conditional residuals.
plot(x, y, ...)
plot(x, y, ...)
x |
an object of class "MSM.lm" or "MSM.glm". |
y |
currently not used. |
... |
currently not used. |
Jose A. Sanchez-Espigares, Alberto Lopez-Moreno
Overview: MSwM-package
Classes : MSM.lm
, MSM.glm
, MSM.fitted
Methods : msmFit
,summary
,AIC
,intervals
,msmResid
Plot : plot
,plotProb
,plotReg
,plotDiag
Creates several plots for the residual analysis. It shows a plot of residuals against fitted values, a Normal Q-Q plot, ACF/PACF of residuals and ACF/PACF of square residuals. Depending on the selection, it shows the pooled residuals or the residuals for each regime.
plotDiag(x, regime, which)
plotDiag(x, regime, which)
x |
an object of class "MSM.lm" or "MSM.glm". |
regime |
a single numeric value or vector indicating the regimes to show its residuals. In order to show all the regimes, it could be written
|
which |
if a subset of the plots is required, specify a subset of the numbers 1:3. The default value is |
The argument which
has three values:
- 1
: represents the plot of residuals against fitted values.
- 2
: represents the Normal Q-Q plot.
- 3
: represents the ACF/PACF of residuals and ACF/PACF of square residuals.
Jose A. Sanchez-Espigares, Alberto Lopez-Moreno
Overview: MSwM-package
Classes : MSM.lm
, MSM.glm
, MSM.fitted
Methods : msmFit
,summary
,AIC
,intervals
,msmResid
Plot : plot
,plotProb
,plotReg
,plotDiag
Creates one plot (or more, depending on the number of states) that contains, for each regime, its smoothed and filtered probabilities. Finally, it shows a plot for each regime with the response variable versus the smoothed probabilities, showing the periods where the variable is in that regime.
plotProb(x, which)
plotProb(x, which)
x |
an object of class "MSM.lm" or "MSM.glm". |
which |
if a subset of the plots is required, specify a subset of the numbers 1:(number of regimes +1). See details for more information. |
The argument which
has options:
-1
: represents the plots that contains, for each regime, its smoothed and filtered probabilities..
-2:(number of regimes +1)
: represents plot of the regime minus one with the response variable against the smoothed probabilities.
Jose A. Sanchez-Espigares, Alberto Lopez-Moreno
Overview: MSwM-package
Classes : MSM.lm
, MSM.glm
, MSM.fitted
Methods : msmFit
,summary
,AIC
,intervals
,msmResid
Plot : plot
,plotProb
,plotReg
,plotDiag
Creates a plot with the response and the explanatory variables with the smoothed probabilities of a specific regime.
plotReg(x, expl, regime = 1)
plotReg(x, expl, regime = 1)
x |
an object of class "MSM.lm" or "MSM.glm". |
expl |
a character vector containing the names of the covariates of the model to show. If it is missing, all the variables are plotted. |
regime |
a numeric value indicating which regime has to be shown. By defect the value is 1. |
Jose A. Sanchez-Espigares, Alberto Lopez-Moreno
Overview: MSwM-package
Classes : MSM.lm
, MSM.glm
, MSM.fitted
Methods : msmFit
,summary
,AIC
,intervals
,msmResid
Plot : plot
,plotProb
,plotReg
,plotDiag
summary
produces results summaries of the results of fitting Markov Switching Models.
## S4 method for signature 'MSM.lm' summary(object) ## S4 method for signature 'MSM.glm' summary(object)
## S4 method for signature 'MSM.lm' summary(object) ## S4 method for signature 'MSM.glm' summary(object)
object |
an object of class "MSM.lm" or "MSM.glm". |
Jose A. Sanchez-Espigares, Alberto Lopez-Moreno
Overview: MSwM-package
Classes : MSM.lm
, MSM.glm
, MSM.fitted
Methods : msmFit
,summary
,AIC
,intervals
,msmResid
Plot : plot
,plotProb
,plotReg
,plotDiag
The traffic
data contains the daily number of deaths in traffic accidents in Spain during the year 2010, the average daily temperature and
the daily sum of precipitations.
data(traffic)
data(traffic)
A data frame with 210 observations on the following 2 variables.
Date
The date of each observation in format DD/MM/YYYY.
NDead
The number of daily traffic deaths.
Temp
The daily mean temperature in Spain (Celsius).
Prec
The daily mean precipitation in Spain (l/m2).
The data were obtained from the General Directorate of Traffic (Direccion General de Trafico) and the State Meteorological Agency of Spain (Agencia Estatal de Meterorologia de Espanya).
data=data(traffic) ts.plot(traffic$NDead) ##See the vignette #vignette("examples")
data=data(traffic) ts.plot(traffic$NDead) ##See the vignette #vignette("examples")