Early stage development version of a possible R package for simulating quantities of interest from Error Correction Models (ECM). This includes interactions [!INCOMPLETE!].
Use the ecm_builder
function to simulated the quantities of interest and the
ecm_plot
function (NOT COMPLETED to plot the results.
Imagine we have a two time series dv
and iv
. We want to estimate the
relationship between these two series using an error correction model.
Our estimation model could look like this (assuming we have already created the change and lag variables and they are each in their own vectors or equal length):
# 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)
We then create a data frame of fitted values for the baseline scenario to simulate.
baseline_scen <- data.frame(lag_dv = mean(lag_dv, na.rm = TRUE), lag_iv = mean(lag_iv, na.rm = TRUE))
We also specify the "shock" to iv
, the effects of which we want to compare to
the baseline.
iv_shock <- sd(d_iv, na.rm = TRUE)
We now have all of the information we need to simulate the effects estimated in the ECM over 20 periods:
library(ecmSim) 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) # Show a sample of the simulation output head(m1_sims)
The simulated quantity of interest is the change in the value of dv
. We could
alternatively supply qi_d_dv = FALSE
to ecm_builder
to return the
dependent variable at each time point (i.e. lagged dv
+ the change in dv
from the previous period).
We can plot the results (note, in the future there will be a ecm_plot
function
to simplify this process):
library(ggplot2) ggplot(m1_sims, aes(time__, qi_median, group = is_shocked, colour == is_shocked, fill = is_shocked)) + geom_line(aes(color = is_shocked)) + geom_ribbon(aes(ymin = qi_min, ymax = qi_max), alpha = 0.2) + xlab('\nSimulation Time') + ylab('Predicted dv Change\n') + theme_bw()
[ ] Create ecm_plot
function.
[ ] Interaction example.
See Warner (2016) for details.
King, Gary, Michael Tomz, and Jason Wittenberg. 2000. "Making the Most of Statistical Analyses: Improving Interpretation and Presentation." American Journal of Political Science 44(2): 341-55.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.