-
Notifications
You must be signed in to change notification settings - Fork 449
/
nychvs.Rmd
453 lines (327 loc) · 13.1 KB
/
nychvs.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# New York City Housing and Vacancy Survey (NYCHVS) {-}
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) <a href="https://github.com/asdfree/nychvs/actions"><img src="https://github.com/asdfree/nychvs/actions/workflows/r.yml/badge.svg" alt="Github Actions Badge"></a>
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](http://www.nyc.gov/hpd), run by the [Census Bureau](http://www.census.gov/).
---
## Recommended Reading {-}
Four Example Strengths & Limitations:
✔️ [About two-thirds of 2023 sampled units were also interviewed in 2021](https://www.nyc.gov/assets/hpd/downloads/pdfs/about/2023-nychvs-selected-initial-findings.pdf#page=97)
✔️ [Additional questionnaire for units whose resident does not live there most of the time](https://www.nyc.gov/assets/hpd/downloads/pdfs/about/2023-nychvs-core-questionnaire.pdf#page=886)
❌ [Some survey years contain as much as ~30% missing data for certain housing quality indicators](https://media.nyam.org/filer_public/42/36/4236a15a-e56e-4551-8a72-cccbd1171e7e/new_york_city_housing_and_vacancy_survey.pdf)
❌ [Does not use administrative records to determine the status of tenant-based housing assistance](https://www.jstor.org/stable/20868592)
<br>
Three Example Findings:
1. [Manhattan apartment vacancies reached 10% during 2021, the Bronx less than 1%](http://doi.org/10.3386/w30662).
2. [New Yorkers paid a median rent of about $1,650 in 2023](https://www.nyc.gov/site/hpd/news/007-24/new-york-city-s-vacancy-rate-reaches-historic-low-1-4-percent-demanding-urgent-action-new).
3. [Across 1991 and 2021, the number of private, unregulated units in NYC has doubled](https://www.nyc.gov/assets/hpd/downloads/pdfs/about/2022-hpd-nychvs-rgb-testimony.pdf#page=8).
<br>
Two Methodology Documents:
> [Public Use File User Guide and Codebook](https://www.nyc.gov/assets/hpd/downloads/pdfs/about/2023-nychvs-puf-user-guide-codebook.pdf)
> [Sample Design, Weighting, and Error Estimation](https://www.nyc.gov/assets/hpd/downloads/pdfs/about/2023-nychvs-sample-design-weighting-error-estimation.pdf)
<br>
One Haiku:
```{r}
# all i want is a
# room somewhere / with clawfoot tub
# and a frigidaire
```
---
## Function Definitions {-}
Define a function to download and import each comma-separated value file:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
# nychvs_fn <- file.path( path.expand( "~" ) , "NYCHVS" , "this_file.rds" )
# saveRDS( nychvs_df , file = nychvs_fn , compress = FALSE )
```
Load the same object:
```{r eval = FALSE , results = "hide" }
# nychvs_df <- readRDS( nychvs_fn )
```
### Survey Design Definition {-}
Construct a complex sample survey design:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
svytotal( ~ one , nychvs_design )
svyby( ~ one , ~ borough , nychvs_design , svytotal )
```
### Descriptive Statistics {-}
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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):
```{r eval = FALSE , results = "hide" }
sub_nychvs_design <- subset( nychvs_design , rentburden_cat %in% 1:2 )
```
Calculate the mean (average) of this subset:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
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:
```{r eval = FALSE , results = "hide" }
degf( nychvs_design )
```
Calculate the complex sample survey-adjusted variance of any statistic:
```{r eval = FALSE , results = "hide" }
svyvar( ~ hhinc_rec1 , nychvs_design , na.rm = TRUE )
```
Include the complex sample design effect in the result for a specific statistic:
```{r eval = FALSE , results = "hide" }
# 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:
```{r eval = FALSE , results = "hide" }
svyciprop( ~ home_owners , nychvs_design ,
method = "likelihood" )
```
### Regression Models and Tests of Association {-}
Perform a design-based t-test:
```{r eval = FALSE , results = "hide" }
svyttest( hhinc_rec1 ~ home_owners , nychvs_design )
```
Perform a chi-squared test of association for survey data:
```{r eval = FALSE , results = "hide" }
svychisq(
~ home_owners + food_insecurity ,
nychvs_design
)
```
Perform a survey-weighted generalized linear model:
```{r eval = FALSE , results = "hide" }
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](https://www.nyc.gov/assets/hpd/downloads/misc/gettingstartedwithnychvs_r_video.mp4):
```{r eval = FALSE , results = "hide" }
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](https://www.nyc.gov/assets/hpd/downloads/pdfs/about/2023-nychvs-guide-to-estimating-variances.pdf#page=52):
```{r eval = FALSE , results = "hide" }
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](https://github.com/tidyverse/dplyr/)-like syntax. [srvyr](https://github.com/gergness/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](https://cran.r-project.org/web/packages/srvyr/vignettes/srvyr-vs-survey.html) details the available features. As a starting point for NYCHVS users, this code replicates previously-presented examples:
```{r eval = FALSE , results = "hide" }
library(srvyr)
nychvs_srvyr_design <- as_survey( nychvs_design )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
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 ) )
```