New York City Housing and Vacancy Survey (NYCHVS)

License: GPL v3 Github Actions Badge

A city-wide assessment of the rental vacancy rate and other characteristics related to housing stock.

  • One table with one record per occupied housing unit, a second table with one record per person inside each occupied housing unit, and a third table with one record per unoccupied housing unit.

  • A complex sample designed to generalize to occupied & unoccupied housing in the five boroughs.

  • Released more or less triennially since 1991.

  • Funded by the NYC Dept. of Housing Preservation & Development, run by the Census Bureau.


Function Definitions

Define a function to download and import each comma-separated value file:

nychvs_csv_import <-
    function( this_url ){
        
        tf <- tempfile()
        
        download.file( this_url , tf , mode = 'wb' )
        
        this_df <- read.csv( tf )
        
        names( this_df ) <- tolower( names( this_df ) )
        
        this_df
    }

Download, Import, Preparation

Download and import the all units, occupied units, person, and vacant units tables:

datasets_url <- "https://www.nyc.gov/assets/hpd/data/"

all_units_df <-
    nychvs_csv_import( paste0( datasets_url , "allunits_puf_23.csv" ) )

occupied_units_df <-
    nychvs_csv_import( paste0( datasets_url , "occupied_puf_23.csv" ) )

person_df <-
    nychvs_csv_import( paste0( datasets_url , "person_puf_23.csv" ) )

vacant_units_df <-
    nychvs_csv_import( paste0( datasets_url , "vacant_puf_23.csv" ) )

stopifnot( nrow( all_units_df ) == nrow( occupied_units_df ) + nrow( vacant_units_df ) )

Merge the information stored in the all units table onto both the occupied and vacant unit tables, then merge the information (not related to weighting) from the occupied unit table onto the person table:

before_nrow <- nrow( occupied_units_df )

occupied_units_df <- merge( all_units_df , occupied_units_df )

stopifnot( nrow( occupied_units_df ) == before_nrow )

before_nrow <- nrow( vacant_units_df )

vacant_units_df <- merge( all_units_df , vacant_units_df )

stopifnot( nrow( vacant_units_df ) == before_nrow )

before_nrow <- nrow( person_df )

weighting_variables <- grep( "^fw([0-9]+)?$" , names( occupied_units_df ) , value = TRUE )

person_df <-
    merge(
        occupied_units_df[ setdiff( names( occupied_units_df ) , weighting_variables ) ] ,
        person_df
    )

stopifnot( nrow( person_df ) == before_nrow )

all_units_df[ , 'one' ] <- occupied_units_df[ , 'one' ] <-
    vacant_units_df[ , 'one' ] <- person_df[ , 'one' ] <- 1

Save Locally  

Save the object at any point:

# nychvs_fn <- file.path( path.expand( "~" ) , "NYCHVS" , "this_file.rds" )
# saveRDS( nychvs_df , file = nychvs_fn , compress = FALSE )

Load the same object:

# nychvs_df <- readRDS( nychvs_fn )

Survey Design Definition

Construct a complex sample survey design:

library(survey)

all_units_design <-
    svrepdesign(
        weight = ~fw ,
        repweights = 'fw[0-9]+' ,
        scale = 4 / 80 ,
        rscales = rep( 1 , 80 ) ,
        mse = TRUE ,
        type = 'JK1' ,
        data = all_units_df
    )
    
occupied_units_design <-
    svrepdesign(
        weight = ~fw ,
        repweights = 'fw[0-9]+' ,
        scale = 4 / 80 ,
        rscales = rep( 1 , 80 ) ,
        mse = TRUE ,
        type = 'JK1' ,
        data = occupied_units_df
    )
    
vacant_units_design <-
    svrepdesign(
        weight = ~fw ,
        repweights = 'fw[0-9]+' ,
        scale = 4 / 80 ,
        rscales = rep( 1 , 80 ) ,
        mse = TRUE ,
        type = 'JK1' ,
        data = vacant_units_df
    )
    
person_design <-
    svrepdesign(
        weight = ~pw ,
        repweights = 'pw[0-9]+' ,
        scale = 4 / 80 ,
        rscales = rep( 1 , 80 ) ,
        mse = TRUE ,
        type = 'JK1' ,
        data = person_df
    )

nychvs_design <-
    occupied_units_design

Variable Recoding

Add new columns to the data set:

nychvs_design <- 
    update( 
        nychvs_design , 
        
        one = 1 ,
        
        home_owners = as.numeric( tenure == 2 ) ,

        yearly_household_income = hhinc_rec1 ,
        
        rent_amount = ifelse( rent_amount == -2 , NA , rent_amount ) ,
        
        borough =
            factor( boro , levels = 1:5 , labels =
                c( 'Bronx' , 'Brooklyn' , 'Manhattan' , 
                'Queens' , 'Staten Island' )
            ) ,
            
        food_insecurity = 
            factor( 
                foodinsecure , 
                levels = 1:3 , 
                labels = 
                    c( 'not insecure' , 'insecure' , 'very insecure' )
            )
            
    )

Analysis Examples with the survey library  

Unweighted Counts

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

sum( weights( nychvs_design , "sampling" ) != 0 )

svyby( ~ one , ~ borough , nychvs_design , unwtd.count )

Weighted Counts

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

svytotal( ~ one , nychvs_design )

svyby( ~ one , ~ borough , nychvs_design , svytotal )

Descriptive Statistics

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

svymean( ~ hhinc_rec1 , nychvs_design , na.rm = TRUE )

svyby( ~ hhinc_rec1 , ~ borough , nychvs_design , svymean , na.rm = TRUE )

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

svymean( ~ food_insecurity , nychvs_design , na.rm = TRUE )

svyby( ~ food_insecurity , ~ borough , nychvs_design , svymean , na.rm = TRUE )

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

svytotal( ~ hhinc_rec1 , nychvs_design , na.rm = TRUE )

svyby( ~ hhinc_rec1 , ~ borough , nychvs_design , svytotal , na.rm = TRUE )

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

svytotal( ~ food_insecurity , nychvs_design , na.rm = TRUE )

svyby( ~ food_insecurity , ~ borough , nychvs_design , svytotal , na.rm = TRUE )

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

svyquantile( ~ hhinc_rec1 , nychvs_design , 0.5 , na.rm = TRUE )

svyby( 
    ~ hhinc_rec1 , 
    ~ borough , 
    nychvs_design , 
    svyquantile , 
    0.5 ,
    ci = TRUE , na.rm = TRUE
)

Estimate a ratio:

svyratio( 
    numerator = ~ rent_amount , 
    denominator = ~ hhinc_rec1 , 
    nychvs_design ,
    na.rm = TRUE
)

Subsetting

Restrict the survey design to rent burdened units (more than 30% of income paid toward rent alone):

sub_nychvs_design <- subset( nychvs_design , rentburden_cat %in% 1:2 )

Calculate the mean (average) of this subset:

svymean( ~ hhinc_rec1 , sub_nychvs_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( ~ hhinc_rec1 , nychvs_design , na.rm = TRUE )

coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )

grouped_result <-
    svyby( 
        ~ hhinc_rec1 , 
        ~ borough , 
        nychvs_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( nychvs_design )

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

svyvar( ~ hhinc_rec1 , nychvs_design , na.rm = TRUE )

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

# SRS without replacement
svymean( ~ hhinc_rec1 , nychvs_design , na.rm = TRUE , deff = TRUE )

# SRS with replacement
svymean( ~ hhinc_rec1 , nychvs_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( ~ home_owners , nychvs_design ,
    method = "likelihood" )

Regression Models and Tests of Association

Perform a design-based t-test:

svyttest( hhinc_rec1 ~ home_owners , nychvs_design )

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

svychisq( 
    ~ home_owners + food_insecurity , 
    nychvs_design 
)

Perform a survey-weighted generalized linear model:

glm_result <- 
    svyglm( 
        hhinc_rec1 ~ home_owners + food_insecurity , 
        nychvs_design 
    )

summary( glm_result )

Replication Example

This example matches the estimate and standard error of the number of occupied housing units across the five boroughs shown at minute 6:05:

occupied_units_2021_df <-
    nychvs_csv_import( paste0( datasets_url , "occupied_puf_21.csv" ) )

occupied_units_2021_df[ , 'one' ] <- 1

nychvs_2021_design <-
    svrepdesign(
        weight = ~fw ,
        repweights = 'fw[0-9]+' ,
        scale = 4 / 80 ,
        rscales = rep( 1 , 80 ) ,
        mse = TRUE ,
        type = 'JK1' ,
        data = occupied_units_2021_df
    )

result <- svytotal( ~ one , nychvs_2021_design )
stopifnot( round( coef( result ) , 0 ) == 3157105 )
stopifnot( round( SE( result ) , 0 ) == 13439 )

This example matches Figure 6.3.2: R Output for Estimating Variance of a Mean:

result <- svymean( ~ grent , subset( nychvs_2021_design , grent > 0 & rent_amount != -2 ) )
stopifnot( round( coef( result ) , 3 ) == 1726.739 )
stopifnot( round( SE( result ) , 4 ) == 15.8292 )

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

library(srvyr)
nychvs_srvyr_design <- as_survey( nychvs_design )

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

nychvs_srvyr_design %>%
    summarize( mean = survey_mean( hhinc_rec1 , na.rm = TRUE ) )

nychvs_srvyr_design %>%
    group_by( borough ) %>%
    summarize( mean = survey_mean( hhinc_rec1 , na.rm = TRUE ) )