Package 'MSwM'

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

Help Index


MSwM package

Description

Univariate Autoregressive Markov Switching Models for Linear and Generalized Models by using the EM algorithm.

Details

Package: MSwM
Type: Package
Version: 1.0
Date: 2012-11-13
License: GPL (>=2.0)
LazyLoad: yes
Depends: methods, nlme, graphics, parallel

Author(s)

Josep Anton Sanchez Espigares, Alberto Lopez-Moreno

Maintainer: Josep Anton Sanchez Espigares <[email protected]>

References

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).

See Also

Overview: MSwM-package
Classes : MSM.lm, MSM.glm, MSM.fitted
Methods : msmFit,summary,AIC,intervals,msmResid
Plot : plot,plotProb,plotReg,plotDiag


Akaike Information Criterion for Markov Switching models.

Description

Calculates the Akaike information criterion for one Markov Switching model.

Usage

AIC(object, ..., k = 2)

Arguments

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 k=2 is the classical AIC.

...

currently not used.

Value

Returns a numeric value with the corresponding AIC (or BIC, or ..., depending on k).

Author(s)

Jose A. Sanchez-Espigares, Alberto Lopez-Moreno

See Also

Overview: MSwM-package
Classes : MSM.lm, MSM.glm, MSM.fitted
Methods : msmFit,summary,AIC,intervals,msmResid
Plot : plot,plotProb,plotReg,plotDiag


Price of energy in Spain

Description

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)

Usage

data(energy)

Format

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)

Source

The data were obtained from the Spanish Market Operator of Energy (OMEL), the Bank of Spain and the U.S. Energy Information Administrationa

References

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.


Example data generated

Description

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.

Usage

data(example)

Format

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.

Examples

data(example)
plot(x~y,data=example)

##See the vignette
#vignette("examples")

Function: Confidence Intervals on Coefficients

Description

Confidence intervals are obtained for the parameters associated with the model represented by the object.

Usage

intervals(object, level = 0.95, ...)

Arguments

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.

Value

Print the coefficients with their intervals for each regime.

Author(s)

Jose A. Sanchez-Espigares, Alberto Lopez-Moreno

See Also

Overview: MSwM-package
Classes : MSM.lm, MSM.glm, MSM.fitted
Methods : msmFit,summary,AIC,intervals,msmResid
Plot : plot,plotProb,plotReg,plotDiag


Class "MSM.fitted"

Description

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

Slots

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.

Methods

[

signature(x = "MSM.fitted", i = "character", j = "missing", drop = "missing"): extract the componentes of the model.

Author(s)

Jose A. Sanchez-Espigares, Alberto Lopez-Moreno

See Also

Overview: MSwM-package
Classes : MSM.lm, MSM.glm, MSM.fitted
Methods : msmFit,summary,AIC,intervals,msmResid
Plot : plot,plotProb,plotReg,plotDiag


Class "MSM.glm"

Description

MSM.glm is an object containing Markov Switching model information for general linear models.

Objects from the Class

msmFit is an algorithm that builds a MSM.glm when the original model class is glm.

Slots

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.

Extends

Class "MSM.linear", directly. Class "MSM", by class "MSM.linear", distance 2.

Methods

[

signature(x = "MSM.glm", i = "character", j = "missing", drop = "missing"): extract the componentes of the model.

Author(s)

Jose A. Sanchez-Espigares, Alberto Lopez-Moreno

See Also

Overview: MSwM-package
Classes : MSM.lm, MSM.glm, MSM.fitted
Methods : msmFit,summary,AIC,intervals,msmResid
Plot : plot,plotProb,plotReg,plotDiag


Class "MSM.lm"

Description

MSM.lm is an object containing Markov Switching model information for linear models.

Objects from the Class

msmFit is an algorithm that builds a MSM.lm when the original model class is lm.

Slots

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.

Extends

Class "MSM.linear", directly. Class "MSM", by class "MSM.linear", distance 2.

Methods

[

signature(x = "MSM.lm", i = "character", j = "missing", drop = "missing"): extract the componentes of the model.

Author(s)

Jose A. Sanchez-Espigares, Alberto Lopez-Moreno

See Also

Overview: MSwM-package
Classes : MSM.lm, MSM.glm, MSM.fitted
Methods : msmFit,summary,AIC,intervals,msmResid
Plot : plot,plotProb,plotReg,plotDiag


Fitting Markov Switching Models

Description

msmFit is an implementation for modeling Markov Switching Models using the EM algorithm

Usage

msmFit(object, k, sw, p, data, family, control)

Arguments

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 p is higher than zero, the last values of sw have to contain the AR coefficients which have switching.

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".

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.

Value

msm.fit returns an object of class MSM.lm or MSM.glm, depending on the input model.

Author(s)

Jose A. Sanchez-Espigares, Alberto Lopez-Moreno

References

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).

See Also

Overview: MSwM-package
Classes : MSM.lm, MSM.glm, MSM.fitted
Methods : msmFit,summary,AIC,intervals,msmResid
Plot : plot,plotProb,plotReg,plotDiag

Examples

## 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)

Extract Markov Switching Model Residuals

Description

msmResid is a function which extracts model residuals from objects returned by Markov Switching modeling functions.

Usage

msmResid(object, regime)

Arguments

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.

Value

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.

Author(s)

Jose A. Sanchez-Espigares, Alberto Lopez-Moreno

See Also

Overview: MSwM-package
Classes : MSM.lm, MSM.glm, MSM.fitted
Methods : msmFit,summary,AIC,intervals,msmResid
Plot : plot,plotProb,plotReg,plotDiag


Function: plot for Markov Switching Models

Description

Plot that shows the rediduals for each regime with the conditional residuals.

Usage

plot(x, y, ...)

Arguments

x

an object of class "MSM.lm" or "MSM.glm".

y

currently not used.

...

currently not used.

Author(s)

Jose A. Sanchez-Espigares, Alberto Lopez-Moreno

See Also

Overview: MSwM-package
Classes : MSM.lm, MSM.glm, MSM.fitted
Methods : msmFit,summary,AIC,intervals,msmResid
Plot : plot,plotProb,plotReg,plotDiag


Plot Diagnostics for an msm Object

Description

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.

Usage

plotDiag(x, regime, which)

Arguments

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 all.

which

if a subset of the plots is required, specify a subset of the numbers 1:3. The default value is c(1:3). See details for more information.

Details

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.

Author(s)

Jose A. Sanchez-Espigares, Alberto Lopez-Moreno

See Also

Overview: MSwM-package
Classes : MSM.lm, MSM.glm, MSM.fitted
Methods : msmFit,summary,AIC,intervals,msmResid
Plot : plot,plotProb,plotReg,plotDiag


Plot of filtered and smoothed probabilities with regimes specifications.

Description

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.

Usage

plotProb(x, which)

Arguments

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.

Details

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.

Author(s)

Jose A. Sanchez-Espigares, Alberto Lopez-Moreno

See Also

Overview: MSwM-package
Classes : MSM.lm, MSM.glm, MSM.fitted
Methods : msmFit,summary,AIC,intervals,msmResid
Plot : plot,plotProb,plotReg,plotDiag


Comparative plots of response and explanatory variables with regime specifications.

Description

Creates a plot with the response and the explanatory variables with the smoothed probabilities of a specific regime.

Usage

plotReg(x, expl, regime = 1)

Arguments

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.

Author(s)

Jose A. Sanchez-Espigares, Alberto Lopez-Moreno

See Also

Overview: MSwM-package
Classes : MSM.lm, MSM.glm, MSM.fitted
Methods : msmFit,summary,AIC,intervals,msmResid
Plot : plot,plotProb,plotReg,plotDiag


Function: Summary for Markov Switching Models

Description

summary produces results summaries of the results of fitting Markov Switching Models.

Usage

## S4 method for signature 'MSM.lm'
summary(object)
## S4 method for signature 'MSM.glm'
summary(object)

Arguments

object

an object of class "MSM.lm" or "MSM.glm".

Author(s)

Jose A. Sanchez-Espigares, Alberto Lopez-Moreno

See Also

Overview: MSwM-package
Classes : MSM.lm, MSM.glm, MSM.fitted
Methods : msmFit,summary,AIC,intervals,msmResid
Plot : plot,plotProb,plotReg,plotDiag


Traffic Deads in Spain

Description

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.

Usage

data(traffic)

Format

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).

Source

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).

Examples

data=data(traffic)
	ts.plot(traffic$NDead)
	
##See the vignette
#vignette("examples")