American Community Survey (ACS)

License: GPL v3 Github Actions Badge

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.


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)

tf_household <- tempfile()

this_url_household <-
    "https://www2.census.gov/programs-surveys/acs/data/pums/2023/1-Year/sas_hal.zip"

download.file( this_url_household , tf_household , mode = 'wb' )

unzipped_files_household <- unzip( tf_household , exdir = tempdir() )

acs_sas_household <-
    grep( '\\.sas7bdat$' , unzipped_files_household , value = TRUE )

acs_df_household <- read_sas( acs_sas_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:

tf_person <- tempfile()

this_url_person <-
    "https://www2.census.gov/programs-surveys/acs/data/pums/2023/1-Year/sas_pal.zip"

download.file( this_url_person , tf_person , mode = 'wb' )

unzipped_files_person <- unzip( tf_person , exdir = tempdir() )

acs_sas_person <-
    grep( '\\.sas7bdat$' , unzipped_files_person , value = TRUE )

acs_df_person <- read_sas( acs_sas_person )

names( acs_df_person ) <- tolower( names( acs_df_person ) )

Remove overlapping column and merge household + person files:

acs_df_household[ , 'rt' ] <- NULL

acs_df_person[ , 'rt' ] <- NULL

acs_df <- merge( acs_df_household , acs_df_person )
    
stopifnot( nrow( acs_df ) == nrow( acs_df_person ) )

acs_df[ , 'one' ] <- 1

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( state ) ,
                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:

sub_acs_design <- subset( acs_design , agep >= 65 )

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:

this_result <- svymean( ~ poverty_level , acs_design , na.rm = TRUE )

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( 
        poverty_level ~ married + sex , 
        acs_design 
    )

summary( glm_result )

Replication Example

This matches statistics, standard errors, and margin of errors from Alabama’s 2023 PUMS tallies:

Match the sum of the weights:

stopifnot( round( coef( svytotal( ~ one , acs_design ) ) , 0 ) == 5108468 )

Compute the population by age:

pums_estimate <- 
    c(287689L, 306458L, 325713L, 355557L, 334520L, 640995L, 649985L, 
    621783L, 307747L, 344812L, 553817L, 289119L, 90273L)

pums_standard_error <- 
    c(2698L, 5964L, 5865L, 5081L, 4427L, 5202L, 4615L, 4804L, 4947L, 
    4804L, 2166L, 3600L, 3080L)

pums_margin_of_error <- 
    c(4439L, 9811L, 9647L, 8358L, 7282L, 8557L, 7592L, 7903L, 8137L, 
    7902L, 3563L, 5922L, 5067L)

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)
acs_design <- convey_prep( 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)
acs_srvyr_design <- as_survey( acs_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 ) )