# Load required libraries library(readxl) library(dplyr) library(ggplot2) library(broom) library(stargazer) # Import the Excel file data <- read_excel("mock_ER_visits_DiD.xlsx") # Clean and prepare the data data <- data %>% mutate( state = factor(state), year = as.numeric(year), treat = as.factor(treat), post = as.factor(post), treat_post = as.factor(treat_post) ) # Run the DiD regression model model_did <- lm(er_visits ~ treat + post + treat_post, data = data) # Display regression results using stargazer stargazer(model_did, type = "text", title = "Difference-in-Differences Regression", digits = 3) # Visual inspection of parallel trends assumption avg_trend <- data %>% group_by(state, year) %>% summarise(mean_er = mean(er_visits), .groups = 'drop') ggplot(avg_trend, aes(x = year, y = mean_er, color = state)) + geom_line(size = 1.2) + geom_vline(xintercept = 2021, linetype = "dashed", color = "black") + labs( title = "Parallel Trends Check: ER Visits by State", y = "Mean ER Visits per 1000 Residents", x = "Year" ) + theme_minimal()