# \donttest{
# This example replicates a result from the Supplementary Appendix
# by Ramey and Zubairy (2018) (RZ-18)
# Load data
ag_data <- ag_data
sample_start <- 7
sample_end <- dim(ag_data)[1]
# Endogenous data
endog_data <- ag_data[sample_start:sample_end,3:5]
# Variable to shock with. Here government spending due to
# Blanchard and Perotti (2002) framework
shock <- ag_data[sample_start:sample_end, 3]
# Estimate linear model
results_lin_iv <- lp_lin_iv(endog_data,
lags_endog_lin = 4,
shock = shock,
trend = 0,
confint = 1.96,
hor = 20)
# Show all impulse responses
plot(results_lin_iv)
# Make and save plots
iv_lin_plots <- plot_lin(results_lin_iv)
# * The first element of 'iv_lin_plots' shows the response of the first
# variable (Gov) to the shock (Gov).
# * The second element of 'iv_lin_plots' shows the response of the second
# variable (Tax) to the shock (Gov).
# * ...
# This plot replicates the left plot in the mid-panel of Figure 12 in the
# Supplementary Appendix by RZ-18.
iv_lin_plots[[1]]
# Show diagnostics. The first element shows the reaction of the first given endogenous variable.
summary(results_lin_iv)
## Add lags of the identified shock ##
# Endogenous data but now exclude government spending
endog_data <- ag_data[sample_start:sample_end, 4:5]
# Variable to shock with (government spending)
shock <- ag_data[sample_start:sample_end, 3]
# Add the shock variable to exogenous data
exog_data <- shock
# Estimate linear model with lagged shock variable
results_lin_iv <- lp_lin_iv(endog_data,
lags_endog_lin = 4,
shock = shock,
exog_data = exog_data,
lags_exog = 2,
trend = 0,
confint = 1.96,
hor = 20)
# Show all responses
plot(results_lin_iv)
# Show diagnostics. The first element shows the reaction of the first endogenous variable.
summary(results_lin_iv)
##############################################################################
##### Use 2SLS #########
##############################################################################
# Set seed
set.seed(007)
# Load data
ag_data <- ag_data
sample_start <- 7
sample_end <- dim(ag_data)[1]
# Endogenous data
endog_data <- ag_data[sample_start:sample_end,3:5]
# Variable to shock with (government spending)
shock <- ag_data[sample_start:sample_end, 3]
# Generate instrument variable that is correlated with government spending
instrum <- as.data.frame(0.9*shock$Gov + rnorm(length(shock$Gov), 0, 0.02) )
# Estimate linear model via 2SLS
results_lin_iv <- lp_lin_iv(endog_data,
lags_endog_lin = 4,
shock = shock,
instrum = instrum,
use_twosls = TRUE,
trend = 0,
confint = 1.96,
hor = 20)
# Show all responses
plot(results_lin_iv)
# }
Run the code above in your browser using DataLab