Skip to content

Instantly share code, notes, and snippets.

@ericrobskyhuntley
Last active May 25, 2023 18:46
Show Gist options
  • Save ericrobskyhuntley/1ee7a3e319e3bd8eeb06c15615954fc1 to your computer and use it in GitHub Desktop.
Save ericrobskyhuntley/1ee7a3e319e3bd8eeb06c15615954fc1 to your computer and use it in GitHub Desktop.
Script to aggregate PUMS responses to reproduce and extend table characterizing the demographics of mobile home residents found in Esther Sullivan, Manufactured Insecurity Mobile Home Parks and Americans’ Tenuous Right to Place. (Berkeley, CA: University of California Press, 2018). Made for David Tisel for a forthcoming article in the _Review of…
library(tidycensus)
library(dplyr)
library(tidyr)
library(gt)
# vars <- tidycensus::pums_variables %>%
# dplyr::filter(year == 2017, survey == "acs5")
#
# vars_housing <- vars %>%
# dplyr::filter(level == "housing")
#
# vars_people <- vars %>%
# dplyr::filter(level == "person")
fetch_data <- function(year, state){
tidycensus::get_pums(
variables = base::c(
"BLD", # Units in structure (this is how we get mobile homes.)
"HISP", # Recoded detailed Hispanic origin
"RAC1P", # Recoded detailed race code
"AGEP", # Age
"HHT", # Household/family type ("Living Alone")
"CIT", # Citizenship status
"POVPIP" # Income-to-poverty ratio recode
),
state = state,
survey = "acs5",
year = year
)
}
process_data <- function(df, state, people_method = "head_household") {
# Household vars.
df %>%
# This filters for heads of households.
# TODO: Extend to aggregate individuals in households for demographics.
dplyr::filter(SPORDER == "1") %>%
dplyr::mutate(
mobile = (BLD == "1"),
alone = HHT %in% c("4", "6"),
POVPIP = base::as.numeric(POVPIP),
# Below 100% of the poverty line.
pov = dplyr::between(base::as.numeric(POVPIP), 0, 100),
# Over 55.
over55 = (base::as.numeric(AGEP) >= 55),
# Citizenship status.
noncit = (CIT == "5"),
# Hispanic/Latino
hisp = (HISP != "1"),
# Non-Hispanic white
white = (RAC1P == "1") & !hisp,
# Non-Hispanic Black
black = (RAC1P == "2") & !hisp
) %>%
summarize(
# Total mobile home households.
`Mobile Home Households` = sum(WGTP[mobile]),
# Total households.
hh_all = sum(WGTP),
# Total population in mobile homes...
ppl_mobile = sum(PWGTP[mobile]),
# ...who are living alone.
ppl_mobile_alone = sum(PWGTP[mobile & alone]),
`Living alone (%)` = (ppl_mobile_alone / ppl_mobile),
# ...who are below the poverty line.
ppl_mobile_pov = sum(PWGTP[mobile & pov]),
`Poverty (%)` = (ppl_mobile_pov / ppl_mobile),
# ...who are over 55.
ppl_mobile_over55 = sum(PWGTP[mobile & over55]),
`Age 55 or Older (%)` = (ppl_mobile_over55 / ppl_mobile),
# ...who are noncitizens
ppl_mobile_noncit = sum(PWGTP[mobile & noncit]),
`Non-Citizen (%)` = (ppl_mobile_noncit / ppl_mobile),
# ...who identify as Non-Hispanic white.
ppl_mobile_white = sum(PWGTP[mobile & white]),
`Non-Hispanic White (%)` = (ppl_mobile_white / ppl_mobile),
# ...who identify as Non-Hispanic Black.
ppl_mobile_black = sum(PWGTP[mobile & black]),
`Non-Hispanic Black (%)` = (ppl_mobile_black / ppl_mobile),
# ...who identify as Hispanic or Latino.
ppl_mobile_hisp = sum(PWGTP[mobile & hisp]),
`Hispanic (%)` = (ppl_mobile_hisp / ppl_mobile),
state = state
) %>%
select(-c(starts_with(c('ppl_', 'hh_'))))
}
va <- fetch_data(state = "VA", year = 2011)
fl <- fetch_data(state = "FL", year = 2011)
tx <- fetch_data(state = "TX", year = 2011)
# national <- fetch_data(state = "all", year = 2011)
va_processed <- process_data(va, state = "Virginia")
fl_processed <- process_data(fl, state = "Florida")
tx_processed <- process_data(tx, state = "Texas")
#national_processed <- process_data(national, state = "National")
results <- bind_rows(va_processed, tx_processed, fl_processed) %>%
tidyr::pivot_longer(
-c(state),
names_to = 'var',
values_to = 'val'
) %>%
tidyr::pivot_wider(
id_cols = var,
names_from = state,
values_from = val
)
gt(results, rowname_col = "var") %>%
fmt_integer(
rows = var %in% ("Mobile Home Households")
) %>%
fmt_percent(
rows = ends_with("(%)"),
decimals = 1
) %>%
tab_row_group(
label = "Race and Ethnicity",
rows = c(
"Non-Hispanic White (%)",
"Non-Hispanic Black (%)",
"Hispanic (%)"
)
) %>%
tab_header(
title = "Characteristics of Mobile Home Residents in Virginia, Texas, and Florida"
) %>%
tab_source_note(
source_note = "Source: American Community Survey Public-Use Microdata 2007-2011 five-year sample. Made following Esther Sullivan, Manufactured Insecurity Mobile Home Parks and Americans’ Tenuous Right to Place. (Berkeley, CA: University of California Press, 2018)."
) %>%
tab_footnote(
footnote = "Demographic characteristics are based on heads of households, not all residents of households.",
locations = cells_stub(rows = 4:8)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment