# 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 single-year only
acs_cat <- subset( acs_cat , year == 2011 & time_period == '1-Year' )
# download the microdata to your local computer
lodown( "acs" , acs_cat )
```

## Analysis Examples with the `survey`

library

Construct a database-backed complex sample survey design:

```
library(DBI)
library(MonetDBLite)
library(survey)
acs_design <- readRDS( file.path( path.expand( "~" ) , "ACS" , "acs2011_1yr.rds" ) )
acs_design_with_puerto_rico <- open( acs_design , driver = MonetDBLite() )
# remove puerto rico
acs_design <- subset( acs_design_with_puerto_rico , st != 72 )
```

### Variable Recoding

Add new columns to the data set:

```
acs_design <-
update(
acs_design ,
state_name =
factor(
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) ,
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")
) ,
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 , ~ state_name , acs_design , unwtd.count )
```

### Weighted Counts

Count the weighted size of the generalizable population, overall and by groups:

```
svytotal( ~ one , acs_design )
svyby( ~ one , ~ state_name , acs_design , svytotal )
```

### Descriptive Statistics

Calculate the mean (average) of a linear variable, overall and by groups:

```
svymean( ~ povpip , acs_design , na.rm = TRUE )
svyby( ~ povpip , ~ state_name , acs_design , svymean , na.rm = TRUE )
```

Calculate the distribution of a categorical variable, overall and by groups:

```
svymean( ~ sex , acs_design )
svyby( ~ sex , ~ state_name , acs_design , svymean )
```

Calculate the sum of a linear variable, overall and by groups:

```
svytotal( ~ povpip , acs_design , na.rm = TRUE )
svyby( ~ povpip , ~ state_name , acs_design , svytotal , na.rm = TRUE )
```

Calculate the weighted sum of a categorical variable, overall and by groups:

```
svytotal( ~ sex , acs_design )
svyby( ~ sex , ~ state_name , acs_design , svytotal )
```

Calculate the median (50th percentile) of a linear variable, overall and by groups:

```
svyquantile( ~ povpip , acs_design , 0.5 , na.rm = TRUE )
svyby(
~ povpip ,
~ state_name ,
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 California:

`sub_acs_design <- subset( acs_design , st == 6 )`

Calculate the mean (average) of this subset:

`svymean( ~ povpip , 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( ~ povpip , acs_design , na.rm = TRUE )
coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )
grouped_result <-
svyby(
~ povpip ,
~ state_name ,
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( ~ povpip , acs_design , na.rm = TRUE )`

Include the complex sample design effect in the result for a specific statistic:

```
# SRS without replacement
svymean( ~ povpip , acs_design , na.rm = TRUE , deff = TRUE )
# SRS with replacement
svymean( ~ povpip , 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( povpip ~ 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(
povpip ~ married + sex ,
acs_design
)
summary( glm_result )
```

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

## Database Shutdown

`close( acs_design , shutdown = TRUE )`