ecm_builder: Create simulations for long-term effects from error...

Description Usage Arguments Details Examples

Description

Create simulations for long-term effects from error correction models (ECM)

Usage

1
2
3
4
ecm_builder(obj, baseline_df, lag_dv, lag_iv, d_iv, iv_shock, lag_iv_2, d_iv_2,
  lag_iv_lag_iv2_interaction, d_iv_d_iv2_interaction, lag_iv_d_iv2_interaction,
  d_iv_lag_iv2_interaction, iv_2_shock, shock_duration, t_extent = 5,
  nsim = 1000, ci = 0.95, slim = TRUE, qi_d_dv = TRUE, mu, Sigma)

Arguments

obj

a fitted model object from an ECM model estimated with lm.

baseline_df

a data frame with fitted values for the baseline scenario before the shock to lag_dv. Note column names should match coefficient names in obj. Variables not include are assumed to be 0. Note also that change variables should be 0.

lag_dv

charachter string identifying the name of the lagged dependent variable in obj.

lag_iv

charachter string identifying the name of the lagged independent variable in obj that experiences a shock.

d_iv

charachter string identifying the name of the change in lag_dv between the current and previous time, i.e. the size of the one period shock to lag_dv.

iv_shock

numeric shock to iv.

lag_iv_2

character string identifying variable name of the second lagged independent variable in an interaction with lag_iv, if applicable.

d_iv_2

character string identifying variable name of the second shocked independent variable in an interaction with lag_iv, if applicable.

lag_iv_lag_iv2_interaction

character string identifying the interaction term for lag_iv * lag_iv_2, if applicable.

d_iv_d_iv2_interaction

character string identifying the interaction term for d_iv * d_iv_2, if applicable.

lag_iv_d_iv2_interaction

character string identifying the interaction term for lag_iv * d_iv_2, if applicable.

d_iv_lag_iv2_interaction

character string identifying the interaction term for d_iv * lag_iv_2, if applicable.

iv_2_shock

numeric shock to iv_2. If not specified, set to 0.

shock_duration

numeric specifying how many time periods over which the shock occurs.

t_extent

numeric specifying the time points from the shock to simulate the long-term effects of the shock for. The default is 5 time points.

nsim

numeric. Number of simulations to draw.

ci

the proportion of the central interval of the simulations to return. Must be in (0, 1] or equivalently (0, 100]. Note: if ci = 1 then the full interval (i.e. 100 percent) is assumed.

slim

logical indicating whether to (if FALSE) return all simulations in the central interval specified by ci for each fitted scenario or (if TRUE) just the minimum, median, and maxium values. See qi_slimmer for more details.

qi_d_dv

logical specifying if the quantity of interest should be the simulated change in the dependent variable or its level.

mu

an optional vector giving the means of the variables estimated from an ECM model. If obj is supplied then mu is ignored.

Sigma

an optional positive-definite symmetric matrix estimated from an ECM model. The matrix specifies the covariance matrix of the variables. If obj is supplied then Sigma is ignored. Note that if the model includes an intercept the corresponding column in Sigma must be called intercept_.

Details

If an interaction with the shocked variable is included, the function assumes a "general model" setup as discussed in Warner (2016).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Simulated data to estimate the model from
iv <- rnorm(10)
lag_iv <- c(NA, iv[-length(iv)])
d_iv <-  iv - lag_iv

b0 = 2
b1 = -0.2
b2 = 1.1
b3 = 2.2
starting_d_dv <- -4.1
eps <- rnorm(iv, sd = 0.2)

dv <- vector()
d_dv <- vector()
lag_dv <- vector()
for (i in 2:length(lag_iv)) {
    if (i == 2) {
        dv_start <- b0 + b2*lag_iv[i] + b3*d_iv[i] + eps[i]
                          lag_dv[3] <- dv_start + starting_d_dv
    }
    else if (i == 3) {
        d_dv[i] <- b0 + b1*lag_dv[i] + b2*lag_iv[i] + b3*d_iv[i] + eps[i]
    }
    else if (i > 3) {
        lag_dv[i] <- lag_dv[i-1] + d_dv[i-1]
        d_dv[i] <- b0 + b1*lag_dv[i] + b2*lag_iv[i] + b3*d_iv[i] + eps[i]
    }
}

# Estimate error correction model
m1 <- lm(d_dv ~ lag_dv + lag_iv + d_iv)

# Create baseline scenario
baseline_scen <- data.frame(lag_dv = mean(lag_dv, na.rm = TRUE),
                            lag_iv = mean(lag_iv, na.rm = TRUE))

# Simulate effects of a 1 standard deviation shock to the IV over 20 time
## periods
m1_sims <- ecm_builder(obj = m1, lag_iv = 'lag_iv', d_iv = 'd_iv',
                       iv_shock = sd(d_iv, na.rm = TRUE),
                       baseline_df = baseline_scen, t_extent = 20)

christophergandrud/ecmSim documentation built on May 13, 2019, 7:02 p.m.