Food Insecurity

Author

Coda

library(tidycensus)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(sf)
Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(ggplot2)
library(censusapi)

Attaching package: 'censusapi'
The following object is masked from 'package:methods':

    getFunction
library(ipumsr)


# Set the working directory
setwd("C:/Users/rayo-garza/Documents/R 2022")

IPUMS 2021 ACS 1-Year Sample

usa_00048<- read_ipums_ddi("usa_00048.xml")
kc_00048 <- read_ipums_micro(usa_00048, data_file = ("usa_00048.dat.gz"), verbose = FALSE)


names(kc_00048) <- tolower(names(kc_00048))


head(kc_00048)
# A tibble: 6 × 18
   year sample            serial cbser…¹  hhwt cluster statefip countyfip strata
  <int> <int+lbl>          <dbl>   <dbl> <dbl>   <dbl> <int+lb> <dbl+lbl>  <dbl>
1  2021 202101 [2021 ACS] 1.20e6 2.02e12    73 2.02e12 48 [Tex… 209       540048
2  2021 202101 [2021 ACS] 1.20e6 2.02e12    49 2.02e12 48 [Tex…  41       360248
3  2021 202101 [2021 ACS] 1.20e6 2.02e12   133 2.02e12 48 [Tex… 121       200548
4  2021 202101 [2021 ACS] 1.20e6 2.02e12    23 2.02e12 48 [Tex…   0 [Cou… 280048
5  2021 202101 [2021 ACS] 1.20e6 2.02e12     9 2.02e12 48 [Tex… 187       570048
6  2021 202101 [2021 ACS] 1.20e6 2.02e12    24 2.02e12 48 [Tex… 201       460348
# … with 9 more variables: gq <int+lbl>, foodstmp <int+lbl>, pernum <dbl>,
#   perwt <dbl>, age <int+lbl>, race <int+lbl>, raced <int+lbl>,
#   hispan <int+lbl>, hispand <int+lbl>, and abbreviated variable name
#   ¹​cbserial
names(kc_00048)
 [1] "year"      "sample"    "serial"    "cbserial"  "hhwt"      "cluster"  
 [7] "statefip"  "countyfip" "strata"    "gq"        "foodstmp"  "pernum"   
[13] "perwt"     "age"       "race"      "raced"     "hispan"    "hispand"  
snap <- filter(kc_00048,  countyfip ==113, age<=18)

# Create a new variable for weighted count of households receiving SNAP benefits
snap <- snap %>% mutate(weighted_snap = foodstmp * perwt)




# Creating new variables
snap2 <- snap %>%
   mutate(new_race = case_when(race == 1 & hispan == 0 ~ "Non-Hispanic White",
                               race == 2 & hispan == 0 ~ "Black/African American",
                               race %in% c(3,4,5,6,7,8,9) & hispan == 0 ~ "Other",
                               race %in% c(1,2) & hispan %in% c(1,2,3,4) ~ "Hispanic",
                               race %in% c(4,5,6) & hispan == 0 ~ "Asian",
                               race == 8 & hispan == 0 ~ "Two or More",
                               TRUE ~ NA_character_))

# Removing not reported values
snap3<- snap2 %>% filter(new_race != "NA")

# Checking the new variable
table(snap3$new_race)

Black/African American               Hispanic     Non-Hispanic White 
                   295                    102                     73 
                 Other 
                    90 
# Create a summary table by race and ethnicity
snap_table3 <- snap3 %>% 
  group_by(new_race) %>% 
  summarize(n = sum(weighted_snap))

# Add a column for the total number of households in each group
snap_table3 <- snap_table3 %>% 
  mutate(total_hh = sum(n))

# Print the summary table
snap_table3
# A tibble: 4 × 3
  new_race                    n total_hh
  <chr>                   <dbl>    <dbl>
1 Black/African American 119182   198110
2 Hispanic                37658   198110
3 Non-Hispanic White      19180   198110
4 Other                   22090   198110
# Filter data to include only people receiving food stamps
data_foodstmp <- snap3 %>% filter(foodstmp == 2)

data_foodstmp <- data_foodstmp %>% mutate(weight = perwt)


# Summarize weighted estimates by race and ethnicity
data_foodstmp %>% group_by(new_race) %>%  summarize(weighted_estimate = sum(weight))
# A tibble: 4 × 2
  new_race               weighted_estimate
  <chr>                              <dbl>
1 Black/African American             59591
2 Hispanic                           18829
3 Non-Hispanic White                  9590
4 Other                              11045
# Get the total weighted estimate
total_weighted_estimate <- sum(data_foodstmp$weight)

# Create a summary table by race 
snap_table4 <- data_foodstmp %>% 
  group_by(new_race) %>% 
  summarize(n = sum(weight))

# Add a column for the percentage of total weighted estimate
snap_table4 <- snap_table4 %>% 
  mutate(percentage = n / total_weighted_estimate)

# Print the summary table
snap_table4
# A tibble: 4 × 3
  new_race                   n percentage
  <chr>                  <dbl>      <dbl>
1 Black/African American 59591     0.602 
2 Hispanic               18829     0.190 
3 Non-Hispanic White      9590     0.0968
4 Other                  11045     0.112 
library(writexl)
write_xlsx(snap_table4, "snap.xlsx")