### Install eventstudyr Package Source: https://context7.com/jmslab/eventstudyr/llms.txt Install the eventstudyr package from CRAN or the latest version from GitHub using devtools. ```r install.packages("eventstudyr") ``` ```r install.packages("devtools") devtools::install_github("JMSLab/eventstudyr") ``` -------------------------------- ### Install eventstudyr from GitHub Source: https://github.com/jmslab/eventstudyr/blob/main/README.md Installs the latest version of the eventstudyr package directly from GitHub using the devtools package. ```r install.packages("devtools") devtools::install_github("JMSLab/eventstudyr") ``` -------------------------------- ### Install eventstudyr from CRAN Source: https://github.com/jmslab/eventstudyr/blob/main/README.md Installs the eventstudyr package from the Comprehensive R Archive Network (CRAN). ```r install.packages("eventstudyr") ``` -------------------------------- ### Load and Utilize Sample Panel Data Source: https://context7.com/jmslab/eventstudyr/llms.txt Access the example_data dataset to demonstrate event study workflows and model estimation. ```r library(eventstudyr) # Load and inspect sample data data(example_data) dim(example_data) # [1] 2000 12 head(example_data) # A tibble showing: id, t, y_base, y_jump_m, y_smooth_m, z, x_r, etc. # View documentation ?example_data # Key variables: # - id: unit identifier # - t: time period # - y_base, y_jump_m, y_smooth_m: outcome variables # - z: policy variable # - x_r: control/proxy variable # Example usage with sample data estimates <- EventStudy( estimator = "OLS", data = example_data, outcomevar = "y_jump_m", policyvar = "z", idvar = "id", timevar = "t", post = 3, pre = 0, kernel = "fixest" ) EventStudyPlot(estimates = estimates) ``` -------------------------------- ### example_data - Sample Panel Dataset Source: https://context7.com/jmslab/eventstudyr/llms.txt A sample panel dataset from Freyaldenhoven et al. (2021) used for demonstrating event study analysis. ```APIDOC ## example_data ### Description A sample panel dataset containing 2000 rows and 12 columns, including unit identifiers, time periods, outcome variables, policy indicators, and control variables. ### Key Variables - **id** (integer) - Unit identifier. - **t** (integer) - Time period. - **y_base, y_jump_m, y_smooth_m** (numeric) - Outcome variables. - **z** (integer) - Policy variable. - **x_r** (numeric) - Control/proxy variable. ``` -------------------------------- ### Estimate Dynamic OLS Model with Controls Source: https://context7.com/jmslab/eventstudyr/llms.txt Fit a dynamic OLS model with anticipation effects, controls, unit and time fixed effects, and clustered standard errors. The 'fixest' kernel is utilized, and anticipation effects normalization is enabled. ```r library(eventstudyr) # Dynamic OLS model with anticipation effects and controls estimates_dynamic <- EventStudy( estimator = "OLS", data = example_data, outcomevar = "y_base", policyvar = "z", idvar = "id", timevar = "t", controls = "x_r", FE = TRUE, TFE = TRUE, post = 3, overidpost = 5, pre = 2, overidpre = 4, normalize = -3, cluster = TRUE, kernel = "fixest", anticipation_effects_normalization = TRUE ) summary(estimates_dynamic$output) ``` -------------------------------- ### Estimate Static Model Source: https://context7.com/jmslab/eventstudyr/llms.txt Fit a static event study model, which does not consider event-time dynamics. This is useful for a simple estimation of the overall policy effect. ```r library(eventstudyr) # Static model (no event-time dynamics) estimates_static <- EventStudy( estimator = "OLS", data = example_data, outcomevar = "y_jump_m", policyvar = "z", idvar = "id", timevar = "t", FE = TRUE, TFE = TRUE, post = 0, overidpost = 0, pre = 0, overidpre = 0, cluster = TRUE, kernel = "fixest" ) summary(estimates_static$output) ``` -------------------------------- ### Create Basic Event Study Plot Source: https://context7.com/jmslab/eventstudyr/llms.txt Generate a publication-quality event study plot using EventStudyPlot. This function takes the output from EventStudy and creates a ggplot2 object with default confidence intervals and sup-t bands. ```r library(eventstudyr) library(ggplot2) set.seed(1234) # For reproducible sup-t bands # Estimate model estimates <- EventStudy( estimator = "OLS", data = example_data, outcomevar = "y_smooth_m", policyvar = "z", idvar = "id", timevar = "t", controls = "x_r", FE = TRUE, TFE = TRUE, post = 3, overidpost = 5, pre = 2, overidpre = 4, normalize = -3, kernel = "fixest" ) # Basic event study plot with default settings plt <- EventStudyPlot(estimates = estimates) plt ``` -------------------------------- ### Plot Event Study with Smoothest Confound Path Source: https://context7.com/jmslab/eventstudyr/llms.txt Visualize the event study results along with the smoothest confound path by setting the 'smpath' argument to TRUE in EventStudyPlot. This helps in assessing potential confounding factors. ```r library(eventstudyr) library(ggplot2) set.seed(1234) # For reproducible sup-t bands # Estimate model estimates <- EventStudy( estimator = "OLS", data = example_data, outcomevar = "y_smooth_m", policyvar = "z", idvar = "id", timevar = "t", controls = "x_r", FE = TRUE, TFE = TRUE, post = 3, overidpost = 5, pre = 2, overidpre = 4, normalize = -3, kernel = "fixest" ) # Plot with smoothest confound path EventStudyPlot(estimates = estimates, smpath = TRUE) ``` -------------------------------- ### TestLinear() - Hypothesis Testing Source: https://context7.com/jmslab/eventstudyr/llms.txt Performs linear hypothesis tests on event study coefficients, including pre-trends and leveling-off effects using F-tests. ```APIDOC ## TestLinear(estimates, pretrends, leveling_off, test, test_name) ### Description Performs linear hypothesis tests on event study coefficients. It evaluates whether coefficients meet specified hypotheses and returns a data frame with test statistics and p-values. ### Parameters - **estimates** (object) - Required - The output object from the EventStudy() function. - **pretrends** (boolean) - Optional - If TRUE, performs a test for pre-trends. - **leveling_off** (boolean) - Optional - If TRUE, performs a test for leveling-off effects. - **test** (string) - Optional - A string specifying a custom linear hypothesis (e.g., "z_fd_lag1 = z_fd"). - **test_name** (string) - Optional - A label for the custom hypothesis test. ### Response - **data.frame** - Returns a data frame containing the Test name, F-statistic, and p-value. ``` -------------------------------- ### Estimate Basic OLS Event Study Source: https://context7.com/jmslab/eventstudyr/llms.txt Perform a basic OLS event study estimation using the EventStudy function. Specify outcome variable, policy variable, identifiers, time variable, and event window. The 'fixest' kernel is used for estimation. ```r library(eventstudyr) # Basic OLS event study estimation estimates_ols <- EventStudy( estimator = "OLS", data = example_data, outcomevar = "y_base", policyvar = "z", idvar = "id", timevar = "t", pre = 0, post = 3, normalize = -1, kernel = "fixest" ) # Access estimated model and coefficients summary(estimates_ols$output) fixest::coeftable(estimates_ols$output) # Access arguments used in estimation estimates_ols$arguments$eventstudy_coefficients ``` -------------------------------- ### Perform Linear Hypothesis Tests with TestLinear Source: https://context7.com/jmslab/eventstudyr/llms.txt Evaluate event study coefficients using F-tests for pre-trends, leveling-off effects, or custom linear hypotheses. ```r library(eventstudyr) # Estimate model estimates <- EventStudy( estimator = "OLS", data = example_data, outcomevar = "y_base", policyvar = "z", idvar = "id", timevar = "t", controls = "x_r", FE = TRUE, TFE = TRUE, post = 3, pre = 2, overidpre = 4, overidpost = 5, normalize = -3, cluster = TRUE, anticipation_effects_normalization = TRUE, kernel = "fixest" ) # Test for pre-trends and leveling-off (default behavior) test_results <- TestLinear( estimates, pretrends = TRUE, leveling_off = TRUE ) print(test_results) # Output: data.frame with Test, F-statistic, and p-value columns # Custom hypothesis test on specific coefficients TestLinear( estimates, test = "z_fd_lag1 = z_fd", test_name = "Custom Hypothesis Test", pretrends = TRUE, leveling_off = TRUE ) # Test only for pre-trends TestLinear( estimates, pretrends = TRUE, leveling_off = FALSE ) # Test only for leveling-off TestLinear( estimates, pretrends = FALSE, leveling_off = TRUE ) ``` -------------------------------- ### Customize Event Study Plots with ggplot2 Source: https://context7.com/jmslab/eventstudyr/llms.txt Apply standard ggplot2 functions to modify the appearance of event study plots generated by the package. ```r plt + geom_point(color = "red") + geom_hline(color = "gray", yintercept = 0) + theme_light() + theme(panel.grid.minor.x = element_blank()) ``` -------------------------------- ### Plot Event Study Estimates Source: https://github.com/jmslab/eventstudyr/blob/main/README.md Generates a plot of the estimated event study coefficients. This function takes the results from the EventStudy function as input. ```r plt <- EventStudyPlot(estimates = estimates_ols) plt ``` -------------------------------- ### Estimate OLS Event Study Model Source: https://github.com/jmslab/eventstudyr/blob/main/README.md Estimates a linear panel event study model using OLS. Requires specifying outcome, policy, id, and time variables, along with control variables and the pre/post periods. Uses the 'fixest' kernel for estimation. ```r library(eventstudyr) set.seed(10) # for reproducibility of sup-t bands estimates_ols <- EventStudy( estimator = "OLS", data = example_data, # Use package sample data outcomevar = "y_smooth_m", policyvar = "z", idvar = "id", timevar = "t", controls = "x_r", pre = 0, post = 4, kernel = "fixest" ) ``` -------------------------------- ### Plot Event Study Without Sup-t Bands Source: https://context7.com/jmslab/eventstudyr/llms.txt Generate an event study plot that excludes sup-t bands, displaying only confidence intervals. This can be achieved by setting the 'supt' argument to NULL in EventStudyPlot. ```r library(eventstudyr) library(ggplot2) set.seed(1234) # For reproducible sup-t bands # Estimate model estimates <- EventStudy( estimator = "OLS", data = example_data, outcomevar = "y_smooth_m", policyvar = "z", idvar = "id", timevar = "t", controls = "x_r", FE = TRUE, TFE = TRUE, post = 3, overidpost = 5, pre = 2, overidpre = 4, normalize = -3, kernel = "fixest" ) # Plot without sup-t bands (only confidence intervals) EventStudyPlot(estimates = estimates, supt = NULL) ``` -------------------------------- ### Plot Event Study with Mean and Zero Line Source: https://context7.com/jmslab/eventstudyr/llms.txt Enhance the event study plot by adding the mean of the outcome variable at the normalization period and a zero effect line. This provides additional context for interpreting the estimated effects. ```r library(eventstudyr) library(ggplot2) set.seed(1234) # For reproducible sup-t bands # Estimate model estimates <- EventStudy( estimator = "OLS", data = example_data, outcomevar = "y_smooth_m", policyvar = "z", idvar = "id", timevar = "t", controls = "x_r", FE = TRUE, TFE = TRUE, post = 3, overidpost = 5, pre = 2, overidpre = 4, normalize = -3, kernel = "fixest" ) # Plot showing mean of outcome variable at normalization period EventStudyPlot( estimates = estimates, add_mean = TRUE, add_zero_line = TRUE ) ``` -------------------------------- ### Customize Event Study Plot Appearance Source: https://context7.com/jmslab/eventstudyr/llms.txt Customize the appearance of an event study plot by setting axis titles and y-axis breaks. This allows for tailored visualization of the estimated effects. ```r library(eventstudyr) library(ggplot2) set.seed(1234) # For reproducible sup-t bands # Estimate model estimates <- EventStudy( estimator = "OLS", data = example_data, outcomevar = "y_smooth_m", policyvar = "z", idvar = "id", timevar = "t", controls = "x_r", FE = TRUE, TFE = TRUE, post = 3, overidpost = 5, pre = 2, overidpre = 4, normalize = -3, kernel = "fixest" ) # Customized plot with axis titles and y-axis breaks EventStudyPlot( estimates = estimates, xtitle = "Relative time", ytitle = "Effect on outcome", ybreaks = seq(-2, 1, 0.5) ) ``` -------------------------------- ### Estimate IV Event Study using FHS Method Source: https://context7.com/jmslab/eventstudyr/llms.txt Perform an Instrumental Variables (IV) event study estimation using the Freyaldenhoven-Hansen-Shapiro (FHS) method. This includes unit and time fixed effects, clustered standard errors, and specified event windows. ```r library(eventstudyr) # Instrumental Variables estimation using FHS method estimates_iv <- EventStudy( estimator = "FHS", data = example_data, outcomevar = "y_base", policyvar = "z", idvar = "id", timevar = "t", proxy = "x_r", FE = TRUE, TFE = TRUE, post = 2, overidpost = 1, pre = 0, overidpre = 3, normalize = -1, cluster = TRUE, kernel = "fixest" ) summary(estimates_iv$output) ``` -------------------------------- ### Plot Event Study Disabling Hypothesis Tests Source: https://context7.com/jmslab/eventstudyr/llms.txt Disable the display of hypothesis testing results in the plot caption by setting 'pre_event_coeffs' and 'post_event_coeffs' to FALSE in EventStudyPlot. This simplifies the plot's annotation. ```r library(eventstudyr) library(ggplot2) set.seed(1234) # For reproducible sup-t bands # Estimate model estimates <- EventStudy( estimator = "OLS", data = example_data, outcomevar = "y_smooth_m", policyvar = "z", idvar = "id", timevar = "t", controls = "x_r", FE = TRUE, TFE = TRUE, post = 3, overidpost = 5, pre = 2, overidpre = 4, normalize = -3, kernel = "fixest" ) # Disable hypothesis testing in plot caption EventStudyPlot( estimates = estimates, pre_event_coeffs = FALSE, post_event_coeffs = FALSE ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.