# load packages ----
library(readxl)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
snap_data <- read_excel("errorrates.xlsx")
summary(snap_data)
## State/Territory FY2003 FY2004 FY2005
## Length:54 Min. : 1.160 Min. : 1.970 Min. :1.190
## Class :character 1st Qu.: 5.170 1st Qu.: 4.843 1st Qu.:4.522
## Mode :character Median : 6.690 Median : 5.885 Median :5.770
## Mean : 7.007 Mean : 6.003 Mean :5.668
## 3rd Qu.: 8.240 3rd Qu.: 6.680 3rd Qu.:6.603
## Max. :13.880 Max. :13.300 Max. :9.890
##
## FY2006 FY2007 FY2008 FY2009
## Min. :1.830 Min. : 1.280 Min. :0.850 Min. :0.700
## 1st Qu.:4.053 1st Qu.: 4.207 1st Qu.:3.888 1st Qu.:2.875
## Median :6.065 Median : 5.575 Median :5.120 Median :3.865
## Mean :5.671 Mean : 5.585 Mean :5.097 Mean :3.973
## 3rd Qu.:6.975 3rd Qu.: 6.840 3rd Qu.:6.268 3rd Qu.:4.805
## Max. :9.620 Max. :10.540 Max. :9.360 Max. :7.130
##
## FY2010 FY2011 FY2012 FY2013
## Min. :0.780 Min. :0.760 Min. :0.770 Min. :0.440
## 1st Qu.:3.210 1st Qu.:3.183 1st Qu.:2.490 1st Qu.:1.923
## Median :4.275 Median :4.090 Median :3.460 Median :3.675
## Mean :4.187 Mean :4.270 Mean :3.826 Mean :3.664
## 3rd Qu.:5.268 3rd Qu.:5.015 3rd Qu.:4.938 3rd Qu.:4.780
## Max. :7.680 Max. :9.630 Max. :7.360 Max. :9.660
##
## FY2014 FY2015 FY2016 FY2017
## Min. :0.420 Min. :0.750 Mode:logical Min. : 1.210
## 1st Qu.:2.152 1st Qu.:2.545 NA's:54 1st Qu.: 4.968
## Median :4.265 Median :4.700 Median : 6.185
## Mean :3.867 Mean :4.093 Mean : 6.492
## 3rd Qu.:5.220 3rd Qu.:5.330 3rd Qu.: 7.650
## Max. :7.610 Max. :8.530 Max. :13.950
## NA's :43
## FY2018 FY2019 FY2020 FY2021
## Min. : 1.040 Min. : 1.250 Mode:logical Mode:logical
## 1st Qu.: 4.872 1st Qu.: 5.607 NA's:54 NA's:54
## Median : 6.670 Median : 7.200
## Mean : 6.988 Mean : 7.889
## 3rd Qu.: 8.838 3rd Qu.: 9.275
## Max. :16.330 Max. :22.660
##
## FY2022 FY2023 FY2024 FY2025
## Min. : 3.070 Min. : 3.270 Min. : 3.280 Min. : 2.470
## 1st Qu.: 7.317 1st Qu.: 7.022 1st Qu.: 7.655 1st Qu.: 6.890
## Median : 9.345 Median :10.375 Median : 9.525 Median : 9.475
## Mean :11.931 Mean :12.081 Mean : 9.832 Mean : 9.831
## 3rd Qu.:13.023 3rd Qu.:12.660 3rd Qu.:11.370 3rd Qu.:12.165
## Max. :56.970 Max. :60.370 Max. :24.660 Max. :23.150
##
print(head(snap_data))
## # A tibble: 6 × 24
## `State/Territory` FY2003 FY2004 FY2005 FY2006 FY2007 FY2008 FY2009 FY2010
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Alabama 8.02 8 3.68 3.8 4.78 4.23 3.57 3.75
## 2 Alaska 13.9 6.96 6.51 5.81 4.04 7.48 3.81 2.15
## 3 Arizona 5.83 6.54 7.61 8.26 4.87 4.68 5.99 6.69
## 4 Arkansas 4.02 5.34 5.43 7.15 7.01 6.1 4.74 5.64
## 5 California 7.96 6.32 6.38 6.98 5.31 5.77 4.36 4.81
## 6 Colorado 7.4 2.93 7.42 6.68 7.05 3.32 2.72 3.18
## # ℹ 15 more variables: FY2011 <dbl>, FY2012 <dbl>, FY2013 <dbl>, FY2014 <dbl>,
## # FY2015 <dbl>, FY2016 <lgl>, FY2017 <dbl>, FY2018 <dbl>, FY2019 <dbl>,
## # FY2020 <lgl>, FY2021 <lgl>, FY2022 <dbl>, FY2023 <dbl>, FY2024 <dbl>,
## # FY2025 <dbl>
per <- snap_data |>
rename(state = 1) |>
pivot_longer(-state, names_to = "fy", values_to = "per") |>
mutate(
fy = as.integer(gsub("\\D", "", fy)),
per = suppressWarnings(as.numeric(per))
) |>
filter(!is.na(per))
per <- per |>
mutate(state = trimws(state)) |>
filter(!grepl("united states|national|guam|virgin islands",
state, ignore.case = TRUE))
n_distinct(per$state)
## [1] 51
# Have to analyze 2003-2014 and 2017-2025 separately -- different procedures due to fraud
per <- per |>
mutate(era = case_when(
fy <= 2014 ~ "FY2003-2014",
fy >= 2017 ~ "FY2017-2025"
)) |>
filter(!is.na(era))
pairs <- per |>
group_by(state) |>
mutate(
per_3yr = per[match(fy + 3, fy)], #will find the match for three years later
era_3yr = era[match(fy + 3, fy)], #makes sure that 2014 and 2017 aren't paired together
per_1yr = per[match(fy + 1, fy)],
era_1yr = era[match(fy + 1, fy)]
) |>
ungroup()
lag3 <- filter(pairs, !is.na(per_3yr), era == era_3yr)
lag1 <- filter(pairs, !is.na(per_1yr), era == era_1yr)
summarize_lag <- function(df, later) {
df |>
group_by(era) |>
summarise(
pairs = n(),
pearson = cor(per, .data[[later]]), #general correation, how closely do the two years' rates move together
spearman = cor(per, .data[[later]], method = "spearman"),
slope = coef(lm(.data[[later]] ~ per))[["per"]], #
.groups = "drop")
}
cat("\nThree years later:\n")
##
## Three years later:
print(summarize_lag(lag3, "per_3yr"))
## # A tibble: 2 × 5
## era pairs pearson spearman slope
## <chr> <int> <dbl> <dbl> <dbl>
## 1 FY2003-2014 459 0.356 0.339 0.331
## 2 FY2017-2025 102 0.329 0.529 0.319
cat("\nOne year later (for context):\n")
##
## One year later (for context):
print(summarize_lag(lag1, "per_1yr"))
## # A tibble: 2 × 5
## era pairs pearson spearman slope
## <chr> <int> <dbl> <dbl> <dbl>
## 1 FY2003-2014 561 0.691 0.697 0.646
## 2 FY2017-2025 255 0.756 0.833 0.634
cat("\nThree years later, excluding Alaska:\n")
##
## Three years later, excluding Alaska:
print(summarize_lag(filter(lag3, state != "Alaska"), "per_3yr"))
## # A tibble: 2 × 5
## era pairs pearson spearman slope
## <chr> <int> <dbl> <dbl> <dbl>
## 1 FY2003-2014 450 0.351 0.333 0.330
## 2 FY2017-2025 100 0.362 0.510 0.338
lag3 |>
mutate(change = per_3yr - per) |>
arrange(desc(abs(change))) |>
select(state, fy, per, per_3yr, change) |>
head(10)
## # A tibble: 10 × 5
## state fy per per_3yr change
## <chr> <int> <dbl> <dbl> <dbl>
## 1 Alaska 2019 11.2 57.0 45.8
## 2 Alaska 2022 57.0 23.2 -33.8
## 3 Maryland 2019 8.43 35.6 27.1
## 4 Maryland 2022 35.6 13.1 -22.5
## 5 Hawaii 2019 6.21 21.8 15.6
## 6 North Carolina 2019 5.78 19.1 13.3
## 7 Oregon 2019 9.74 23.0 13.2
## 8 Tennessee 2019 6.72 19.8 13.1
## 9 North Carolina 2022 19.1 7.36 -11.7
## 10 Hawaii 2022 21.8 10.9 -10.9
tier <- function(x) {
cut(x, breaks = c(-Inf, 6, 8, 10, Inf), right = FALSE,
labels = c("0% (<6)", "5% (6-7.99)", "10% (8-9.99)", "15% (10+)"))
}
lag3 <- mutate(lag3, tier_now = tier(per), tier_3yr = tier(per_3yr))
cat("\nShare of pairs in the same tier three years later:\n")
##
## Share of pairs in the same tier three years later:
print(lag3 |>
group_by(era) |>
summarise(pairs = n(), same_tier = mean(tier_now == tier_3yr),
.groups = "drop"))
## # A tibble: 2 × 3
## era pairs same_tier
## <chr> <int> <dbl>
## 1 FY2003-2014 459 0.590
## 2 FY2017-2025 102 0.461
for (e in unique(lag3$era)) {
cat("\nTier now (rows) vs. tier three years later (columns),", e, ":\n")
print(with(filter(lag3, era == e), table(tier_now, tier_3yr)))
}
##
## Tier now (rows) vs. tier three years later (columns), FY2003-2014 :
## tier_3yr
## tier_now 0% (<6) 5% (6-7.99) 10% (8-9.99) 15% (10+)
## 0% (<6) 240 51 6 0
## 5% (6-7.99) 83 28 9 0
## 10% (8-9.99) 25 5 2 0
## 15% (10+) 5 3 1 1
##
## Tier now (rows) vs. tier three years later (columns), FY2017-2025 :
## tier_3yr
## tier_now 0% (<6) 5% (6-7.99) 10% (8-9.99) 15% (10+)
## 0% (<6) 11 3 5 5
## 5% (6-7.99) 4 6 6 6
## 10% (8-9.99) 1 5 9 10
## 15% (10+) 0 1 9 21
#Graph time
p <- ggplot(lag3, aes(per, per_3yr)) +
geom_hline(yintercept = c(6, 8, 10), colour = "grey85") +
geom_vline(xintercept = c(6, 8, 10), colour = "grey85") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed", colour = "grey40") +
geom_point(alpha = 0.6) +
facet_wrap(~era, scales = "free") +
labs(
x = "Payment error rate in year t (%)",
y = "Payment error rate three years later (%)",
title = "State SNAP payment error rates now and three years later",
caption = paste("Source: USDA FNS.",
"FY2015-16 and FY2020-21 not available",
"Gray lines mark the 6, 8, and 10 percent cost-share cutoffs.")
) +
theme_minimal()
p

ggsave("per_persistence.png", p, width = 9, height = 4.5, dpi = 300)
zoom_recent <- TRUE
era_names <- c("FY2003-2014" = "FY2003-2014 (earlier review procedures)",
"FY2017-2025" = "FY2017-2025 (current review procedures)")
plot_df <- lag3 |>
mutate(
era_label = unname(era_names[era]),
tier_status = if_else(tier_now == tier_3yr,
"Same cost-share tier three years later",
"Different cost-share tier three years later"))
hidden <- filter(plot_df, era == "FY2017-2025", per > 25 | per_3yr > 25)
if (zoom_recent) plot_df <- anti_join(plot_df, hidden, by = c("state", "fy"))
limits_df <- plot_df |>
group_by(era_label) |>
summarise(top = max(c(per, per_3yr)), .groups = "drop") |>
crossing(corner = c(0, 1)) |>
mutate(per = top * corner, per_3yr = top * corner)
cor_labels2 <- lag3 |>
group_by(era) |>
summarise(
label = sprintf("Correlation: %.2f\nRank correlation: %.2f\n%d state pairs",
cor(per, per_3yr), cor(per, per_3yr, method = "spearman"), n()),
.groups = "drop"
) |>
mutate(era_label = unname(era_names[era]))
hidden_note <- if (zoom_recent && nrow(hidden) > 0) {
paste0("Not shown (outside the axis range): ",
paste0(hidden$state, " ", hidden$fy, "-", hidden$fy + 3, " (",
round(hidden$per, 1), "% to ", round(hidden$per_3yr, 1), "%)",
collapse = "; "),
". Correlations include all pairs.")
} else ""
caption_text <- paste(
c(strwrap(paste("Source: USDA Food and Nutrition Service.",
"FY2015-16 and FY2020-21 not available. Dotted lines mark the 6, 8,",
"and 10 percent cost-share cutoffs.", hidden_note), width = 130)),
collapse = "\n")
p_final <- ggplot(plot_df, aes(per, per_3yr)) +
geom_blank(data = limits_df) +
geom_hline(yintercept = c(6, 8, 10), colour = "grey65", linetype = "dotted") +
geom_vline(xintercept = c(6, 8, 10), colour = "grey65", linetype = "dotted") +
geom_abline(slope = 1, intercept = 0, colour = "grey30", linewidth = 0.4) +
geom_point(aes(colour = tier_status), size = 1.8, alpha = 0.75) +
geom_text(data = cor_labels2, aes(x = Inf, y = -Inf, label = label),
hjust = 1.05, vjust = -0.3, size = 3.2, lineheight = 0.95,
colour = "grey20", inherit.aes = FALSE) +
facet_wrap(~ era_label, scales = "free") +
scale_colour_manual(
values = c("Same cost-share tier three years later" = "blue",
"Different cost-share tier three years later" = "orange"),
breaks = c("Same cost-share tier three years later",
"Different cost-share tier three years later"),
name = NULL) +
labs(
title = "State SNAP payment error rates in a given year and three years later",
subtitle = "Each dot is one state in one starting year. Dots on the solid line had the same rate three years later.",
x = "Payment error rate in the starting year (%)",
y = "Payment error rate three years later (%)",
caption = caption_text) +
theme_minimal(base_size = 11) +
theme(
aspect.ratio = 1,
panel.grid.minor = element_blank(),
panel.spacing = unit(2, "lines"),
strip.text = element_text(face = "bold", hjust = 0, size = 11),
legend.position = "top",
legend.justification = "left",
plot.title = element_text(face = "bold", size = 14),
plot.title.position = "plot",
plot.subtitle= element_text(colour = "grey30"),
plot.caption = element_text(hjust = 0, colour = "grey40", size = 8.5),
plot.caption.position = "plot")
p_final

tier_boxes <- data.frame(
lo = c(0, 6, 8, 10),
hi = c(6, 8, 10, Inf),
tier = factor(c("0% (rate below 6%)", "5% (6 to 7.99%)",
"10% (8 to 9.99%)", "15% (10% or more)"),
levels = c("0% (rate below 6%)", "5% (6 to 7.99%)",
"10% (8 to 9.99%)", "15% (10% or more)"))
)
p_tiers <- ggplot(plot_df, aes(per, per_3yr)) +
geom_blank(data = limits_df) +
geom_rect(data = tier_boxes,
aes(xmin = lo, xmax = hi, ymin = lo, ymax = hi, fill = tier),
alpha = 0.55, inherit.aes = FALSE) +
geom_hline(yintercept = c(6, 8, 10), colour = "grey65", linetype = "dotted") +
geom_vline(xintercept = c(6, 8, 10), colour = "grey65", linetype = "dotted") +
geom_abline(slope = 1, intercept = 0, colour = "grey30", linewidth = 0.4) +
geom_point(aes(colour = tier_status), size = 1.8, alpha = 0.85) +
geom_text(data = cor_labels2, aes(x = Inf, y = -Inf, label = label),
hjust = 1.05, vjust = -0.3, size = 3.2, lineheight = 0.95,
colour = "grey20", inherit.aes = FALSE) +
facet_wrap(~ era_label, scales = "free") +
scale_fill_manual(
values = c("#FBE0CC", "#F6BE95", "#EE9C60", "#DB7533"),
name = "State's share of benefit costs:"
) +
scale_colour_manual(
values = c("Same cost-share tier three years later" = "#1F4E79",
"Different cost-share tier three years later" = "#C0392B"),
breaks = c("Same cost-share tier three years later",
"Different cost-share tier three years later"),
name = NULL
) +
labs(
title = "State SNAP payment error rates in a given year and three years later",
subtitle = paste("Each dot is one state in one starting year. Shaded squares mark the cost-share tiers;",
"a dot inside a square was in the same tier in both years.", sep = "\n"),
x = "Payment error rate in the starting year (%)",
y = "Payment error rate three years later (%)",
caption = caption_text
) +
guides(fill = guide_legend(nrow = 1, order = 1),
colour = guide_legend(nrow = 1, order = 2)) +
theme_minimal(base_size = 11) +
theme(
aspect.ratio = 1,
legend.box = "vertical",
legend.box.just = "left",
panel.grid.minor = element_blank(),
panel.spacing = unit(2, "lines"),
strip.text = element_text(face = "bold", hjust = 0, size = 11),
legend.position = "top",
legend.justification = "left",
legend.title = element_text(size = 10),
plot.title = element_text(face = "bold", size = 14),
plot.title.position = "plot",
plot.subtitle = element_text(colour = "grey30"),
plot.caption = element_text(hjust = 0, colour = "grey40", size = 8.5),
plot.caption.position = "plot"
)
p_tiers

ggsave("per_persistence_final.png", p_final, width = 10, height = 6.5, dpi = 300, bg = "white")
ggsave("per_persistence_tiers.png", p_tiers, width = 10, height = 7.2, dpi = 300, bg = "white")