-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathosha-fatality-inspections.Rmd
322 lines (238 loc) · 8.82 KB
/
osha-fatality-inspections.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
---
title: "OSHA fatality inspections analysis"
output:
html_notebook:
toc: true
toc_float: true
---
# Setup
```{r setup, echo=FALSE, message=FALSE}
library(tidyverse)
library(lubridate)
```
## Download the inspections data
```{r download inspections data, warning=FALSE}
file_date <- str_remove_all(Sys.Date(), "-") # Set file date
data_path <- "data"
data_dir <- "osha_inspections"
data_url <- "https://enfxfr.dol.gov/data_catalog/OSHA"
data_file <- paste0("osha_inspection_", file_date, ".csv.zip")
if (!file.exists(file.path(data_path, data_dir))) {
dir.create(data_path)
download.file(paste(data_url, data_file, sep = "/"),
file.path(data_path, data_file))
unzip(file.path(data_path, data_file),
exdir = file.path(data_path, data_dir))
}
```
## Import the inspections data
Read in the data, converting non-date columns to character type to minimize data type errors.
```{r import inspections data}
inspections <-
list.files(
path = file.path(data_path, data_dir),
pattern = "*.csv",
full.names = TRUE
) %>%
map_dfr(
read_csv,
col_types = cols(
.default = "c",
open_date = col_date(),
case_mod_date = col_date(),
close_conf_date = col_date(),
close_case_date = col_date()
)
)
count(inspections)
```
## Filter to just OSHA-inspected workplaces
A five as the third digit of the reporting ID indicates a state office inspected the worksite.
```{r filter to just inspections conducted by OSHA}
state_office_indicator <- "5"
osha_inspections <- inspections %>%
filter(str_sub(reporting_id, 3, 3) != state_office_indicator)
count(osha_inspections)
```
# Analysis
## How many safety inspections did OSHA conduct during the first three years of Trump's presidency?
```{r trump inspections}
safety <- "S"
trump_begin_date <- "2017-01-20"
trump_end_date <- "2020-01-20"
trump_inspections <- osha_inspections %>%
filter(safety_hlth == safety,
open_date >= trump_begin_date,
open_date < trump_end_date,
insp_scope != "D") # no non-inspections
count(trump_inspections)
```
## How many safety inspections were conducted during the last three years of Obama's presidency?
```{r obama inspections}
obama_begin_date <- "2014-01-20"
obama_inspections <- osha_inspections %>%
filter(safety_hlth == safety,
open_date >= obama_begin_date,
open_date < trump_begin_date,
insp_scope != "D") # no non-inspections
count(obama_inspections)
```
## What's the difference?
```{r trump-obama difference}
(
count(trump_inspections) - count(obama_inspections)
) / count(obama_inspections) * 100
```
## What does this look like over time?
```{r inspections by month, warning=FALSE}
inspections_by_month <- osha_inspections %>%
filter(safety_hlth == safety &
open_date >= obama_begin_date &
open_date < trump_end_date &
insp_scope != "D") %>% # no non-inspections
group_by(month = floor_date(open_date, unit = "month")) %>%
summarize(count = n())
ggplot(inspections_by_month, aes(x = month, y = count)) +
geom_col()
# Export this data
write_csv(inspections_by_month, "data/exported/inspections_by_month.csv")
ggsave("data/exported/inspections_by_month.png")
```
## Filter the OSHA inspections to only fatality/catastrophe safety inspections
```{r calculate the number of fatality/catastrophe inspections each year}
fat_cat <- "M"
fat_cat_inspections <- osha_inspections %>%
filter(safety_hlth == safety, insp_type == fat_cat)
count(fat_cat_inspections)
```
## How many fat/cat safety inspections did OSHA conduct during the first 3.5 years of Trump's presidency?
```{r}
trump_fat_cat_inspections <- fat_cat_inspections %>%
filter(open_date >= trump_begin_date,
open_date < "2020-07-20")
count(trump_fat_cat_inspections)
```
## Fat/cat inspections grouped by year
```{r fat/cat inspections by year, warning=FALSE}
fat_cat_inspections_by_year <- fat_cat_inspections %>%
group_by(year = year(open_date)) %>%
summarize(num_fatality_inspections = n()) %>%
arrange(desc(year))
fat_cat_inspections_by_year
```
## When was the first fat/cat inspection reported?
```{r}
fat_cat_inspections %>% summarize(min(open_date))
```
## Fat/cat inspections charted by year
```{r fat/cat by year chart}
ggplot(fat_cat_inspections_by_year,
aes(x = year, y = num_fatality_inspections)) +
geom_col()
```
## How many workplaces received any sort of OSHA safety inspection (including fat/cat inspections) when grouped by address and inspection date?
```{r}
osha_inspections_w_workplace <- osha_inspections %>%
filter(safety_hlth == safety) %>%
mutate(estab_address = toupper(str_replace_all(paste0(
site_address, site_zip
), " ", "")))
n_distinct(osha_inspections_w_workplace %>%
select(estab_address, open_date))
```
## How many workplaces received an OSHA safety fat/cat inspection when grouped by address and inspection date?
```{r calculate the number of workplaces that witnessed a fatality/catastrophic safety accident since January 1, 2013 for which OSHA conducted a prior safety inspection in the decade preceding the accident}
accident_begin_date <- "2013-01-01"
osha_fatality_inspections_w_workplace <- osha_inspections_w_workplace %>%
filter(insp_type == fat_cat, open_date >= accident_begin_date)
n_distinct(osha_fatality_inspections_w_workplace %>%
select(estab_address, open_date))
```
## Join the data frames to see every safety inspection (including fat/cat inspections) OSHA conducted prior to each workplace's catastrophic/fatal accidents.
```{r}
osha_inspected_prior <-
inner_join(osha_fatality_inspections_w_workplace,
osha_inspections_w_workplace,
by = "estab_address",
suffix = c("_fatality", "_all")) %>%
mutate(
years_between = time_length(open_date_fatality - open_date_all, "year"),
prior_inspection = years_between < 10
) %>%
filter(open_date_fatality > open_date_all)
```
## And how many of these catastrophic/fatal accidents were there?
```{r}
n_distinct(osha_inspected_prior %>%
select(estab_address, open_date_fatality))
```
## What does this look like for one address?
```{r}
osha_inspected_prior %>%
filter(estab_address == "102-0166THROADFORESTHILLSNY11375") %>%
select(estab_address, safety_hlth_fatality, safety_hlth_all, insp_type_fatality, insp_type_all, open_date_fatality, open_date_all, years_between, prior_inspection) %>%
arrange(desc(open_date_fatality), desc(open_date_all))
osha_inspections %>%
filter(site_address == "102-01 66TH ROAD" & site_city == "FOREST HILLS" & site_state == "NY" & site_zip == "11375" & safety_hlth == "S" & insp_type == "M" & open_date >= "2013-01-01") %>%
arrange(desc(open_date))
osha_inspections %>%
filter(site_address == "102-01 66TH ROAD" & site_city == "FOREST HILLS" & site_state == "NY" & site_zip == "11375" & safety_hlth == "S") %>%
arrange(desc(open_date))
```
## And how many of these catastrophic/fatal accidents were preceded by an inspection in the prior 10 years?
```{r}
n_distinct(osha_inspected_prior %>%
filter(prior_inspection == TRUE,
insp_scope_all != "D") %>%
select(estab_address, open_date_fatality))
```
## How many inspections has OSHA conducted since COVID began?
That is, since the White House declared a national emergency on 3/13.
```{r}
covid_emergency_declared_date <- "2020-03-13"
covid_inspections <- osha_inspections %>%
filter(open_date >= covid_emergency_declared_date,
insp_scope != "D")
count(covid_inspections)
```
## How many inspections did OSHA conduct in the same period a year earlier?
```{r}
year_before_covid_emergency <- "2019-03-13"
year_before_covid_end_date <- "2019-08-06"
year_ago_covid_inspections <- osha_inspections %>%
filter(open_date >= year_before_covid_emergency,
open_date < year_before_covid_end_date,
insp_scope != "D")
count(year_ago_covid_inspections)
```
## And what's been the change?
```{r}
(
count(covid_inspections) - count(year_ago_covid_inspections)
) / count(year_ago_covid_inspections) * 100
```
## How many health inspections has OSHA conducted since COVID began?
That is, since the White House declared a national emergency on 3/13.
```{r}
health <- "H"
covid_inspections_health <- osha_inspections %>%
filter(open_date >= covid_emergency_declared_date,
safety_hlth == health,
insp_scope != "D")
count(covid_inspections_health)
```
## How many health inspections did OSHA conduct in the same period a year earlier?
```{r}
year_ago_covid_inspections_health <- osha_inspections %>%
filter(open_date >= year_before_covid_emergency,
open_date < year_before_covid_end_date,
safety_hlth == health,
insp_scope != "D")
count(year_ago_covid_inspections_health)
```
## And what's been the change?
```{r}
(
count(covid_inspections_health) - count(year_ago_covid_inspections_health)
) / count(year_ago_covid_inspections_health) * 100
```