ecm: Build an error correction model

Description Usage Arguments Details Value See Also Examples

View source: R/ecm.R

Description

Builds an lm object that represents an error correction model (ECM) by automatically differencing and lagging predictor variables according to ECM methodology.

Usage

1
ecm(y, xeq, xtr, includeIntercept = TRUE, weights = NULL, ...)

Arguments

y

The target variable

xeq

The variables to be used in the equilibrium term of the error correction model

xtr

The variables to be used in the transient term of the error correction model

includeIntercept

Boolean whether the y-intercept should be included

weights

Optional vector of weights to be passed to the fitting process

...

Additional arguments to be passed to the 'lm' function (careful in that these may need to be modified for ecm or may not be appropriate!)

Details

The general format of an ECM is

Δ y = β_{0} + β_{1}Δ x_{1,t} +...+ β_{i}Δ x_{i,t} + γ(y_{t-1} - (α_{1}x_{1,t-1} +...+ α_{i}x_{i,t-1})).

The ecm function here modifies the equation to the following:

Δ y = β_{0} + β_{1}Δ x_{1,t} +...+ β_{i}Δ x_{i,t} + γ y_{t-1} + γ_{1}x_{1,t-1} +...+ γ_{i}x_{i,t-1},

where γ_{i} = -γ α_{i},

so it can be modeled as a simpler ordinary least squares (OLS) function using R's lm function.

Notice that an ECM models the change in the target variable (y). This means that the predictors will be lagged and differenced, and the model will be built on one observation less than what the user inputs for y, xeq, and xtr. If these arguments contain vectors with too few observations (eg. one single observation), the function will not work. Additionally, for the same reason, if using weights in the ecm function, the length of weights should be one less than the number of rows in xeq or xtr.

When inputting a single variable for xeq or xtr, it is important to input it in the format "xeq=df['col1']" in order to retain the data frame class. Inputting such as "xeq=df[,'col1']" or "xeq=df$col1" will result in errors in the ecm function.

ECM models are used for time series data. This means the user may need to consider stationarity and/or cointegration before using the model.

Value

an lm object representing an error correction model

See Also

lm

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
##Not run

#Use ecm to predict Wilshire 5000 index based on corporate profits, 
#Federal Reserve funds rate, and unemployment rate
data(Wilshire)

#Use 2015-12-01 and earlier data to build models
trn <- Wilshire[Wilshire$date<='2015-12-01',]

#Assume all predictors are needed in the equilibrium and transient terms of ecm
xeq <- xtr <- trn[c('CorpProfits', 'FedFundsRate', 'UnempRate')]
model1 <- ecm(trn$Wilshire5000, xeq, xtr, includeIntercept=TRUE)

#Assume CorpProfits and FedFundsRate are in the equilibrium term, 
#UnempRate has only transient impacts
xeq <- trn[c('CorpProfits', 'FedFundsRate')]
xtr <- trn['UnempRate']
model2 <- ecm(trn$Wilshire5000, xeq, xtr, includeIntercept=TRUE)

gaurbans/ecm documentation built on Jan. 6, 2019, 3:07 a.m.