-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data_Analysis.R
379 lines (296 loc) · 16.7 KB
/
Data_Analysis.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
library(mongolite) # Added for mongo connectivity
library(lubridate)
library(plyr)
library(ggplot2)
library(ggmap) # Added for maps
accident <- mongo(collection = "ACCIDENT",db = "ACCIDENTDB",url = "mongodb://localhost",verbose = FALSE)
road_surface_cond <- mongo(collection = "ROAD_SURFACE_COND",db = "ACCIDENTDB",url = "mongodb://localhost",verbose = FALSE)
atmospheric_cond <- mongo(collection = "ATMOSPHERIC_COND",db = "ACCIDENTDB",url = "mongodb://localhost",verbose = FALSE)
accident_node <- mongo(collection = "ACCIDENT_NODE",db = "ACCIDENTDB",url = "mongodb://localhost",verbose = FALSE)
setwd("/Users/blackbox/Documents/Misc/Datasets/ACCIDENT/")
vehicle_df <- read.csv("VEHICLE.csv")
accident_dataframe <- accident$find()
accident_node_df <- accident_node$find()
atmospheric_cond_df <- atmospheric_cond$find()
road_surface_cond_df <- road_surface_cond$find()
accident_dataframe_new <- accident_dataframe
write.csv(accident_dataframe_new, file = "AccidentFreqAll.csv")
# Adding a year column to the Accident data frame
accident_dataframe$ACCIDENTYEAR <- year(as.Date(accident_dataframe$ACCIDENTDATE, format = '%d/%m/%Y'))
# Accident taken between 2007 and 2016
accident_dataframe <- subset(accident_dataframe,accident_dataframe$ACCIDENTYEAR != 2006 & accident_dataframe$ACCIDENTYEAR != 2017)
accidentAnalysis <- function()
{
AccidentTrend <- count(accident_dataframe$ACCIDENTYEAR)
Years <- c(AccidentTrend$x)
ggplot(AccidentTrend, aes(x = AccidentTrend$x, y = AccidentTrend$freq)) +
geom_line( colour = "red") +
ggtitle("Accident Trend in Victoria") +
xlab(" Year ") +
ylab("Accident Count") +
theme(plot.title = element_text(face = "bold", hjust = 0.5),
axis.title = element_text(face = "bold", color = "black"),
axis.text.x = element_text(face = "bold", size = 9),
axis.text.y = element_text(face = "bold", size = 8))+
scale_x_discrete(limits=Years)
write.csv(AccidentTrend, file = "AccidentFreq.csv")
}
AccidentSeverityAnalysis <- function()
{
AccidentSeverityFreq <- count(accident_dataframe$SEVERITY)
names <- c("Fatal accident",
"Serious injury accident",
"Other injury accident",
"Non injury accident")
ggplot(AccidentSeverityFreq, aes(x,freq, label = freq))+
geom_bar(stat = "identity", fill = "steelblue", width = 0.5) +
geom_text(vjust = -0.5, color = "black") +
labs(title = "Accident Severity", x = "Severity Level", y = "Accident Count") +
theme(title = element_text(face = "bold", color = "black"),
axis.title = element_text(face = "bold", color = "black"),
axis.text.x = element_text(face = "bold",size = 8),
axis.text.y = element_text(face = "bold", size = 8),
plot.title = element_text(hjust = 0.5)) +
scale_x_discrete(limits=names,labels=names)
write.csv(AccidentSeverityFreq, file = "AccidentSeverityFreq.csv")
}
AccidentVariableAnalysis <- function()
{
merged <- merge(accident_dataframe, accident_node_df, by=c("ACCIDENT_NO"))
# Removing the invalid values
merged <- subset(merged, merged$SPEED_ZONE != 777 | merged$SPEED_ZONE != 888 | merged$SPEED_ZONE != 999)
#Merging atmospheric condition with Accident and Node datasets
merged <- merge(merged, atmospheric_cond_df, by=c("ACCIDENT_NO"))
#Merging All the tables
merged <- merge(merged,road_surface_cond_df,by=c("ACCIDENT_NO"))
install.packages("randomForest")
library(party)
library(randomForest)
AccidentImportanceAnalysis <- merged[sample(nrow(merged),10000), ]
AccidentImportanceAnalysis$SEVERITY <- as.factor(AccidentImportanceAnalysis$SEVERITY)
AccidentImportanceAnalysis$SPEED_ZONE <- as.factor(AccidentImportanceAnalysis$SPEED_ZONE)
AccidentImportanceAnalysis$ACCIDENT_TYPE <- as.factor(AccidentImportanceAnalysis$ACCIDENT_TYPE)
AccidentImportanceAnalysis$LIGHT_CONDITION <- as.factor(AccidentImportanceAnalysis$LIGHT_CONDITION)
AccidentImportanceAnalysis$ROAD_GEOMETRY <- as.factor(AccidentImportanceAnalysis$ROAD_GEOMETRY)
AccidentImportanceAnalysis$ATMOSPH_COND <- as.factor(AccidentImportanceAnalysis$ATMOSPH_COND)
output.forest <- randomForest(SEVERITY ~ LIGHT_CONDITION +
ATMOSPH_COND +
ROAD_GEOMETRY+
SPEED_ZONE +
ACCIDENT_TYPE,
data = AccidentImportanceAnalysis)
print(output.forest)
print(importance(output.forest,type = 2))
}
VehicleTypeAnalysis <- function()
{
vehicleFreq <- count(vehicle_df$Vehicle.Type.Desc)
ggplot(vehicleFreq, aes(x,freq, label = freq)) +
geom_bar(stat = "identity", fill = "steelblue", width = 0.5) +
geom_text(vjust = -0.5, color = "black") +
labs(title = "Light Condition Frequency", x = "Light Condition types", y = "Frequency") +
theme(title = element_text(face = "bold", color = "black"),
axis.title = element_text(face = "bold", color = "black"),
axis.text.x = element_text(face = "bold",angle = 60, hjust=1, size = 8),
axis.text.y = element_text(face = "bold", size = 8),
plot.title = element_text(hjust = 0.5)) +
scale_x_discrete(limits=vehicleFreq$x,labels=vehicleFreq$x)
}
LightCconditionAnalysis <- function()
{
LighConditionFreq <- count(accident_dataframe$LIGHT_CONDITION)
names <- c("Day",
"Dusk/Dawn",
"Dark street lights on",
"Dark Street lights off",
"Dark no street lights",
"Dark no street unknown")
ggplot(LighConditionFreq, aes(x,freq, label = freq)) +
geom_bar(stat = "identity", fill = "steelblue", width = 0.5) +
geom_text(vjust = -0.5, color = "black") +
coord_cartesian(xlim=c(1,6)) +
labs(title = "Light Condition Frequency", x = "Light Condition types", y = "Frequency") +
theme(title = element_text(face = "bold", color = "black"),
axis.title = element_text(face = "bold", color = "black"),
axis.text.x = element_text(face = "bold",angle = 60, hjust=1, size = 8),
axis.text.y = element_text(face = "bold", size = 8),
plot.title = element_text(hjust = 0.5)) +
scale_x_discrete(limits=names,labels=names)
write.csv(LighConditionFreq, file = "LighConditionFreq.csv")
# Considering all accident that happened when there it was dark and No Street light
DarkNoStreetLight <- subset(accident_dataframe,accident_dataframe$LIGHT_CONDITION == 5)
Merged_NoLight_Node <- merge(DarkNoStreetLight, accident_node_df, by=c("ACCIDENT_NO"))
Merged_NoLight_Node <- unique( Merged_NoLight_Node[ , ] )
Merged_NoLight_Node_vehicle <- merge(Merged_NoLight_Node, vehicle_df, by=c("ACCIDENT_NO"))
Merged_NoLight_Node_vehicle <- unique( Merged_NoLight_Node_vehicle[ , ] )
write.csv(Merged_NoLight_Node_vehicle, file = "NoLightAccidentAllVehicle.csv")
postcodeNoLight <- count(Merged_NoLight_Node_vehicle$POSTCODE)
write.csv(postcodeNoLight, file = "PostcodewiseNoLight.csv")
# Top 5 suburbs with Accident count during no street light
TopAccidents <- postcodeNoLight[order(postcodeNoLight$freq,decreasing=T)[1:5],]
}
BicycleAccidentAnalysis <- function()
{
BicycleVehicleData <- subset (vehicle_df, vehicle_df$VEHICLE_TYPE == 13)
write.csv(BicycleVehicleData, file = "BicycleAccident.csv")
FatalAccident <- subset(accident_dataframe, accident_dataframe$SEVERITY == 1 | accident_dataframe$SEVERITY == 2)
Merged <- merge(FatalAccident, BicycleVehicleData, by=c("ACCIDENT_NO"))
Merged <- merge(accident_node_df, Merged, by=c("ACCIDENT_NO"))
#Merged has all Fatal and serious accidents for Bicycle users
Merged <- unique( Merged[ , ] )
write.csv(Merged, file = "BicyleSeriousAccidents.csv")
BicycleAccidentType <- count(Merged$ACCIDENT_TYPE)
write.csv(BicycleAccidentType, file = "BicycleAccidentType.csv")
# We consider only collision with vehicle
Merged_VehilceCollision <- subset(Merged, Merged$ACCIDENT_TYPE==1)
PostcodeWiseBicycleAccident <- count(Merged_VehilceCollision$POSTCODE)
write.csv(PostcodeWiseBicycleAccident, file = "PostcodeWiseBicycleAccident.csv")
#Postcodes 3000, 3121, 3053, 3182, 3065
TopAccidents <- PostcodeWiseBicycleAccident[order(PostcodeWiseBicycleAccident$freq,decreasing=T)[1:10],]
print(TopAccidents)
#Finding all bicycle accidents that killed
Merged_bicycle_Killed <- subset(Merged, Merged$NO_PERSONS_KILLED > 0)
write.csv(Merged_bicycle_Killed, file = "BicycleAccidents_Killed.csv")
#****************************Locating Bicycle Accidents for 3000**************************
Merged_3000 <- subset(Merged_VehilceCollision, Merged_VehilceCollision$POSTCODE == 3000)
write.csv(Merged_3000, file = "3000_BicycleAccidents.csv")
maps <- get_map(location = c(lon = mean(Merged_3000$LONGITUDE), lat = mean(Merged_3000$LATITUDE)),
zoom = 15,
maptype = "roadmap",
scale = 2)
ggmap(maps) +
geom_point(data = Merged_3000, aes(x = Merged_3000$LONGITUDE, y = Merged_3000$LATITUDE,
fill = "Black", alpha = 1),
size = 3,
shape = 20) +
guides(fill=FALSE, alpha=FALSE, size=FALSE) +
labs(title = "Postcode 3000 Bicycle Accident locations", x = "Longitude", y = "Latitude") +
theme(title = element_text(face = "bold", color = "black"),
axis.title = element_text(face = "bold", color = "black"),
axis.text.x = element_text(face = "bold", size = 8),
axis.text.y = element_text(face = "bold", size = 8),
plot.title = element_text(hjust = 0.5))
#********************************************************************************
#****************************Locating Bicycle Accidents for 3121**************************
Merged_3121 <- subset(Merged_VehilceCollision, Merged_VehilceCollision$POSTCODE == 3121)
write.csv(Merged_3121, file = "3121_BicycleAccidents.csv")
maps <- get_map(location = c(lon = mean(Merged_3121$LONGITUDE), lat = mean(Merged_3121$LATITUDE)),
zoom = 14,
maptype = "roadmap",
scale = 2)
ggmap(maps) +
geom_point(data = Merged_3121, aes(x = Merged_3121$LONGITUDE, y = Merged_3121$LATITUDE, fill = "red", alpha = 0.8),
size = 2,
shape = 20) +
guides(fill=FALSE, alpha=FALSE, size=FALSE) +
labs(title = "Postcode 3121 Bicycle Accident locations", x = "Longitude", y = "Latitude") +
theme(title = element_text(face = "bold", color = "black"),
axis.title = element_text(face = "bold", color = "black"),
axis.text.x = element_text(face = "bold", size = 8),
axis.text.y = element_text(face = "bold", size = 8),
plot.title = element_text(hjust = 0.5))
#********************************************************************************
#****************************Locating Bicycle Accidents for 3053**************************
Merged_3053 <- subset(Merged_VehilceCollision, Merged_VehilceCollision$POSTCODE == 3053)
maps <- get_map(location = c(lon = mean(Merged_3053$LONGITUDE), lat = mean(Merged_3053$LATITUDE)),
zoom = 15,
maptype = "roadmap",
scale = 2)
ggmap(maps) +
geom_point(data = Merged_3053, aes(x = Merged_3053$LONGITUDE, y = Merged_3053$LATITUDE,
fill = "Black", alpha = 0.8),
size = 2,
shape = 20) +
guides(fill=FALSE, alpha=FALSE, size=FALSE) +
labs(title = "Postcode 3053 Bicycle Accident locations", x = "Longitude", y = "Latitude") +
theme(title = element_text(face = "bold", color = "black"),
axis.title = element_text(face = "bold", color = "black"),
axis.text.x = element_text(face = "bold", size = 8),
axis.text.y = element_text(face = "bold", size = 8),
plot.title = element_text(hjust = 0.5))
#********************************************************************************
#****************************Locating Bicycle Accidents for 3182**************************
Merged_3182 <- subset(Merged_VehilceCollision, Merged_VehilceCollision$POSTCODE == 3182)
maps <- get_map(location = c(lon = mean(Merged_3182$LONGITUDE), lat = mean(Merged_3182$LATITUDE)),
zoom = 14,
maptype = "roadmap",
scale = 2)
ggmap(maps) +
geom_point(data = Merged_3182, aes(x = Merged_3182$LONGITUDE, y = Merged_3182$LATITUDE,
fill = "red", alpha = 0.8),
size = 2,
shape = 21) +
guides(fill=FALSE, alpha=FALSE, size=FALSE) +
labs(title = "Postcode 3182 Bicycle Accident locations", x = "Longitude", y = "Latitude") +
theme(title = element_text(face = "bold", color = "black"),
axis.title = element_text(face = "bold", color = "black"),
axis.text.x = element_text(face = "bold", size = 8),
axis.text.y = element_text(face = "bold", size = 8),
plot.title = element_text(hjust = 0.5))
#********************************************************************************
#****************************Locating Bicycle Accidents for 3065**************************
Merged_3065 <- subset(Merged_VehilceCollision, Merged_VehilceCollision$POSTCODE == 3065)
maps <- get_map(location = c(lon = mean(Merged_3065$LONGITUDE), lat = mean(Merged_3065$LATITUDE)),
zoom = 14,
maptype = "roadmap",
scale = 2)
ggmap(maps) +
geom_point(data = Merged_3065, aes(x = Merged_3065$LONGITUDE, y = Merged_3065$LATITUDE,
fill = "red", alpha = 0.8),
size = 2,
shape = 21) +
guides(fill=FALSE, alpha=FALSE, size=FALSE) +
labs(title = "Postcode 3065 Bicycle Accident locations", x = "Longitude", y = "Latitude") +
theme(title = element_text(face = "bold", color = "black"),
axis.title = element_text(face = "bold", color = "black"),
axis.text.x = element_text(face = "bold", size = 8),
axis.text.y = element_text(face = "bold", size = 8),
plot.title = element_text(hjust = 0.5))
#********************************************************************************
#****************************Locating Bicycle Accidents for 3186**************************
Merged_3186 <- subset(Merged_VehilceCollision, Merged_VehilceCollision$POSTCODE == 3186)
write.csv(Merged_3186, file = "3186_Accidents.csv")
maps <- get_map(location = c(lon = mean(Merged_3186$LONGITUDE), lat = mean(Merged_3186$LATITUDE)),
zoom = 14,
maptype = "roadmap",
scale = 2)
ggmap(maps) +
geom_point(data = Merged_3186, aes(x = Merged_3186$LONGITUDE, y = Merged_3186$LATITUDE,
fill = "Black", alpha = 1),
size = 3,
shape = 20) +
guides(fill=FALSE, alpha=FALSE, size=FALSE) +
labs(title = "Postcode 3186 Bicycle Accident locations", x = "Longitude", y = "Latitude") +
theme(title = element_text(face = "bold", color = "black"),
axis.title = element_text(face = "bold", color = "black"),
axis.text.x = element_text(face = "bold", size = 8),
axis.text.y = element_text(face = "bold", size = 8),
plot.title = element_text(hjust = 0.5))
#********************************************************************************
#****************************Locating Bicycle Accidents for 3195**************************
Merged_3195 <- subset(Merged_VehilceCollision, Merged_VehilceCollision$POSTCODE == 3195)
write.csv(Merged_3195, file = "3121_BicycleAccidents.csv")
maps <- get_map(location = c(lon = mean(Merged_3195$LONGITUDE), lat = mean(Merged_3195$LATITUDE)),
zoom = 14,
maptype = "roadmap",
scale = 2)
ggmap(maps) +
geom_point(data = Merged_3195, aes(x = Merged_3195$LONGITUDE, y = Merged_3195$LATITUDE,
fill = "Black", alpha = 1),
size = 3,
shape = 20) +
guides(fill=FALSE, alpha=FALSE, size=FALSE) +
labs(title = "Postcode 3195 Bicycle Accident locations", x = "Longitude", y = "Latitude") +
theme(title = element_text(face = "bold", color = "black"),
axis.title = element_text(face = "bold", color = "black"),
axis.text.x = element_text(face = "bold", size = 8),
axis.text.y = element_text(face = "bold", size = 8),
plot.title = element_text(hjust = 0.5))
#********************************************************************************
}
accidentAnalysis()
AccidentSeverityAnalysis()
AccidentVariableAnalysis()
BicycleAccidentAnalysis()
LightCconditionAnalysis()
VehicleTypeAnalysis()