American Community Survey (ACS)
The US Census Bureau’s annual replacement for the long-form decennial census.
Two tables per state, the first with one row per household and the second with one row per individual.
The civilian population of the United States.
Released annually since 2005.
Administered and financed by the US Census Bureau.
Please skim before you begin:
A haiku regarding this microdata:
# one percent sample
# the decennial census
# in miniature
Download, Import, Preparation
Choose either the entire United States with sas_hus.zip
, or use a state’s abbreviation like sas_hal.zip
for Alabama or sas_hak.zip
for Alaska. This imports the Alabama household file:
library(haven)
<- tempfile()
tf_household
<-
this_url_household "https://www2.census.gov/programs-surveys/acs/data/pums/2022/1-Year/sas_hal.zip"
download.file( this_url_household , tf_household , mode = 'wb' )
<- unzip( tf_household , exdir = tempdir() )
unzipped_files_household
<-
acs_sas_household grep( '\\.sas7bdat$' , unzipped_files_household , value = TRUE )
<- read_sas( acs_sas_household )
acs_df_household
names( acs_df_household ) <- tolower( names( acs_df_household ) )
Choose either the entire United States with sas_pus.zip
, or use a state’s abbreviation like sas_pal.zip
for Alabama or sas_pak.zip
for Alaska. This imports the Alabama person file:
<- tempfile()
tf_person
<-
this_url_person "https://www2.census.gov/programs-surveys/acs/data/pums/2022/1-Year/sas_pal.zip"
download.file( this_url_person , tf_person , mode = 'wb' )
<- unzip( tf_person , exdir = tempdir() )
unzipped_files_person
<-
acs_sas_person grep( '\\.sas7bdat$' , unzipped_files_person , value = TRUE )
<- read_sas( acs_sas_person )
acs_df_person
names( acs_df_person ) <- tolower( names( acs_df_person ) )
Remove overlapping column and merge household + person files:
'rt' ] <- NULL
acs_df_household[ ,
'rt' ] <- NULL
acs_df_person[ ,
<- merge( acs_df_household , acs_df_person )
acs_df
stopifnot( nrow( acs_df ) == nrow( acs_df_person ) )
'one' ] <- 1 acs_df[ ,
Save Locally
Save the object at any point:
# acs_fn <- file.path( path.expand( "~" ) , "ACS" , "this_file.rds" )
# saveRDS( acs_df , file = acs_fn , compress = FALSE )
Load the same object:
# acs_df <- readRDS( acs_fn )
Survey Design Definition
Construct a complex sample survey design:
library(survey)
<-
acs_design svrepdesign(
weight = ~pwgtp ,
repweights = 'pwgtp[0-9]+' ,
scale = 4 / 80 ,
rscales = rep( 1 , 80 ) ,
mse = TRUE ,
type = 'JK1' ,
data = acs_df
)
Variable Recoding
Add new columns to the data set:
<-
acs_design update(
acs_design ,
state_name =
factor(
as.numeric( st ) ,
levels =
c(1L, 2L, 4L, 5L, 6L, 8L, 9L, 10L,
11L, 12L, 13L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 32L,
33L, 34L, 35L, 36L, 37L, 38L, 39L,
40L, 41L, 42L, 44L, 45L, 46L, 47L,
48L, 49L, 50L, 51L, 53L, 54L, 55L,
56L, 72L) ,labels =
c("Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "District of Columbia",
"Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana",
"Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
"Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri",
"Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey",
"New Mexico", "New York", "North Carolina", "North Dakota", "Ohio",
"Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming", "Puerto Rico")
) ,
cit =
factor(
cit , levels = 1:5 ,
labels =
c(
'born in the u.s.' ,
'born in the territories' ,
'born abroad to american parents' ,
'naturalized citizen' ,
'non-citizen'
)
) ,
poverty_level = as.numeric( povpip ) ,
married = as.numeric( mar %in% 1 ) ,
sex = factor( sex , labels = c( 'male' , 'female' ) )
)
Analysis Examples with the survey
library
Unweighted Counts
Count the unweighted number of records in the survey sample, overall and by groups:
sum( weights( acs_design , "sampling" ) != 0 )
svyby( ~ one , ~ cit , acs_design , unwtd.count )
Weighted Counts
Count the weighted size of the generalizable population, overall and by groups:
svytotal( ~ one , acs_design )
svyby( ~ one , ~ cit , acs_design , svytotal )
Descriptive Statistics
Calculate the mean (average) of a linear variable, overall and by groups:
svymean( ~ poverty_level , acs_design , na.rm = TRUE )
svyby( ~ poverty_level , ~ cit , acs_design , svymean , na.rm = TRUE )
Calculate the distribution of a categorical variable, overall and by groups:
svymean( ~ sex , acs_design )
svyby( ~ sex , ~ cit , acs_design , svymean )
Calculate the sum of a linear variable, overall and by groups:
svytotal( ~ poverty_level , acs_design , na.rm = TRUE )
svyby( ~ poverty_level , ~ cit , acs_design , svytotal , na.rm = TRUE )
Calculate the weighted sum of a categorical variable, overall and by groups:
svytotal( ~ sex , acs_design )
svyby( ~ sex , ~ cit , acs_design , svytotal )
Calculate the median (50th percentile) of a linear variable, overall and by groups:
svyquantile( ~ poverty_level , acs_design , 0.5 , na.rm = TRUE )
svyby(
~ poverty_level ,
~ cit ,
acs_design ,
svyquantile , 0.5 ,
ci = TRUE , na.rm = TRUE
)
Estimate a ratio:
svyratio(
numerator = ~ ssip ,
denominator = ~ pincp ,
acs_design ,na.rm = TRUE
)
Subsetting
Restrict the survey design to senior citizens:
<- subset( acs_design , agep >= 65 ) sub_acs_design
Calculate the mean (average) of this subset:
svymean( ~ poverty_level , sub_acs_design , na.rm = TRUE )
Measures of Uncertainty
Extract the coefficient, standard error, confidence interval, and coefficient of variation from any descriptive statistics function result, overall and by groups:
<- svymean( ~ poverty_level , acs_design , na.rm = TRUE )
this_result
coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )
<-
grouped_result svyby(
~ poverty_level ,
~ cit ,
acs_design ,
svymean ,na.rm = TRUE
)
coef( grouped_result )
SE( grouped_result )
confint( grouped_result )
cv( grouped_result )
Calculate the degrees of freedom of any survey design object:
degf( acs_design )
Calculate the complex sample survey-adjusted variance of any statistic:
svyvar( ~ poverty_level , acs_design , na.rm = TRUE )
Include the complex sample design effect in the result for a specific statistic:
# SRS without replacement
svymean( ~ poverty_level , acs_design , na.rm = TRUE , deff = TRUE )
# SRS with replacement
svymean( ~ poverty_level , acs_design , na.rm = TRUE , deff = "replace" )
Compute confidence intervals for proportions using methods that may be more accurate near 0 and 1. See ?svyciprop
for alternatives:
svyciprop( ~ married , acs_design ,
method = "likelihood" )
Regression Models and Tests of Association
Perform a design-based t-test:
svyttest( poverty_level ~ married , acs_design )
Perform a chi-squared test of association for survey data:
svychisq(
~ married + sex ,
acs_design )
Perform a survey-weighted generalized linear model:
<-
glm_result svyglm(
~ married + sex ,
poverty_level
acs_design
)
summary( glm_result )
Replication Example
This matches statistics, standard errors, and margin of errors from Alabama’s 2022 PUMS tallies:
Match the sum of the weights:
stopifnot( round( coef( svytotal( ~ one , acs_design ) ) , 0 ) == 5074296 )
Compute the population by age:
<-
pums_estimate c(277094L, 316627L, 319132L, 336759L, 350132L, 651975L, 633907L,
616899L, 317765L, 341937L, 543576L, 284790L, 83703L)
<-
pums_standard_error c(2624L, 5197L, 5002L, 4112L, 3626L, 5047L, 4559L, 4538L, 5849L,
5731L, 2061L, 3527L, 2896L)
<-
pums_margin_of_error c(4317L, 8550L, 8229L, 6764L, 5964L, 8302L, 7499L, 7465L, 9622L,
9427L, 3390L, 5801L, 4765L)
<-
results svytotal(
~ as.numeric( agep %in% 0:4 ) +
as.numeric( agep %in% 5:9 ) +
as.numeric( agep %in% 10:14 ) +
as.numeric( agep %in% 15:19 ) +
as.numeric( agep %in% 20:24 ) +
as.numeric( agep %in% 25:34 ) +
as.numeric( agep %in% 35:44 ) +
as.numeric( agep %in% 45:54 ) +
as.numeric( agep %in% 55:59 ) +
as.numeric( agep %in% 60:64 ) +
as.numeric( agep %in% 65:74 ) +
as.numeric( agep %in% 75:84 ) +
as.numeric( agep %in% 85:100 ) ,
acs_design
)
stopifnot( all( round( coef( results ) , 0 ) == pums_estimate ) )
stopifnot( all( round( SE( results ) , 0 ) == pums_standard_error ) )
stopifnot( all( round( SE( results ) * 1.645 , 0 ) == pums_margin_of_error ) )
Poverty and Inequality Estimation with convey
The R convey
library estimates measures of income concentration, poverty, inequality, and wellbeing. This textbook details the available features. As a starting point for ACS users, this code calculates the gini coefficient on complex sample survey data:
library(convey)
<- convey_prep( acs_design )
acs_design
svygini( ~ hincp , acs_design , na.rm = TRUE )
Analysis Examples with srvyr
The R srvyr
library calculates summary statistics from survey data, such as the mean, total or quantile using dplyr-like syntax. srvyr allows for the use of many verbs, such as summarize
, group_by
, and mutate
, the convenience of pipe-able functions, the tidyverse
style of non-standard evaluation and more consistent return types than the survey
package. This vignette details the available features. As a starting point for ACS users, this code replicates previously-presented examples:
library(srvyr)
<- as_survey( acs_design ) acs_srvyr_design
Calculate the mean (average) of a linear variable, overall and by groups:
%>%
acs_srvyr_design summarize( mean = survey_mean( poverty_level , na.rm = TRUE ) )
%>%
acs_srvyr_design group_by( cit ) %>%
summarize( mean = survey_mean( poverty_level , na.rm = TRUE ) )