forked from dgrtwo/data-screencasts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nyc-restaurants.Rmd
168 lines (137 loc) · 4.81 KB
/
nyc-restaurants.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
---
title: "NYC Restaurants"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
library(lubridate)
theme_set(theme_light())
# You can use this url to download the data directly into R (will take a few seconds)
restaurant_inspections_raw <- read_csv("https://data.cityofnewyork.us/api/views/43nn-pn8j/rows.csv")
restaurant_inspections <- restaurant_inspections_raw %>%
janitor::clean_names() %>%
select(-phone, -grade_date, -record_date, -building, -street) %>%
mutate(inspection_date = mdy(inspection_date)) %>%
separate(inspection_type, c("inspection_program", "inspection_type"), sep = " / ")
```
```{r}
restaurant_inspections %>%
count(dba, camis, sort = TRUE)
restaurant_inspections %>%
count(year = year(inspection_date))
restaurant_inspections %>%
count(grade, sort = TRUE)
restaurant_inspections %>%
count(violation_code, violation_description, sort = TRUE)
restaurant_inspections %>%
filter(camis == 41297769, inspection_date == "2018-09-25") %>%
count(camis, dba, inspection_date, sort = TRUE)
restaurant_inspections %>%
count(cuisine_description, sort = TRUE)
restaurant_inspections %>%
filter(action == "No violations were recorded at the time of this inspection.") %>%
count(critical_flag)
inspections <- restaurant_inspections %>%
group_by(camis,
dba,
boro,
zipcode,
cuisine_description,
inspection_date,
action,
score,
grade,
inspection_type,
inspection_program) %>%
summarize(critical_violations = sum(critical_flag == "Critical", na.rm = TRUE),
non_critical_violations = sum(critical_flag == "Not Critical", na.rm = TRUE)) %>%
ungroup()
most_recent_cycle_inspection <- inspections %>%
filter(inspection_program == "Cycle Inspection",
inspection_type == "Initial Inspection") %>%
arrange(desc(inspection_date)) %>%
distinct(camis, .keep_all = TRUE)
```
```{r}
by_dba <- most_recent_cycle_inspection %>%
group_by(dba, cuisine = cuisine_description) %>%
summarize(locations = n(),
avg_score = mean(score),
median_score = median(score)) %>%
ungroup() %>%
arrange(desc(locations))
by_dba %>%
mutate(locations_bin = cut(locations, c(0, 1, 3, 10, Inf), labels = c("1", "2-3", "3-10", ">10"))) %>%
ggplot(aes(locations_bin, avg_score + 1)) +
geom_boxplot() +
scale_y_log10()
```
```{r}
by_cuisine <- by_dba %>%
group_by(cuisine) %>%
summarize(avg_score = mean(avg_score),
median_score = median(avg_score),
restaurants = n()) %>%
arrange(desc(restaurants))
library(broom)
cuisine_conf_ints <- by_dba %>%
add_count(cuisine) %>%
filter(n > 100) %>%
nest(-cuisine) %>%
mutate(model = map(data, ~ t.test(.$avg_score))) %>%
unnest(map(model, tidy))
cuisine_conf_ints %>%
mutate(cuisine = str_remove(cuisine, " \\(.*"),
cuisine = fct_reorder(cuisine, estimate)) %>%
ggplot(aes(estimate, cuisine)) +
geom_point() +
geom_errorbarh(aes(xmin = conf.low,
xmax = conf.high)) +
labs(x = "Average inspection score (higher means more violations)",
y = "Type of cuisine",
title = "Average inspection score by type of cuisine in NYC",
subtitle = "Each restaurant chain was counted once based on its average score")
```
```{r}
violation_cuisine_counts <- restaurant_inspections %>%
semi_join(most_recent_cycle_inspection, by = c("camis", "inspection_date")) %>%
count(critical_flag, violation_code, violation_description, cuisine = cuisine_description) %>%
group_by(violation_code) %>%
mutate(violation_total = sum(n)) %>%
group_by(cuisine) %>%
mutate(cuisine_total = sum(n)) %>%
ungroup() %>%
filter(violation_total >= 1000,
cuisine_total >= 2000) %>%
group_by(violation_description) %>%
mutate(fraction = n / cuisine_total,
avg_fraction = mean(fraction)) %>%
ungroup()
violation_cuisine_counts %>%
mutate(relative_risk = fraction / avg_fraction) %>%
arrange(desc(relative_risk)) %>%
filter(str_detect(violation_description, "mice"))
```
### What violations tend to occur together?
```{r}
library(widyr)
violations <- restaurant_inspections %>%
semi_join(most_recent_cycle_inspection, by = c("camis", "inspection_date")) %>%
filter(!is.na(violation_description))
violations %>%
pairwise_cor(violation_description, camis, sort = TRUE)
principal_components <- violations %>%
mutate(value = 1) %>%
widely_svd(violation_description, camis, value, nv = 6)
principal_components %>%
filter(dimension == 2) %>%
top_n(10, abs(value)) %>%
mutate(violation_description = str_sub(violation_description, 1, 60),
violation_description = fct_reorder(violation_description, value)) %>%
ggplot(aes(violation_description, value)) +
geom_col() +
coord_flip()
```