# American Housing Survey (AHS)

The nationwide assessment of housing stock, with information on physical condition and neighborhood, costs of financing and maintenance, owner and renter characteristics, and changes over time.

Nationally-representative and metropolitan flat files with one row per household, plus relational files.

A complex sample survey of occupied and vacant housing units designed to generalize to all structures in the United States, both nationally and also for about thirty-five metropolitan areas.

Released more or less biennially since 1973, with longitudinal samples redrawn in 1985 and 2015.

Sponsored by the Department of Housing and Urban Development, run by the Census Bureau.

Please skim before you begin:

This human-composed haiku or a bouquet of artificial intelligence-generated limericks

## Download, Import, Preparation

Download and import the national 2021 flat file:

```
library(haven)
library(httr)
tf <- tempfile()
this_url <-
paste0(
"https://www2.census.gov/programs-surveys/ahs/" ,
"2021/AHS%202021%20National%20PUF%20v1.0%20Flat%20SAS.zip"
)
GET( this_url , write_disk( tf ) , progress() )
ahs_tbl <- read_sas( tf )
ahs_df <- data.frame( ahs_tbl )
names( ahs_df ) <- tolower( names( ahs_df ) )
```

### Save locally

Save the object at any point:

```
# ahs_fn <- file.path( path.expand( "~" ) , "AHS" , "this_file.rds" )
# saveRDS( ahs_df , file = ahs_fn , compress = FALSE )
```

Load the same object:

### Variable Recoding

Add new columns to the data set:

```
ahs_design <-
update(
ahs_design ,
one = 1 ,
tenure =
factor(
ifelse( tenure %in% c( -6 , 'N' ) , 4 , tenure ) ,
levels = 1:4 ,
labels =
c( 'Owned or being bought' ,
'Rented for cash rent' ,
'Occupied without payment of cash rent' ,
'Not occupied' )
) ,
lotsize =
factor(
lotsize ,
levels = 1:7 ,
labels = c( "Less then 1/8 acre" ,
"1/8 up to 1/4 acre" , "1/4 up to 1/2 acre" ,
"1/2 up to 1 acre" , "1 up to 5 acres" ,
"5 up to 10 acres" , "10 acres or more" ) ) ,
below_poverty = as.numeric( perpovlvl < 100 )
)
```

## Analysis Examples with the `survey`

library

### Unweighted Counts

Count the unweighted number of records in the survey sample, overall and by groups:

### Descriptive Statistics

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

```
svymean( ~ totrooms , ahs_design , na.rm = TRUE )
svyby( ~ totrooms , ~ tenure , ahs_design , svymean , na.rm = TRUE )
```

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

```
svymean( ~ lotsize , ahs_design , na.rm = TRUE )
svyby( ~ lotsize , ~ tenure , ahs_design , svymean , na.rm = TRUE )
```

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

```
svytotal( ~ totrooms , ahs_design , na.rm = TRUE )
svyby( ~ totrooms , ~ tenure , ahs_design , svytotal , na.rm = TRUE )
```

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

```
svytotal( ~ lotsize , ahs_design , na.rm = TRUE )
svyby( ~ lotsize , ~ tenure , ahs_design , svytotal , na.rm = TRUE )
```

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

```
svyquantile( ~ totrooms , ahs_design , 0.5 , na.rm = TRUE )
svyby(
~ totrooms ,
~ tenure ,
ahs_design ,
svyquantile ,
0.5 ,
ci = TRUE , na.rm = TRUE
)
```

Estimate a ratio:

### Subsetting

Restrict the survey design to homes with a garage or carport:

Calculate the mean (average) of this subset:

### 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( ~ totrooms , ahs_design , na.rm = TRUE )
coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )
grouped_result <-
svyby(
~ totrooms ,
~ tenure ,
ahs_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:

Calculate the complex sample survey-adjusted variance of any statistic:

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

```
# SRS without replacement
svymean( ~ totrooms , ahs_design , na.rm = TRUE , deff = TRUE )
# SRS with replacement
svymean( ~ totrooms , ahs_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:

## Replication Example

This example matches the estimate and margin of error of the `Total`

row of the `General Housing`

tab from the AHS 2021 Table Specifications and PUF Estimates for User Verification:

```
result <- svytotal( ~ as.numeric( intstatus == 1 ) , ahs_design )
stopifnot( round( coef( result ) / 1000 , 0 ) == 128504 )
ci_results <- confint( result , level = 0.9 )
stopifnot( round( ( ci_results[ 2 ] - coef( result ) ) / 1000 , 0 ) == 388 )
```

## 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 AHS users, this code replicates previously-presented examples:

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