# American Housing Survey (AHS)

The American Housing Survey tracks housing structures across the United States.

A collection of tables, most with one row per housing unit.

A complex sample survey designed to generalize to both occupied and vacant housing units across the United States and also for about twenty-five metropolitan areas.

Released more or less biennially since 1973.

Sponsored by the Department of Housing and Urban Development (HUD) and conducted by the U.S. Census Bureau.

## Simplified Download and Importation

The R `lodown`

package easily downloads and imports all available AHS microdata by simply specifying `"ahs"`

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( "ahs" , output_dir = file.path( path.expand( "~" ) , "AHS" ) )
```

`lodown`

also provides a catalog of available microdata extracts with the `get_catalog()`

function. After requesting the AHS 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 AHS microdata files
ahs_cat <-
get_catalog( "ahs" ,
output_dir = file.path( path.expand( "~" ) , "AHS" ) )
# 2013 only
ahs_cat <- subset( ahs_cat , year == 2013 )
# download the microdata to your local computer
ahs_cat <- lodown( "ahs" , ahs_cat )
```

## Analysis Examples with the `survey`

library

Construct a complex sample survey design:

```
library(survey)
ahs_df <-
readRDS(
file.path( path.expand( "~" ) , "AHS" ,
"2013/national_v1.2/newhouse_repwgt.rds"
)
)
ahs_design <-
svrepdesign(
weights = ~ wgt90geo ,
repweights = "repwgt[1-9]" ,
type = "Fay" ,
rho = ( 1 - 1 / sqrt( 4 ) ) ,
mse = TRUE ,
data = ahs_df
)
```

### Variable Recoding

Add new columns to the data set:

```
ahs_design <-
update(
ahs_design ,
tenure =
factor(
ifelse( is.na( tenure ) , 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(
1 + findInterval( lot ,
c( 5500 , 11000 , 22000 ,
44000 , 220000 , 440000 ) ) ,
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( poor < 100 )
)
```

### Unweighted Counts

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

```
sum( weights( ahs_design , "sampling" ) != 0 )
svyby( ~ one , ~ tenure , ahs_design , unwtd.count )
```

### Weighted Counts

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

```
svytotal( ~ one , ahs_design )
svyby( ~ one , ~ tenure , ahs_design , svytotal )
```

### Descriptive Statistics

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

```
svymean( ~ rooms , ahs_design , na.rm = TRUE )
svyby( ~ rooms , ~ 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( ~ rooms , ahs_design , na.rm = TRUE )
svyby( ~ rooms , ~ 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( ~ rooms , ahs_design , 0.5 , na.rm = TRUE )
svyby(
~ rooms ,
~ tenure ,
ahs_design ,
svyquantile ,
0.5 ,
ci = TRUE ,
keep.var = TRUE ,
na.rm = TRUE
)
```

Estimate a ratio:

```
svyratio(
numerator = ~ rooms ,
denominator = ~ rent ,
ahs_design ,
na.rm = TRUE
)
```

### Subsetting

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

`sub_ahs_design <- subset( ahs_design , garage == 1 )`

Calculate the mean (average) of this subset:

`svymean( ~ rooms , sub_ahs_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( ~ rooms , ahs_design , na.rm = TRUE )
coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )
grouped_result <-
svyby(
~ rooms ,
~ 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:

`degf( ahs_design )`

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

`svyvar( ~ rooms , ahs_design , na.rm = TRUE )`

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

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

```
svyciprop( ~ below_poverty , ahs_design ,
method = "likelihood" , na.rm = TRUE )
```

### Regression Models and Tests of Association

Perform a design-based t-test:

`svyttest( rooms ~ below_poverty , ahs_design )`

Perform a chi-squared test of association for survey data:

```
svychisq(
~ below_poverty + lotsize ,
ahs_design
)
```

Perform a survey-weighted generalized linear model:

```
glm_result <-
svyglm(
rooms ~ below_poverty + lotsize ,
ahs_design
)
summary( glm_result )
```

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

```
library(srvyr)
ahs_srvyr_design <- as_survey( ahs_design )
```

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

```
ahs_srvyr_design %>%
summarize( mean = survey_mean( rooms , na.rm = TRUE ) )
ahs_srvyr_design %>%
group_by( tenure ) %>%
summarize( mean = survey_mean( rooms , na.rm = TRUE ) )
```

## Replication Example

The example below matches statistics and standard errors from this table pulled from the US Census Bureau’s Quick Guide to Estimating Variance Using Replicate Weights:

Compute the statistics and standard errors for monthly housing costs by owner/renter status of the unit:

```
means <- c( 1241.8890 , 972.6051 , 170.0121 )
std_err <- c( 7.3613 , 5.6956 , 6.1586 )
ci_lb <- c( 1227.3511 , 961.3569 , 157.8495 )
ci_ub <- c( 1256.4270 , 983.8532 , 182.1747 )
results <-
svyby(
~ zsmhc ,
~ tenure ,
ahs_design ,
svymean ,
na.rm = TRUE ,
na.rm.all = TRUE
)
ci_res <-
confint( results , df = degf( ahs_design ) + 1 )
stopifnot( all( round( coef( results ) , 4 ) == means ) )
stopifnot( all( round( SE( results ) , 4 ) == std_err ) )
stopifnot( all( round( ci_res[ , 1 ] , 4 ) == ci_lb ) )
stopifnot( all( round( ci_res[ , 2 ] , 4 ) == ci_ub ) )
```