Household examples to pull income for:
Single Mother + Child who receive SNAP + Medicaid
2 Parents + Child + Elderly
Single Parent + Child + Elderly
Pregnant + Child + Elderly
Adding ipums data
FOODSTMP = food stamp recipient?
AGE by single year
POVERTY (we want <=100)
HINSCAID = medicaid status (can use as proxy for snap eligibility?)
INCWELFR = income from welfare programs
HHINCOME = Total household income
MULTGEN = Multigenerational HH
FAMSIZE = family size
Modifications of Kaitlan’s Code
library (ipumsr)
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
# Note to self: Must set wd to where all files are, then open ddi and feed into read_ipums_micro
ddi <- read_ipums_ddi ("usa_00074.xml" )
data_raw <- read_ipums_micro (ddi)
Use of data from IPUMS USA is subject to conditions including that users should cite the data appropriately. Use command `ipums_conditions()` for more details.
data <- data_raw
names (data)
[1] "YEAR" "MULTYEAR" "SAMPLE" "SERIAL" "CBSERIAL"
[6] "HHWT" "CLUSTER" "STATEFIP" "STRATA" "GQ"
[11] "HHINCOME" "FOODSTMP" "COUPLETYPE" "MULTGEN" "MULTGEND"
[16] "PERNUM" "PERWT" "FAMUNIT" "FAMSIZE" "NCHILD"
[21] "ELDCH" "YNGCH" "RELATE" "RELATED" "SEX"
[26] "AGE" "HINSCAID" "INCWELFR" "INCSUPP" "POVERTY"
data$ inc_c <- ifelse (as.numeric (data$ HHINCOME)< 9999999 ,as.numeric (data$ HHINCOME),NA )
data$ pov_c <- ifelse (as.numeric (data$ POVERTY)> 1 & as.numeric (data$ POVERTY)< 201 ,1 ,0 )
#create person-level flags (stealing from Kaitlan's code)
data <- data %>%
mutate (
is_head = RELATE == 1 ,
is_spouse = RELATE == 2 ,
is_elderly = AGE >= 65 ,
is_child = AGE < 18 ,
is_pov = pov_c== 1 ,
is_female_head = is_head & SEX == 2 ,
has_medicaid_person = HINSCAID == 2 ,
has_foodstamps_person = FOODSTMP == 2 ,
child_on_medicaid = is_child & has_medicaid_person,
elderly_on_medicaid = is_elderly & has_medicaid_person
)
#collapse to household-level
hh_summary <- data %>%
group_by (SERIAL) %>%
summarise (
hh_income = first (inc_c),
hh_weight = first (HHWT),
fam_sz = first (FAMSIZE),
num_pov = sum (is_pov),
has_children = any (is_child),
has_elderly = any (is_elderly),
num_medicaid = sum (has_medicaid_person),
num_parents = sum (is_head | is_spouse),
num_children = sum (is_child),
num_elderly = sum (is_elderly),
num_foodstamps = sum (has_foodstamps_person),
num_children_medicaid = sum (child_on_medicaid),
num_elderly_medicaid = sum (elderly_on_medicaid),
female_head = any (is_female_head),
any_foodstamps = any (has_foodstamps_person,na.rm= FALSE ),
any_children_medicaid = all (child_on_medicaid, na.rm= FALSE ),
any_elderly_medicaid = all (elderly_on_medicaid, na.rm= FALSE )
)
#filter for households with children and elderly on Medicaid and all hh members on Food Stamps
hh_summary <- hh_summary %>%
filter (num_foodstamps> 0 & num_medicaid> 0 & num_pov> 0 )
#classify household types
hh_summary <- hh_summary %>%
mutate (
household_type = case_when (
num_parents == 1 & female_head== T & num_children == 1 & ! has_elderly & fam_sz== 2 & num_children_medicaid> 0 ~ "Single Mother + Child" ,
num_parents == 1 & num_children == 1 & fam_sz== 2 & ! has_elderly & num_children_medicaid> 0 ~ "Single Parent + Child" ,
num_parents == 2 & num_children == 1 & has_elderly & num_elderly_medicaid> 0 ~ "Two Parents + Child + Elderly" ,
num_parents == 2 & num_children == 1 & fam_sz== 3 & ! has_elderly & num_children_medicaid> 0 ~ "Two Parents + Child" ,
TRUE ~ "Other"
)
)
#calculate weighted median income by household type
#install.packages("matrixStats")
library (matrixStats)
Attaching package: 'matrixStats'
The following object is masked from 'package:dplyr':
count
weighted_median_income <- hh_summary %>%
group_by (household_type) %>%
summarise (n= n (),
tothh= sum (hh_weight),
weighted_median_income = weightedMedian (hh_income, w = hh_weight, na.rm = TRUE )
)
print (weighted_median_income)
# A tibble: 5 × 4
household_type n tothh weighted_median_income
<chr> <int> <dbl> <dbl>
1 Other 25306 636063 22964
2 Single Mother + Child 1431 42774 16322
3 Single Parent + Child 139 3734 22674.
4 Two Parents + Child 603 15936 25280.
5 Two Parents + Child + Elderly 123 2488 36352.