American Community Survey (ACS)

Build Status Build status

The US Census Bureau’s annual replacement for the long-form decennial census.

  • One table with one row per household and a second table with one row per individual within each household.

  • The civilian population of the United States.

  • Released annually since 2005.

  • Administered and financed by the US Census Bureau.

Simplified Download and Importation

The R lodown package easily downloads and imports all available ACS microdata by simply specifying "acs" with an output_dir = parameter in the lodown() function. Depending on your internet connection and computer processing speed, you might prefer to run this step overnight.

library(lodown)
lodown( "acs" , output_dir = file.path( path.expand( "~" ) , "ACS" ) )

lodown also provides a catalog of available microdata extracts with the get_catalog() function. After requesting the ACS catalog, you could pass a subsetted catalog through the lodown() function in order to download and import specific extracts (rather than all available extracts).

library(lodown)
# examine all available ACS microdata files
acs_cat <-
    get_catalog( "acs" ,
        output_dir = file.path( path.expand( "~" ) , "ACS" ) )

# 2016 alabama single-year only. remove ` & stateab == 'al'` for a nationwide table
acs_cat <- subset( acs_cat , year == 2016 & time_period == '1-Year' & stateab == 'al' )
# download the microdata to your local computer
acs_cat <- lodown( "acs" , acs_cat )

Analysis Examples with the survey library  

Construct a complex sample survey design:

# # alternative subsets:

# # nationwide merged table including puerto rico
# acs_cat <- subset( acs_cat , year == 2016 & time_period == '1-Year' )
# acs_cat <- lodown( "acs" , acs_cat )

# # nationwide merged table excluding puerto rico
# acs_cat <- subset( acs_cat , year == 2016 & time_period == '1-Year' & stateab != 'pr' )
# acs_cat <- lodown( "acs" , acs_cat )

library(survey)

acs_df <- 
    readRDS( 
        file.path( path.expand( "~" ) , "ACS" , 
            "acs2016_1yr.rds" ) 
    )

# because of the catalog subset above
# the `merged.rds` file is alabama only
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 ,
        
        relp = as.numeric( relp ) ,
        
        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' ) )
    )

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 ,
    keep.var = 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 )

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 )

Replication Example

The example below matches statistics, standard errors, and margin of errors from this table pulled from the tallies of 2016 PUMS:

Match the sum of the weights:

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

Compute the population by age:

pums_estimate <- 
    c( 285681 , 314701 , 300814 , 334318 , 327896 , 629329 , 599719 , 644212 , 
    342205 , 300254 , 464893 , 231293 , 87985 )

pums_standard_error <- 
    c( 2888 , 5168 , 5009 , 3673 , 3521 , 4825 , 4088 , 
    4398 , 5329 , 5389 , 1938 , 3214 , 2950 )

pums_margin_of_error <- 
    c( 4751 , 8501 , 8240 , 6043 , 5792 , 7937 , 6725 , 
    7234 , 8767 , 8865 , 3188 , 5287 , 4853 )

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 ) )