New York City Housing and Vacancy Survey (NYCHVS)
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.
Please skim before you begin:
A haiku regarding this microdata:
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://www2.census.gov/programs-surveys/nychvs/datasets/2021/microdata/"
all_units_df <-
nychvs_csv_import( paste0( datasets_url , "allunits_puf_21.csv" ) )
occupied_units_df <-
nychvs_csv_import( paste0( datasets_url , "occupied_puf_21.csv" ) )
person_df <-
nychvs_csv_import( paste0( datasets_url , "person_puf_21.csv" ) )
vacant_units_df <-
nychvs_csv_import( paste0( datasets_url , "vacant_puf_21.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:
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:
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:
Subsetting
Restrict the survey design to rent burdened units (more than 30% of income paid toward rent alone):
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( ~ 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:
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( ~ 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:
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:
result <- svytotal( ~ one , nychvs_design )
stopifnot( round( coef( result ) , 0 ) == 3157105 )
stopifnot( round( SE( result ) , 0 ) == 13439 )
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:
Calculate the mean (average) of a linear variable, overall and by groups: