-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExperimentation.R
469 lines (368 loc) · 16.4 KB
/
Experimentation.R
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
library(readr)
library(tidyverse)
set.seed(6969)
years <- c(1991,1993,1996,1999,2002,2005,2008,2011,2014,2017)
dta <- list()
for (i in 1:10) {
name <- str_c("DATA/NYCHVS ", years[i]," Occupied File for ASA Challenge.csv")
dta[[i]] <- read_csv(name, skip = 1)
}
NYC <- dta[[1]] %>%
select(`Borough`, `Electricity paid separately`, `Gas paid separately`,
`Monthly cost (gas)`, `Combined gas and electric`,
`Water and sewer paid separately`, `Yearly cost (water sewer)`,
`Sub-Borough Area`, `Borough and Sub-Borough Area`,
`Year Identifier`, `Number of rooms`, `Tenure 1`)
for (i in 2:10) {
dta[[i]] %>%
select(`Borough`, `Electricity paid separately`, `Gas paid separately`,
`Monthly cost (gas)`, `Combined gas and electric`,
`Water and sewer paid separately`, `Yearly cost (water sewer)`,
`Sub-Borough Area`, `Borough and Sub-Borough Area`,
`Year Identifier`, `Number of rooms`, `Tenure 1`) %>%
bind_rows(NYC) ->
NYC
}
NYC <- NYC %>% mutate(Borough = ifelse(Borough == 1, "Bronx", Borough))
NYC <- NYC %>% mutate(Borough = ifelse(Borough == 2, "Brooklyn", Borough))
NYC <- NYC %>% mutate(Borough = ifelse(Borough == 3, "Manhattan", Borough))
NYC <- NYC %>% mutate(Borough = ifelse(Borough == 4, "Queens", Borough))
NYC <- NYC %>% mutate(Borough = ifelse(Borough == 5, "Staten Island", Borough))
NYC %>% ggplot(aes(x = `Number of rooms`, fill = Borough)) + geom_bar()
total <- NYC %>% count(Borough, `Number of rooms`) %>% group_by(Borough) %>%
mutate(percent = n/sum(n))
total %>% ggplot(aes(fill = `Number of rooms`, y = percent, x = Borough)) +
geom_bar(stat = "identity") +
scale_fill_gradientn(colors = c("pink", "red", "purple", "blue", "black"))
for (i in 1:10) {
dta[[i]] <- dta[[i]] %>%
mutate(Borough = ifelse(Borough == 1, "Bronx", Borough),
Borough = ifelse(Borough == 2, "Brooklyn", Borough),
Borough = ifelse(Borough == 3, "Manhattan", Borough),
Borough = ifelse(Borough == 4, "Queens", Borough),
Borough = ifelse(Borough == 5, "Staten Island", Borough))
}
## Electricity
elec91 <-dta[[1]] %>%
filter(`Electricity paid separately` == 3,
!(`Monthly cost (electric)` %in% c(998, 999)))
elec93 <-dta[[2]] %>%
filter(`Electricity paid separately` == 3,
!(`Monthly cost (electric)` %in% c(998, 999)))
elec93 %>%
ggplot(aes(x = factor(Borough), y = `Monthly cost (electric)`)) + geom_boxplot()
## Gas
#### 1991
gas91 <-dta[[1]] %>%
filter(`Gas paid separately`== 1, !(`Monthly cost (gas)` %in% c(998, 999, 9999)))
gas91 %>%
ggplot(aes(x = `Monthly cost (gas)`)) + geom_histogram(binwidth = 10) +
coord_cartesian(xlim = c(0, 200))
## Difference in average cost of gas bill compared by borough
gas91 %>%
ggplot(aes(x = factor(Borough), y = `Monthly cost (gas)`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas)")
### Manhattan is the lowest on average, but lots of big outliers
#### 1993
gas93 <-dta[[2]] %>%
filter(`Gas paid separately`== 1, !(`Monthly cost (gas)` %in% c(998, 999, 9999)))
gas93 %>%
ggplot(aes(x = factor(Borough), y = `Monthly cost (gas)`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas)")
### Manhattan and the Bronx are lowest, both have lots of high outliers
#### 1996
gas96 <- dta[[3]] %>%
filter(`Gas paid separately`== 1, !(`Monthly cost (gas)` %in% c(998, 999, 9999)))
gas96 %>%
ggplot(aes(x = factor(Borough), y = `Monthly cost (gas)`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas)")
### Manhattan the lowest, Bronx and Manhattan high outliers
### Staten Island highest, outliers on both sides
#### 1999
gas99 <-dta[[4]] %>%
filter(`Gas paid separately`== 1, !(`Monthly cost (gas)` %in% c(998, 999, 9999)))
gas99 %>%
ggplot(aes(x = factor(Borough), y = `Monthly cost (gas)`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas)")
### Similar to 1996
#### 2002
gas02 <-dta[[5]] %>%
filter(`Gas paid separately`== 1, !(`Monthly cost (gas)` %in% c(998, 999, 9999)))
gas02 %>%
ggplot(aes(x = factor(Borough), y = `Monthly cost (gas)`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas)")
### Similar
#### 2005
gas05 <-dta[[6]] %>%
filter(`Gas paid separately`== 1, !(`Monthly cost (gas)` %in% c(998, 999, 9999)))
gas05 %>%
ggplot(aes(x = factor(Borough), y = `Monthly cost (gas)`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas)")
### Similar
#### 2008
gas08 <-dta[[7]] %>%
filter(`Gas paid separately`== 1, !(`Monthly cost (gas)` %in% c(998, 999, 9999)))
gas08 %>%
ggplot(aes(x = factor(Borough), y = `Monthly cost (gas)`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas)")
### Similar
#### 2011
gas11 <-dta[[8]] %>%
filter(`Gas paid separately`== 1, !(`Monthly cost (gas)` %in% c(998, 999, 9999)))
gas11 %>%
ggplot(aes(x = factor(Borough), y = `Monthly cost (gas)`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas)")
### Similar
#### 2014
gas14 <-dta[[9]] %>%
filter(`Gas paid separately`== 1, !(`Monthly cost (gas)` %in% c(998, 999, 9999)))
gas14 %>%
ggplot(aes(x = factor(Borough), y = `Monthly cost (gas)`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas)")
### Similar
#### 2017
gas17 <-dta[[10]] %>%
filter(`Gas paid separately`== 1, !(`Monthly cost (gas)` %in% c(998, 999, 9999)))
gas17 %>%
ggplot(aes(x = factor(Borough), y = `Monthly cost (gas)`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas)")
### Similar
#### Entire Data Set
NYC %>%
filter(`Gas paid separately`== 1, !(`Monthly cost (gas)` %in% c(998, 999, 9999))) %>%
ggplot(aes(x = factor(Borough), y = `Monthly cost (gas)`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas)")
## Gas/electric
#### 1991
gaselec91 <- dta[[1]] %>%
filter(!(`Combined gas and electric` %in% c(998, 999)))
### Similar, but Manhattan's average increased relative to other boroughs
## Difference in average cost of gas/electricity bill compared by borough
gaselec91 %>%
ggplot(aes(x = factor(Borough), y = `Combined gas and electric`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas & Electric)")
### Staten Island most expensive by far, other 4 relatively equal
### high outliers for Bronx, high and low outliers for Manhattan
#### 1993
gaselec93 <- dta[[2]] %>%
filter(!(`Combined gas and electric` %in% c(998, 999)))
gaselec93 %>%
ggplot(aes(x = factor(Borough), y = `Combined gas and electric`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas & Electric)")
### Staten Island still most expensive barely
### Manhattan still with outliers on both sides
### All relatively close
#### 1996
gaselec96 <- dta[[3]] %>%
filter(!(`Combined gas and electric` %in% c(998, 999, 9999)))
gaselec96 %>%
ggplot(aes(x = factor(Borough), y = `Combined gas and electric`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas & Electric)")
### All relatively close, some outliers for each
#### 1999
gaselec99 <- dta[[4]] %>%
filter(!(`Combined gas and electric` %in% c(998, 999, 9999)))
gaselec99 %>%
ggplot(aes(x = factor(Borough), y = `Combined gas and electric`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas & Electric)")
### Similar
#### 2002
gaselec02 <- dta[[5]] %>%
filter(!(`Combined gas and electric` %in% c(998, 999, 9999)))
gaselec02 %>%
ggplot(aes(x = factor(Borough), y = `Combined gas and electric`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas & Electric)")
### Staten Island and Brooklyn average almost the same
### Manhattan still with weird outliers
#### 2005
gaselec05 <- dta[[6]] %>%
filter(!(`Combined gas and electric` %in% c(998, 999, 9999)))
gaselec05 %>%
ggplot(aes(x = factor(Borough), y = `Combined gas and electric`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas & Electric)")
### Similar story
#### 2008
gaselec08 <- dta[[7]] %>%
filter(!(`Combined gas and electric` %in% c(998, 999, 9999)))
gaselec08 %>%
ggplot(aes(x = factor(Borough), y = `Combined gas and electric`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas & Electric)")
### Similar story, Staten Island's spread becomes huge for some reason
#### 2011
gaselec11 <- dta[[8]] %>%
filter(!(`Combined gas and electric` %in% c(998, 999, 9999)))
gaselec11 %>%
ggplot(aes(x = factor(Borough), y = `Combined gas and electric`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas & Electric)")
### Queens jumps into second, Manhattan with a ton of outliers
#### 2014
gaselec14 <- dta[[9]] %>%
filter(!(`Combined gas and electric` %in% c(998, 999, 9999)))
gaselec14 %>%
ggplot(aes(x = factor(Borough), y = `Combined gas and electric`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas & Electric)")
### Similar
#### 2017
gaselec17 <- dta[[10]] %>%
filter(!(`Combined gas and electric` %in% c(998, 999, 9999)))
gaselec17 %>%
ggplot(aes(x = factor(Borough), y = `Combined gas and electric`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas & Electric)")
### Similar
#### Whole data set
NYC %>%
filter(!(`Combined gas and electric` %in% c(998, 999, 9999))) %>%
ggplot(aes(x = factor(Borough), y = `Combined gas and electric`)) +
geom_boxplot() + scale_y_log10() +
xlab("Boroughs") + ylab("Monthly Cost (Gas & Electric)")
## Water
water91 <- dta[[1]] %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999)))
## Difference in average cost of water bill compared by borough
water91 %>%
ggplot(aes(x = factor(Borough), y = `Yearly cost (water sewer)`)) +
geom_boxplot() +
xlab("Boroughs") + ylab("Yearly Cost (Water)")
### Queens has lots of outliers (#4)
#### 1993
water93 <-dta[[2]] %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999)))
water93 %>%
ggplot(aes(x = factor(Borough), y = `Yearly cost (water sewer)`)) +
geom_boxplot() +
xlab("Boroughs") + ylab("Yearly Cost (Water)")
### Manhattan has a substantially higher average than everywhere else
#### 1996
water96 <-dta[[3]] %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999)))
water96 %>%
ggplot(aes(x = factor(Borough), y = `Yearly cost (water sewer)`)) +
geom_boxplot() +
xlab("Boroughs") + ylab("Yearly Cost (Water)")
### Manhattan has a precipitous drop in water costs, now by far the lowest. Seems to be a cluster around 100,
### but nothing in the data description indicates that being a number meaning anything in particular.
### Further exploration below!!!
#### 1999
water99 <-dta[[4]] %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999)))
water99 %>%
ggplot(aes(x = factor(Borough), y = `Yearly cost (water sewer)`)) +
geom_boxplot() +
xlab("Boroughs") + ylab("Yearly Cost (Water)")
### Manhattan back to biggest average, large spread
#### 2002
water02 <-dta[[5]] %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999)))
water02 %>%
ggplot(aes(x = factor(Borough), y = `Yearly cost (water sewer)`)) +
geom_boxplot() +
xlab("Boroughs") + ylab("Yearly Cost (Water)")
### Manhattan spread tiny, all 5 pretty equal
#### 2005
water05 <-dta[[6]] %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999)))
water05 %>%
ggplot(aes(x = factor(Borough), y = `Yearly cost (water sewer)`)) +
geom_boxplot() +
xlab("Boroughs") + ylab("Yearly Cost (Water)")
### Manhattan's spread is insane
#### 2008
water08 <-dta[[7]] %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999)))
water08 %>%
ggplot(aes(x = factor(Borough), y = `Yearly cost (water sewer)`)) +
geom_boxplot() +
xlab("Boroughs") + ylab("Yearly Cost (Water)")
### Manhattan super small, large outliers for the other four boroughs, particularly Staten Island
#### 2011
water11 <-dta[[8]] %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999)))
water11 %>%
ggplot(aes(x = factor(Borough), y = `Yearly cost (water sewer)`)) +
geom_boxplot() +
xlab("Boroughs") + ylab("Yearly Cost (Water)")
### Similar to 2011
#### 2014
water14 <-dta[[9]] %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999)))
water14 %>%
ggplot(aes(x = factor(Borough), y = `Yearly cost (water sewer)`)) +
geom_boxplot() +
xlab("Boroughs") + ylab("Yearly Cost (Water)")
### Similar to 2011
#### 2017
water17 <-dta[[10]] %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999)))
water17 %>%
ggplot(aes(x = factor(Borough), y = `Yearly cost (water sewer)`)) +
geom_boxplot() +
xlab("Boroughs") + ylab("Yearly Cost (Water)")
### Manhattan man, this is so weird. Check for small sample size?
#### MANHATTAN HAS A VERY SMOL SAMPLE SIZE, approx 45 per year
NYC %>% filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999)),
Borough == "Manhattan") %>% View()
#### Whole data set
NYC %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999))) %>%
ggplot(aes(x = factor(Borough), y = `Yearly cost (water sewer)`)) +
geom_boxplot() +
xlab("Boroughs") + ylab("Year Cost (Water)")
### Extreme outliers are due to high topcode values in 2011 and 2014
### All boroughs pretty equal in water bills
NYC %>% filter(Borough == "Manhattan") %>%
summarise(total = n(), total_own = sum(`Tenure 1` == 1), total_rent = sum(`Tenure 1` == 9),
Own = total_own/total, Rent = total_rent/total) %>% View()
NYC %>% filter(Borough == "Bronx") %>%
summarise(total = n(), total_own = sum(`Tenure 1` == 1), total_rent = sum(`Tenure 1` == 9),
Own = total_own/total, Rent = total_rent/total) %>% View()
NYC %>% filter(Borough == "Brooklyn") %>%
summarise(total = n(), total_own = sum(`Tenure 1` == 1), total_rent = sum(`Tenure 1` == 9),
Own = total_own/total, Rent = total_rent/total) %>% View()
NYC %>% filter(Borough == "Queens") %>%
summarise(total = n(), total_own = sum(`Tenure 1` == 1), total_rent = sum(`Tenure 1` == 9),
Own = total_own/total, Rent = total_rent/total) %>% View()
NYC %>% filter(Borough == "Staten Island") %>%
summarise(total = n(), total_own = sum(`Tenure 1` == 1), total_rent = sum(`Tenure 1` == 9),
Own = total_own/total, Rent = total_rent/total) %>% View()
######### DO NOT INCLUDE IN PRESENTATION
## Separation into sub-boroughs
NYC %>% filter(Borough == "Manhattan") %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999))) %>%
ggplot(aes(x = factor(`Sub-Borough Area`), y = `Yearly cost (water sewer)`)) + geom_boxplot()
NYC %>% filter(Borough == "Bronx") %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999))) %>%
ggplot(aes(x = factor(`Sub-Borough Area`), y = `Yearly cost (water sewer)`)) + geom_boxplot()
NYC %>% filter(Borough == "Queens") %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999))) %>%
ggplot(aes(x = factor(`Sub-Borough Area`), y = `Yearly cost (water sewer)`)) + geom_boxplot()
NYC %>% filter(Borough == "Staten Island") %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999))) %>%
ggplot(aes(x = factor(`Sub-Borough Area`), y = `Yearly cost (water sewer)`)) + geom_boxplot()
NYC %>% filter(Borough == "Brooklyn") %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999))) %>%
ggplot(aes(x = factor(`Sub-Borough Area`), y = `Yearly cost (water sewer)`)) + geom_boxplot()
### Manhattan's sample sizes very low
NYC %>%
filter(`Water and sewer paid separately` == 1, !(`Yearly cost (water sewer)` %in% c(998,999,9999))) %>%
group_by(Borough, `Sub-Borough Area`) %>% count() %>% View()