-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRats-ZipCodes.Rmd
290 lines (235 loc) · 8.28 KB
/
Rats-ZipCodes.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
---
title: 36-315 Final Project Zip Code
author: Arthur Jakobsson, Alex Cheng, Liz Chu, Kevin Ren
date: November 18, 2022
output:
html_document:
toc: yes
toc_float: yes
code_folding: show
pdf_document:
toc: yes
urlcolor: blue
---
# Rat Sighting Dataset - Data Pre-analysis
```{r, message = FALSE}
# library imports
library(tigris)
library(dplyr)
library(leaflet)
library(tidyverse)
library(sp)
library(ggmap)
library(maptools)
library(broom)
library(httr)
library(rgdal)
library(gridExtra)
library(stringr)
library(geosphere)
library(gpclib)
library(broom)
library(geojsonio)
library(tidyverse)
library(plotly)
library(maps)
library(reshape2)
library(shiny)
if (!require(gpclib)) install.packages("gpclib", type="source")
gpclibPermit()
```
```{r, include=FALSE}
rats <- read.csv(file = 'Raw/Rat_Sightings.csv')
zipnames <- read.csv(file = 'Raw/zipcodenames.csv')
tax <- read.csv(file = "Raw/nyctax-cleaned.csv")
pop <- read.csv(file = "Raw/nyc2022population.csv")
rest <- read.csv(file = "Raw/DOHMH_New_York_City_Restaurant_Inspection_Results.csv")
bar <- read.csv(file = "Raw/nycbarlocations.csv")
michelin <- read.csv(file = "Raw/nycmichelin.csv")
# NEW FOR THIS FILE: removing NA's for zipcodes
rats = subset(rats, !is.na(Incident.Zip), Incident.Zip != "")
nyczips <- geojson_read('Raw/NYC_ZIPS.geojson', what = 'sp')
nyczips <- tidy(nyczips, region = "postalCode")
# NUMBER OF RATS: n_rats
ratsumzip = rats %>% group_by(Incident.Zip) %>% tally()
nyczips = nyczips %>% left_join(., ratsumzip, by = c("id" = "Incident.Zip"))
nyczips = subset(nyczips, !is.na(n))
colnames(nyczips)[8] = "n_rats"
```
```{r}
zipnames$ZipCode <- as.character(zipnames$ZipCode)
zipnames = subset(zipnames, select = -c(X, X.1, X.2, X.3))
nyczips = nyczips %>% left_join(., zipnames, by = c("id" = "ZipCode"))
```
```{r, include = FALSE}
rest$SCORE = replace_na(rest$SCORE, 0)
# rest = subset(rest, !is.na(SCORE))
restsumzip = rest %>% group_by(ZIPCODE) %>% tally()
colnames(restsumzip) = c("ZIPCODE", "n_rest")
# NUMBER OF RESTAURANTS: n_rest
nyczips = nyczips %>% left_join(., restsumzip, by = c("id" = "ZIPCODE"))
nyczips = subset(nyczips, !is.na(n_rest))
# RATIOS: rest_to_rat rat_to_rest
# note: we decided to only use rat_to_rest as the other one has too small vals
nyczips$rest_to_rat = nyczips$n_rest / nyczips$n_rats
nyczips$rat_to_rest = nyczips$n_rats / nyczips$n_rest
# AVERAGE RESTAURANT SCORE: score_avg
restscorezip = rest %>% group_by(ZIPCODE) %>% summarize(score_avg = mean(SCORE))
nyczips = nyczips %>% left_join(., restscorezip, by = c("id" = "ZIPCODE"))
# RATIOS: score_to_rat rat_to_score
# note: we decided to only use rat_to_score as the other one has too small vals
nyczips$score_to_rat = nyczips$score_avg / nyczips$n_rats
nyczips$rat_to_score = nyczips$n_rats / nyczips$score_avg
# POPULATION: population
# RATS PER CAPITA: rat_per_cap
pop = subset(pop, population != 0)
pop = subset(pop, select = c("zip", "population"))
pop$zip = as.character(pop$zip)
nyczips = nyczips %>% left_join(., pop, by = c("id" = "zip"))
nyczips$rat_per_cap = nyczips$n_rats / nyczips$population
tax = subset(tax, Zip.Code != 0 & Zip.Code != 99999)
tax$Zip.Code = as.character(tax$Zip.Code)
# this makes a tax rating (weighted average) score from 1 to 6, representing
# the dist. of wealth among the brackets
# NOTE FROM LIZ: this may be a really rarded way to get this info, open to other
# smarter ideas for representing tax data with one number
tax$tax_rating = (tax$X.1.under..25.000 +
2 * tax$X.25.000.under..50.000 +
3 * tax$X.50.000.under..75.000 +
4 * tax$X.75.000.under..100.000 +
5 * tax$X.100.000.under..200.000 +
6 * tax$X.200.000.or.more) / tax$Total
tax = subset(tax, select = c("Zip.Code", "tax_rating"))
nyczips = nyczips %>% left_join(., tax, by = c("id" = "Zip.Code"))
nyczips$rat_to_tax = nyczips$n_rats / nyczips$tax_rating
```
```{r}
#this is to graph population by zip code
popzip <- geojson_read('Raw/NYC_ZIPS.geojson', what = 'sp')
popzip <- tidy(popzip, region = "postalCode")
popzip$id = strtoi(popzip$id)
population = read.csv("Raw/nyc2022population.csv")
colnames(population)[1] = "id"
popzip = popzip %>% left_join(., population, by = "id")
```
```{r}
# SINGULAR VARIABLE ANALYSIS: # RATS, # RESTAURANTS, AVG RESTAURANT SCORE
ggplot() +
geom_polygon(data = nyczips, aes(x = long, y = lat, group = group, fill = n_rats)) +
theme_void() +
# scale_fill_gradient2(low = "darkblue", mid = "purple", high = "pink", midpoint=3000) +
coord_map() + labs(
title = "Rat Sightings by Zip Code",
fill = "# Rats"
)
ggplot() +
geom_polygon(data = nyczips, aes(x = long, y = lat, group = group, fill = n_rest)) +
theme_void() +
coord_map() + labs(
title = "Number of Restaurants by Zip Code",
fill = "# Rest"
)
ggplot() +
geom_polygon(data = nyczips, aes(x = long, y = lat, group = group, fill = score_avg)) +
theme_void() +
coord_map() + labs(
title = "Restaurant Scores by Zip Code",
fill = "Avg Score"
)
ggplot() +
geom_polygon(data = nyczips, aes(x = long, y = lat, group = group, fill = population)) +
theme_void() +
coord_map() + labs(
title = "Population by Zip Code",
fill = "Population"
)
ggplot() +
geom_polygon(data = nyczips, aes(x = long, y = lat, group = group, fill = tax_rating)) +
theme_void() +
coord_map() + labs(
title = "Tax Rating by Zip Code",
fill = "Tax Rating (from 1 to 6)"
)
```
```{r}
# rats per capita
ggplot() +
geom_polygon(data = nyczips, aes(x = long, y = lat, group = group, fill = rat_per_cap)) +
theme_void() +
scale_fill_gradient2(low = "#395184",
mid = "#A964B8",
high = "#FFA9A9", midpoint = 0.05) +
coord_map() + labs(
title = "Rats Per Capita",
fill = "Number of Rats / Population of Zip"
)
```
```{r}
# THROWN OUT: RESTAURANT TO RAT RATIOS
# ggplot() +
# geom_polygon(data = nyczips, aes(x = long, y = lat, group = group, fill = rest_to_rat)) +
# theme_void() +
# scale_fill_gradient2(low = "#395184",
# mid = "#A964B8",
# high = "#FFA9A9", midpoint = 200) +
# coord_map() + labs(
# title = "Restaurant to Rat Ratio by Zip Code",
# fill = "Number of Restaurants / Number of Rats"
# )
ggplot() +
geom_polygon(data = nyczips, aes(x = long, y = lat, group = group, fill = rat_to_rest)) +
theme_void() +
scale_fill_gradient2(low = "#395184",
mid = "#A964B8",
high = "#FFA9A9", midpoint = 4) +
coord_map() + labs(
title = "Rat to Restaurant Ratio by Zip Code",
fill = "Number of Rats / Number of Restaurants"
)
# THROWN OUT: SCORE TO RAT RATIOS
# ggplot() +
# geom_polygon(data = nyczips, aes(x = long, y = lat, group = group, fill = score_to_rat)) +
# theme_void() +
# scale_fill_gradient2(low = "#395184",
# mid = "#A964B8",
# high = "#FFA9A9", midpoint = 10) +
# coord_map() + labs(
# title = "Restaurant Score to Number of Rats by Zip Code",
# fill = "Average Restaurant Score / Number of Rats"
# )
ggplot() +
geom_polygon(data = nyczips, aes(x = long, y = lat, group = group, fill = rat_to_score)) +
theme_void() +
scale_fill_gradient2(low = "#395184",
mid = "#A964B8",
high = "#FFA9A9", midpoint = 150) +
coord_map() + labs(
title = "Rats to Restaurant Score Ratio by Zip Code",
subtitle = "higher score = worse restaurant",
fill = "Number of Rats / Average Restaurant Score"
)
# tax bracket stuff
ggplot() +
geom_polygon(data = nyczips, aes(x = long, y = lat, group = group, fill = rat_to_tax)) +
theme_void() +
scale_fill_gradient2(low = "#395184",
mid = "#A964B8",
high = "#FFA9A9", midpoint = 1500) +
coord_map() + labs(
title = "Rat to Tax Rating Ratio by Zip Code",
fill = "Number of Rats / Tax Rating (from 1 to 6)"
)
```
```{r}
# ggplotly(p)
zipout = subset(nyczips, select = -c(long, lat, order, hole,
piece, group))
colnames(zipout)[1] = "zipcode"
colnames(zipout)[3] = "borough"
colnames(zipout)[4] = "neighborhood"
zipout = zipout %>% distinct()
write.csv(zipout, file = "zipout.csv")
```
```{r}
#PCA
```