Replication for the 2021 paper “Philadelphia’s Excise Tax on Sugar-Sweetened and Artificially Sweetened Beverages and Supplemental Nutrition Assistance Program Benefit Redemption” by Benjamin W. Chrisinger.
Set up
rm(list = ls())
libraries_to_install <- c("knitr", "dplyr", "readr", "reshape2", "lubridate",
"GGally", "Synth", "foreach", "doParallel")
# Check if each library is already installed, and install it if not
libraries_not_installed <- libraries_to_install[
!sapply(libraries_to_install, requireNamespace, quietly = TRUE)]
if (length(libraries_not_installed) > 0) {
install.packages(libraries_not_installed, dependencies = TRUE)
}
# Check if the libraries are successfully loaded
if (!all(sapply(libraries_to_install, requireNamespace, quietly = TRUE))) {
message("Some libraries failed to load. Switch to manual installation")
}
library(readr)
library(dplyr)
library(lubridate)
library(doParallel)
# Set up parallel processing
cl <- makeCluster(detectCores()-2)
registerDoParallel(cl)
Read data, skip columns 1-2
d0 = read_csv("final_dataset.csv", col_select = 3:13)
str(d0)
## tibble [502,827 × 11] (S3: tbl_df/tbl/data.frame)
## $ county_state : chr [1:502827] "Abbeville County, SC" "Abbeville County, SC" "Abbeville County, SC" "Abbeville County, SC" ...
## $ State : chr [1:502827] "SC" "SC" "SC" "SC" ...
## $ Date : chr [1:502827] "2015-02" "2011-04" "2011-07" "2011-03" ...
## $ Year : num [1:502827] 2015 2011 2011 2011 2008 ...
## $ Unemployment.Rate : num [1:502827] 7.3 11.9 13 12.5 6.1 5.4 7.3 3.3 7.9 8.5 ...
## $ population.5yr : num [1:502827] 24951 24951 24951 24951 24951 ...
## $ Redemption.Amt : num [1:502827] 262259 293153 292915 290425 188941 ...
## $ Authorized.Stores : num [1:502827] 20 20 20 20 17 23 19 23 20 17 ...
## $ Snap.Individuals.USDA: num [1:502827] NA NA 4344 NA NA ...
## $ Snap.Benefits.USDA : num [1:502827] NA NA 541136 NA NA ...
## $ county_id : num [1:502827] 1 1 1 1 1 1 1 1 1 1 ...
## - attr(*, "spec")=
## .. cols(
## .. ...1 = col_skip(),
## .. FIPS = col_skip(),
## .. county_state = col_character(),
## .. State = col_character(),
## .. Date = col_character(),
## .. Year = col_double(),
## .. Unemployment.Rate = col_double(),
## .. population.5yr = col_double(),
## .. Redemption.Amt = col_double(),
## .. Authorized.Stores = col_double(),
## .. Snap.Individuals.USDA = col_double(),
## .. Snap.Benefits.USDA = col_double(),
## .. county_id = col_double()
## .. )
From the original paper code:
time counter: 1 for Jan 2005, 2 for July 2005,
3 for Jan 2006, etc. This will be used as the time.variable
for synthetic control# Make sure every year has two data points: in January and July
d0 = d0[month(as.Date(paste0(d0$Date,"-01"))) %in% c(1,7),]
d0 = d0[d0$Year>=2005 & d0$Year<=2019,]
d0 = d0[!d0$Date %in% c("2019-01"),]
posttreat.last.halfyear = length(unique(d0$Date)) # 29
pretreat.last.halfyear = length(unique(d0[d0$Year<2017,]$Date)) # 24
# Create time reference table (Date & time counter)
timekey = data.frame(Date=names(table(unique(d0$Date))),
time=seq(1:posttreat.last.halfyear))
# Left merge
d0 = merge(d0, timekey, by="Date", all.x=TRUE, sort = TRUE)
Turn data into more readable format
timekey$half.year <- format(as.Date(paste0(timekey$Date, "-01")), "%b %Y")
Remove counties with missing values under columns
Snap.Benefits.USDA and
Snap.Individuals.USDA
d0 = d0[!d0$county_state %in%
unique(d0[is.na(d0$Snap.Benefits.USDA),]$county_state),]
d0 = d0[!d0$county_state %in%
unique(d0[is.na(d0$Snap.Individuals.USDA),]$county_state),]
d0 = d0[!d0$county_state %in%
unique(d0[is.na(d0$Unemployment.Rate),]$county_state),]
Transform some predictor variables to log scale
d0$log.Redemption.Amt = log(d0$Redemption.Amt)
d0$log.Snap.Individuals.USDA = log(d0$Snap.Individuals.USDA)
d0$log.Snap.Benefits.USDA = log(d0$Snap.Benefits.USDA)
d0$log.Authorized.Stores = log(d0$Authorized.Stores)
d0$log.population.5yr = log(d0$population.5yr)
Add the outcome for SNAP redemption per participant
d0$Redemption.Amt.PC = d0$Redemption.Amt / d0$Snap.Individuals.USDA
Change the unemployment rate from double (fraction) to
numeric
d0$Unemployment.Rate = as.numeric(d0$Unemployment.Rate)
Remove counties that have been found to exhibit strange high/low cycles, spikes, and other anomalies in outcome or predictor variables
d0 = d0[!d0$county_state %in% c("Ventura County, CA",
"Santa Barbara County, CA",
"Monterey County, CA",
"Horry County, SC",
"Tulsa County, OK",
"Fresno County, CA",
"New Castle County, DE",
"Stark County, OH",
"Baltimore County, MD",
"Kern County, CA",
"Williamson County, TX",
"Cook County, IL"),]
Remove counties without complete data (only include counties with two data points per year)
# new dataframe counts of each unique value in the county_state column
n_obs = data.frame(table(d0$county_state))
# include only counties with complete data
complete = unique(n_obs[n_obs$Freq==posttreat.last.halfyear,]$Var1)
d0 = d0[d0$county_state %in% complete,]
Reassign county ID
newid = data.frame(county_state=unique(d0$county_state),
id = seq_len(length(unique(d0$county_state))))
d0 = merge(d0, newid, by="county_state")
d0$county_id = d0$id
d0 = d0 %>%
arrange(id, time)
Package the code from the original paper into a function:
study.counties = c("Philadelphia County, PA", "Montgomery County, PA",
"Delaware County, PA", "Bucks County, PA")
get_similar_counties <- function(
d, treatment.county, span1=1.25, span2=1.25, span3=5) {
# Step 0: Remove neighbors
neighbors.to.remove = setdiff(study.counties, treatment.county)
d = d[!d$county_state %in% neighbors.to.remove, ]
# Step 1: Compute median values for predictor variables
pred_avgs = d[d$time < pretreat.last.halfyear, ] %>%
group_by(county_state) %>%
summarise(
Authorized.Stores = median(Authorized.Stores, na.rm = TRUE),
Unemployment.Rate = median(Unemployment.Rate, na.rm = TRUE),
Snap.Individuals.USDA = median(Snap.Individuals.USDA, na.rm = TRUE),
Snap.Benefits.USDA = median(Snap.Benefits.USDA, na.rm = TRUE),
population.5yr = median(population.5yr, na.rm = TRUE)
)
treat_avgs = pred_avgs[pred_avgs$county_state == treatment.county, ]
# Step 2.1: Compute the intercept and slope of the linear regression Pre-2012
slope_avgs.1 = d[d$Year < 2012, ] %>%
group_by(county_state) %>%
do({
mod = lm(log.Redemption.Amt ~ time, data = .)
data.frame(Intercept = coef(mod)[1], Slope = coef(mod)[2])
})
treat_avgs.1 = slope_avgs.1[slope_avgs.1$county_state == treatment.county, ]
# Step 2.2: Compute the intercept and slope of the linear regression 2012-2015
slope_avgs.2 = d[d$Year >= 2012 & d$Year <= 2015, ] %>%
group_by(county_state) %>%
do({
mod = lm(log.Redemption.Amt ~ time, data = .)
data.frame(Intercept = coef(mod)[1], Slope = coef(mod)[2])
})
treat_avgs.2 = slope_avgs.2[slope_avgs.2$county_state == treatment.county, ]
# Step 3: Identify counties whose slope of SNAP redemption is within
# a certain range of the treatment county's slope
preds.1 = slope_avgs.1[
(slope_avgs.1$Slope < treat_avgs.1$Slope * span1 &
slope_avgs.1$Slope > treat_avgs.1$Slope * (1 / span1)), ]$county_state
preds.2 = if (treat_avgs.2$Slope > 0) {
slope_avgs.2[(slope_avgs.2$Slope < treat_avgs.2$Slope * span2 &
slope_avgs.2$Slope > treat_avgs.2$Slope * (1 / span2)), ]$county_state
} else {
slope_avgs.2[(slope_avgs.2$Slope > treat_avgs.2$Slope * span2 &
slope_avgs.2$Slope < treat_avgs.2$Slope * (1 / span2)), ]$county_state
}
# Step 4: Select counties based on proximity of predictor variables
# with treatment county
preds.3 = pred_avgs[
(pred_avgs$Authorized.Stores < treat_avgs$Authorized.Stores * span3 &
pred_avgs$Authorized.Stores > treat_avgs$Authorized.Stores * (1 / span3) &
pred_avgs$Unemployment.Rate < treat_avgs$Unemployment.Rate + span3 &
pred_avgs$Unemployment.Rate > treat_avgs$Unemployment.Rate - span3 &
pred_avgs$Snap.Individuals.USDA <
treat_avgs$Snap.Individuals.USDA * span3 &
pred_avgs$Snap.Individuals.USDA >
treat_avgs$Snap.Individuals.USDA * (1 / span3) &
pred_avgs$Snap.Benefits.USDA < treat_avgs$Snap.Benefits.USDA * span3 &
pred_avgs$Snap.Benefits.USDA > treat_avgs$Snap.Benefits.USDA * (1 / span3) &
pred_avgs$population.5yr < treat_avgs$population.5yr * span3 &
pred_avgs$population.5yr > treat_avgs$population.5yr * (1 / span3)
), ]$county_state
# Remove counties predictors that aren't close to the treatment county
d_filtered = d[(d$county_state %in% preds.1 &
d$county_state %in% preds.2 &
d$county_state %in% preds.3), ]
return(d_filtered)
}
Set up a dataframe for counties only. There are 2702 counties in the raw donor pool.
donor_pool = data.frame(county_id = unique(d0$county_id),
county_state = unique(d0$county_state))
nrow(donor_pool)
## [1] 2004
colSums(is.na(d0))
## county_state Date State
## 0 0 0
## Year Unemployment.Rate population.5yr
## 0 0 0
## Redemption.Amt Authorized.Stores Snap.Individuals.USDA
## 0 0 0
## Snap.Benefits.USDA county_id time
## 0 0 0
## log.Redemption.Amt log.Snap.Individuals.USDA log.Snap.Benefits.USDA
## 0 0 0
## log.Authorized.Stores log.population.5yr Redemption.Amt.PC
## 0 0 0
## id
## 0
This is an extra step that the author didn’t perform in the paper. We need to ensure that the matrix involved in the calculations for synthetic control is invertible, by avoiding perfect multicollinearity among the predictors.
In short, we try to find out predictors that are highly correlated.
library(GGally)
vars <- c("Unemployment.Rate",
"log.Redemption.Amt",
"Redemption.Amt.PC",
"log.Snap.Individuals.USDA",
"log.Snap.Benefits.USDA",
"log.Authorized.Stores",
"log.population.5yr")
# exclude NAs when calculate pairwise Pearson's correlation
ggcorr(d0[, vars], use = "pairwise.complete.obs", palette = "RdYlBu") +
ggtitle("Correlation Heatmap")
log.Redemtion.Amt is almost perfectly correlated with
log.population.5yr and log.Authorized.Stores.
log.population.5yr and log.Authorized.Stores
are also highly correlated.
In this case, log.population.5yr should not be used as a
predictor at any time.
Run synthetic control to get the gaps (treated - control)
Plot the gaps
library(ggplot2)
# Figure 2a
ggplot(gaps.to.plot, aes(x = half.year,
y = gap.redemption.million,
color = county,
group = county,
shape = county)) +
geom_line(linewidth = 0.5, linetype = 1) +
geom_point(size = 2) +
geom_hline(yintercept = 0, linetype = "dashed") +
geom_vline(xintercept = "2017-01", linetype = "dashed", color = "black") +
labs(title = "2a. Gaps in SNAP Redemption for 4 counties",
x = "Date",
y = "Gaps in SNAP Redemption ($ Million)",
color = NULL,
shape = NULL) +
scale_x_discrete(labels = timekey$half.year) +
scale_y_continuous(breaks = seq(-1, 3, 0.5)) +
scale_color_manual(values = c("Phil" = "gray3",
"Buck" = "orange",
"Dela" = "blue",
"Mont" = "limegreen"),
labels = c("Phil" = "Philadelphia County",
"Buck" = "Bucks County",
"Dela" = "Delaware County",
"Mont" = "Montgomery County")) +
scale_shape_manual(values = c("Phil" = 15, "Buck" = 16, "Dela" = 18, "Mont" = 17),
labels = c("Phil" = "Philadelphia County",
"Buck" = "Bucks County",
"Dela" = "Delaware County",
"Mont" = "Montgomery County")) +
theme_classic() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1), # Rotate x-label ticks
panel.border = element_blank(), # Remove panel border
legend.position = c(0.02, 0.99), # Adjust legend position
legend.justification = c(0, 1), # Adjust legend justification
legend.box.background = element_rect(color = "black"), # Border around legend
legend.text = element_text(size=8),
legend.key.size = unit(0.5, 'cm'),
legend.key.height = unit(0.3, 'cm'),
legend.key.width = unit(1, 'cm')
)
Run synthetic control to get the gaps (treated - control)
# Reshape the data to long form to plot
pcgaps.to.plot <- melt(pc.gaps,
id.vars = "half.year",
variable.name = "county",
value.name = "pc.gap.redemption")
Plot the gaps
# Figure 2b
ggplot(pcgaps.to.plot, aes(x = half.year,
y = pc.gap.redemption,
color = county,
group = county,
shape = county)) +
geom_line(linewidth = 0.5, linetype = 1) +
geom_point(size = 2) +
geom_hline(yintercept = 0, linetype = "dashed") +
geom_vline(xintercept = "2017-01", linetype = "dashed", color = "black") +
labs(title = "2b. Gaps in SNAP Redemption Per Participant for 4 counties",
x = "Date",
y = "Gaps in SNAP Redemption ($)",
color = NULL,
shape = NULL) +
scale_x_discrete(labels = timekey$half.year) +
scale_y_continuous(breaks = seq(-10, 60, 10)) +
scale_color_manual(values = c("Phil" = "gray3",
"Buck" = "orange",
"Dela" = "blue",
"Mont" = "limegreen"),
labels = c("Phil" = "Philadelphia County",
"Buck" = "Bucks County",
"Dela" = "Delaware County",
"Mont" = "Montgomery County")) +
scale_shape_manual(values = c("Phil" = 15, "Buck" = 16,
"Dela" = 18, "Mont" = 17),
labels = c("Phil" = "Philadelphia County",
"Buck" = "Bucks County",
"Dela" = "Delaware County",
"Mont" = "Montgomery County")) +
theme_classic() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1), # Rotate x-label ticks
panel.border = element_blank(), # Remove panel border
legend.position = c(0.02, 0.99), # Adjust legend position
legend.justification = c(0, 1), # Adjust legend justification
legend.box.background = element_rect(color = "black"),
legend.text = element_text(size=8),
legend.key.size = unit(0.5, 'cm'),
legend.key.height = unit(0.3, 'cm'),
legend.key.width = unit(1, 'cm')
)
# ggsave("fig2b.png")
loo.phil = loo_synth_per_participant("Philadelphia County, PA")
loo.dela = loo_synth_per_participant("Delaware County, PA")
loo.mont = loo_synth_per_participant("Montgomery County, PA")
ggplot(loo.phil, aes(x = half.year, y = gap, group = county)) +
geom_line(linewidth = 0.5, linetype = 1, color = "gray") +
geom_point(size = 1, color = "gray") +
geom_line(data = pcgaps.to.plot[pcgaps.to.plot$county == "Phil", ],
aes(x = half.year, y = pc.gap.redemption),
linetype = 1, color = "black", size = 1) + # Add the new line
geom_hline(yintercept = 0, linetype = "dashed") +
geom_vline(xintercept = "2017-01", linetype = "dashed", color = "black") +
labs(title = "Leave-one-out Analysis for Philadelphia
SNAP Redemption per participant",
x = "Date",
y = "Gaps in SNAP Redemption ($)") +
scale_x_discrete(labels = timekey$half.year) +
theme_classic() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1), # Rotate x-label ticks
panel.border = element_blank(), # Remove panel border
)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
ggplot(loo.dela, aes(x = half.year, y = gap, group = county)) +
geom_line(linewidth = 0.5, linetype = 1, color = "gray") +
geom_point(size = 1, color = "gray") +
geom_line(data = pcgaps.to.plot[pcgaps.to.plot$county == "Dela", ],
aes(x = half.year, y = pc.gap.redemption),
linetype = 1, color = "black", size = 1) + # Add the new line
geom_hline(yintercept = 0, linetype = "dashed") +
geom_vline(xintercept = "2017-01", linetype = "dashed", color = "black") +
labs(title = "Leave-one-out Analysis for Delaware
SNAP Redemption per participant",
x = "Date",
y = "Gaps in SNAP Redemption ($)") +
scale_x_discrete(labels = timekey$half.year) +
theme_classic() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1), # Rotate x-label ticks
panel.border = element_blank(), # Remove panel border
)
ggplot(loo.mont, aes(x = half.year, y = gap, group = county)) +
geom_line(linewidth = 0.5, linetype = 1, color = "gray") +
geom_point(size = 1, color = "gray") +
geom_line(data = pcgaps.to.plot[pcgaps.to.plot$county == "Mont", ],
aes(x = half.year, y = pc.gap.redemption),
linetype = 1, color = "black", size = 1) + # Add the new line
geom_hline(yintercept = 0, linetype = "dashed") +
geom_vline(xintercept = "2017-01", linetype = "dashed", color = "black") +
labs(title = "Leave-one-out Analysis for Montgomery
SNAP Redemption per participant",
x = "Date",
y = "Gaps in SNAP Redemption ($)") +
scale_x_discrete(labels = timekey$half.year) +
theme_classic() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1), # Rotate x-label ticks
panel.border = element_blank(), # Remove panel border
)