# American Community Survey (ACS)

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" ) )
# 2011 alabama single-year only. remove ` & stateab == 'al'` for a nationwide table
acs_cat <- subset( acs_cat , year == 2011 & 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 == 2011 & time_period == '1-Year' )
# acs_cat <- lodown( "acs" , acs_cat )
# # nationwide merged table excluding puerto rico
# acs_cat <- subset( acs_cat , year == 2011 & time_period == '1-Year' & stateab != 'pr' )
# acs_cat <- lodown( "acs" , acs_cat )
library(survey)
acs_df <-
readRDS(
file.path( path.expand( "~" ) , "ACS" ,
"acs2011_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
)
# workaround for a bug in survey::svrepdesign.character
acs_design$mse <- TRUE
```

### 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' ) )
)
```

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

```
svytotal( ~I( relp %in% 0:17 ) , acs_design ) # total population
svytotal( ~I( relp %in% 0:15 ) , acs_design ) # housing unit population
svytotal( ~I( relp %in% 16:17 ) , acs_design ) # gq population
svytotal( ~I( relp == 16 ) , acs_design ) # gq institutional population
svytotal( ~I( relp == 17 ) , acs_design ) # gq noninstitutional population
svyby( ~I( relp %in% 0:17 ) , ~ sex , acs_design , svytotal ) # total males & females
# all age categories at once #
svytotal(
~I( agep %in% 0:4 ) +
I( agep %in% 5:9 ) +
I( agep %in% 10:14 ) +
I( agep %in% 15:19 ) +
I( agep %in% 20:24 ) +
I( agep %in% 25:34 ) +
I( agep %in% 35:44 ) +
I( agep %in% 45:54 ) +
I( agep %in% 55:59 ) +
I( agep %in% 60:64 ) +
I( agep %in% 65:74 ) +
I( agep %in% 75:84 ) +
I( agep %in% 85:100 ) ,
acs_design
)
# note: the MOE (margin of error) column can be calculated as the standard error x 1.645 #
```