-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_MSHA.rmd
288 lines (191 loc) · 7.44 KB
/
mongo_MSHA.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
Analysis of MSHA Inspections, Violations, and Accident Data
========================================================
_An open and ongoing analysis of the data provided by the [US Mine Safety & Health Administration](http://www.msha.gov/OpenGovernmentData/OGIMSHA.asp). See [github.com/mwfrost/MSHA](https://github.com/mwfrost/MSHA) for details._
```{r}
require(reshape)
require(plyr)
require(ggplot2)
require(lubridate)
require(xtable)
require(scales)
require(rmongodb)
require(RMongo)
require(RJSONIO)
setwd('~/Code/MSHA/')
Sys.setlocale(locale="C")
```
## Load Data
### Load Mine Data
```{r}
mdat <- read.table('./data/msha_source/Mines.TXT', header=T, sep="|", fill=T, as.is=c(1:59),quote="\"")
t(head(mdat,3))
```
This script uses `rmongodb` to write the flat files to mongo collections, and uses `RMongo` to read them.
*rmongodb*
Write each source table to a mongodb collection
```{r mongo.write.mines}
data.frame.to.mongo <- function(df, db, col, fields=names(df)) {
mongo <- mongo.create()
if (mongo.is.connected(mongo)) {
for(i in 1:nrow(df)) {
b <- mongo.bson.from.list(df[i,fields])
#print(b)
#print('----------------')
if (!is.null(b)){
mongo.insert(mongo, paste(db, col, sep='.'), b)
}
else {
print('NULL BSON object')
print(t(df[i,]))
}
}
}
}
fields <- setdiff(names(mdat), 'DIRECTIONS_TO_MINE')
# data.frame.to.mongo(mdat, 'msha', 'mines', fields)
```
Now switch to the `RMongo` library and check the data. Some sample Mongo aggregations on the Mines collection, based on these examples:
http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/
http://docs.mongodb.org/manual/reference/sql-comparison/
Example mongodb search: `db.mines.find({MINE_ID : 1519137})`
```{r check.mines}
mongo <- mongoDbConnect(dbName="msha", host="localhost",port='27017')
output <- dbAggregate(mongo, "mines",
c('{
$match: { NO_EMPLOYEES: {$gt: 0} }
}'
,'{
$group: {
_id: "$PRIMARY_SIC",
employees : {$sum:"$NO_EMPLOYEES"}
}
}'
)
)
print(output)
output <- lapply(output, fromJSON)
output <- do.call(rbind, output)
output <- data.frame(output)
```
Compare the time it takes to run a simple aggregation in mongo to the same task with ddply()
```{r compare}
mongo <- mongoDbConnect(dbName="msha", host="localhost",port='27017')
system.time(
dbAggregate(mongo, "mines",
c('{ $match: { NO_EMPLOYEES: {$gt: 0} } }'
,'{ $group: { _id: "$PRIMARY_SIC", employees : {$sum:"$NO_EMPLOYEES"}} }'
)
)
)
system.time(
ddply(mdat[mdat$NO_EMPLOYEES > 0,], .(PRIMARY_SIC), summarize, employees=sum(NO_EMPLOYEES))
)
```
Sample rmongodb queries:
```{r rmongodb.samples}
mongo1 <- mongo.create()
mongo.count(mongo1, 'msha.mines', query=mongo.bson.empty())
```
Sample RMongo queries:
```{r rmongo.sample.queries}
dbGetQuery(mongo, 'mines', '{STATE: "WV", "COAL_METAL_IND" :"C"}')
# Robinson Run
dbGetQuery(mongo, 'mines', '{MINE_ID: 4601318}')
dbAggregate(mongo, "mines",
c(
'{
$group: {
_id: "$PRIMARY_SIC",
employees : {$sum:"$NO_EMPLOYEES"}
}
}'
)
)
```
Evantually add some utility functions
```{r mongo.functions}
```
### Inspections
Inspections.txt is too long to store in memory in R, so read pieces of it and load those into mongo.
```{r}
skip_count <- 250000
start_row <- skip_count + 1
idat <- read.table('./data/msha_source/Inspections.TXT', nrows=skip_count, header=T, sep="|", fill=T, quote="\"",comment.char = "")
inames <- names(idat)
while (start_row < 2000000) {
print(paste("About to scan records ", start_row, " through " , start_row + skip_count))
idat_temp <- read.table('./data/msha_source/Inspections.TXT', nrows=skip_count, header=F, sep="|", fill=T, skip = start_row ,quote="\"",comment.char = "")
names(idat_temp) <- inames
print(paste("Rows collected: " , nrow(idat)))
# data.frame.to.mongo(idat_temp, 'msha', 'inspections', inames)
start_row <- start_row + skip_count
}
mongo1 <- mongo.create()
mongo.count(mongo1, 'msha.inspections', query=mongo.bson.empty())
```
Compare the operation time
```{r compare.inspections}
system.time(
dbAggregate(mongo, "inspections",
c('{ $match: { CURRENT_CONTROLLER_NAME: "CONSOL Energy Inc" } }'
,'{ $group: { _id: "$CURRENT_MINE_NAME", inspections : {$sum:1}} }'
)
)
)
system.time(
ddply(
subset(idat, CURRENT_CONTROLLER_NAME == 'CONSOL Energy Inc'),
.(CURRENT_MINE_NAME) , nrow
)
)
```
### Accidents
```{r}
# adat <- read.table('./data/msha_source/Accidents.TXT', header=T, sep="|", fill=T, quote="\"",comment.char = "")
# data.frame.to.mongo(adat, 'msha', 'accidents')
mongo1 <- mongo.create()
mongo.count(mongo1, 'msha.accidents', query=mongo.bson.empty())
```
### Violations
```{r}
skip_count <- 250000
start_row <- skip_count + 1
# vdat <- read.table('./data/msha_source/Violations.TXT', nrows=skip_count, header=T, sep="|", fill=T, as.is=c(1:55), quote="\"",comment.char = "")
vnames <- names(vdat)
while (start_row < 2000000) {
print(paste("About to scan records ", start_row, " through " , start_row + skip_count))
vdat_temp <- read.table('./data/msha_source/Violations.TXT', nrows=skip_count, header=F, sep="|", fill=T, as.is=c(1:55), skip = start_row ,quote="",comment.char = "")
names(vdat_temp) <- vnames
# data.frame.to.mongo(vdat_temp, 'msha', 'violations')
print(paste("Rows collected: " , nrow(vdat)))
# print(paste("Events ", vdat[last_row,c("EVENT_NO")] , " through " , vdat[nrow(vdat),c("EVENT_NO")]))
start_row <- start_row + skip_count
# last_row <- nrow(vdat)
}
```
Check the violations data.
```{r check.violations}
mongo1 <- mongo.create()
mongo.count(mongo1, 'msha.violations', query=mongo.bson.empty())
```
Next steps:
- Reduce the data sets to only include mines that aren't abandoned
- For each accident, calculate the days since the previous accident at that mine
```{r since.last.accident}
adat.x <- subset(adat, MINE_ID %in% c(4601318) )
ddply(adat.x, .(MINE_ID),
```
- Normalize inspections, violations, and accidents into "event" objects.
- Add a time series for events to each mine object
Reference links:
[seanhess]: http://seanhess.github.io/2012/02/01/mongodb_relational.html
[mongodb]: http://docs.mongodb.org/manual/tutorial/query-documents/
[mongodb 2]: http://docs.mongodb.org/manual/reference/sql-comparison/
[mongodb 3]: http://docs.mongodb.org/manual/reference/method/db.collection.find/#db.collection.find
[mongodb 4]: http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/
[mongodb 5]: http://docs.mongodb.org/manual/reference/aggregation/operators/
[mongodb 6]: http://docs.mongodb.org/manual/tutorial/aggregation-examples/
[msha]: http://www.msha.gov/OpenGovernmentData/OGIMSHA.asp
[readthedocs]: https://media.readthedocs.org/pdf/a-little-book-of-r-for-time-series/latest/a-little-book-of-r-for-time-series.pdf
```{r denormalize}
```