# Generate a balanced panel dataset with 500 cross-sectional units (individuals),
# 5 time periods (labeled 1-5), a binary variable indicating which individual
# receives treatment and 2 control variables ("X_1" and "X_2") The error-terms are generated without
# heteroscedasticity, autocorrelation, or any significant clusters.
# Furthermore, there are no fixed effects (lambda and eta are both vectors
# containing only 0) and no pre-trends present in the data (all values in
# beta are 0). See sim_paneldata() for more details.
sim_data <- sim_paneldata(N = 500, tt = 5, p = 2, beta = rep(0, 5),
gamma = rep(1, 2), het = 0, phi = 0, sd = 1,
burnins = 50)
# ----------------- IU Approach -----------------
# Perform the test with equivalent threshold specified as 1 based on
# pre-treatment periods 1-4 and homoscedastic error-terms:
# To select variables, one can use the column names / numbers in the panel data
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = 2, X= c(5,6),
data = sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "IU")
# Alternatively, one can enter the variables separately:
data_Y <- sim_data$Y
data_ID <- sim_data$ID
data_G <- sim_data$G
data_period <- sim_data$period
data_X <- sim_data[, c(5, 6)]
maxEquivTest(Y = data_Y, ID = data_ID, G = data_G, period = data_period, X = data_X,
equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "IU")
# Perform the test without specifying the equivalence threshold with heteroscedastic
# and autocorrelation robust variance-covariance matrix estimator:
maxEquivTest(Y = 3, ID = 1, G = 4, period = 2,
data = sim_data, equiv_threshold = NULL, pretreatment_period = 1:4,
base_period = 4, type = "IU", vcov = "HAC")
# Perform the test without specifying the equivalence threshold with a custom
# variance-covariance matrix estimator:
vcov_func <- function(x) {plm::vcovHC(x, method = "white1", type = "HC2")}
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "IU", vcov = vcov_func)
# Perform the test using clustered standard errors based on a vector indicating
# the cluster. For instance, two clusters with the following rule: all
# individuals with an ID below 250 are in the same cluster.
cluster_ind <- ifelse(sim_data$ID < 250, 1, 2)
maxEquivTest(Y = data_Y, ID = data_ID, G = data_G, period = data_period, X = data_X,
equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "IU", vcov = "CL", cluster = cluster_ind)
# Note that the testing procedure can also handle unbalanced panels.
# Finally, one should note that the test procedure also works for unbalanced panels.
# To illustrate this, we generate an unbalanced panel dataset by randomly selecting
# 70% of the observations from the balanced panel dataset:
random_indeces <- sample(nrow(sim_data), 0.7*nrow(sim_data))
unbalanced_sim_data <- sim_data[random_indeces, ]
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period", X = c(5, 6),
data = unbalanced_sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "IU", vcov = "HAC")
#----------------- Bootstrap Approach -----------------
# \donttest{
# Perform the test with equivalence threshold specified as 1 based on
# pre-treatment periods 1:4 (with base period 4) with the general bootstrap procedure:
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "Boot")
# Perform the test with the equivalence threshold specified as 1 based on
# pre-treatment periods 1:4 (with base period 4) with the wild bootstrap procedure:
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = sim_data, equiv_threshold = 1, pretreatment_period = 1:4,
base_period = 4, type = "Wild")
# The bootstrap procedures can handle unbalanced panels:
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = unbalanced_sim_data, equiv_threshold = 1,
pretreatment_period = 1:4,
base_period = 4, type = "Boot")
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = unbalanced_sim_data, equiv_threshold = 1,
pretreatment_period = 1:4,
base_period = 4, type = "Wild")
# Performing the test without specifying the equivalence threshold:
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = sim_data, equiv_threshold = NULL, pretreatment_period = 1:4,
base_period = 4, type = "Boot")
maxEquivTest(Y = "Y", ID = "ID", G = "G", period = "period",
data = sim_data, equiv_threshold = NULL, pretreatment_period = 1:4,
base_period = 4, type = "Wild")
# }
Run the code above in your browser using DataLab