Browse Source

Exploratory analysis

Petra Lamborn 4 years ago
parent
commit
6d418ecf3d
1 changed files with 47 additions and 1 deletions
  1. 47
    1
      expl.R

+ 47
- 1
expl.R View File

@@ -2,6 +2,8 @@
2 2
 
3 3
 # Libraries
4 4
 library(ggplot2)
5
+library(dplyr)
6
+library(RColorBrewer)
5 7
 
6 8
 # Load data from Rdata file
7 9
 load("crashdata.Rdata")
@@ -9,8 +11,52 @@ load("crashdata.Rdata")
9 11
 # Alternative way to load data
10 12
 #dat <- read.csv("finaldata_201809.csv")
11 13
 
14
+# Relevel crash severity
15
+dat$CRASH_SEV <- factor(dat$CRASH_SEV, levels = c("N", "M", "S", "F"))
16
+
12 17
 str(dat)
13 18
 
14 19
 yeartab <- table(dat$CRASH_YEAR)
20
+mpy <- mean(yeartab)
21
+
22
+# Obviously, data missing from 2018
23
+ggplot(dat) + geom_bar(aes(x=CRASH_YEAR), fill=NA, col="black") +
24
+  geom_hline(yintercept = mpy, linetype="dashed") + theme_classic() +
25
+  labs(x = "Year", y = "Number of crashes")
26
+table(dat$CRASH_FIN_YEAR)
27
+
28
+# Check that severity is given as F if and only if nonzero number of fatalities
29
+table(dat$CRASH_SEV, dat$FATAL_COUNT)
30
+
31
+# Look at number of vehicles involved
32
+levels(dat$MULTI_VEH)
33
+table(dat$MULTI_VEH, dat$CRASH_SEV)
34
+
35
+# Open road vs vehicles
36
+vehtab <- table(dat$MULTI_VEH, dat$URBAN, dat$CRASH_SEV == "F")
37
+vehtab
38
+
39
+# Graph crash severity
40
+ggplot(dat) + geom_bar(aes(x=CRASH_SEV), fill=NA, col="black") +
41
+  theme_classic() + labs(x = "Crash severity", y = "Number of crashes")
42
+
43
+# Not all openroad crashes are on state highways
44
+table(dat$URBAN, dat$CRASH_SH_DESC)
45
+
46
+# Motorcycle crash severity
47
+table(dat$CRASH_SEV, dat$MOTOR_CYCLE > 0)
48
+
49
+# Just look at fatal crashes on the open road on state highways
50
+rurhwy <- dat %>% filter(URBAN == "Openroad", CRASH_SH_DESC == "Yes")
51
+frurhwy <- rurhwy %>% filter(CRASH_SEV == "F")
52
+
53
+str(frurhwy)
54
+
55
+knitr::kable(table(rurhwy$MULTI_VEH, rurhwy$CRASH_SEV))
15 56
 
16
-ggplot(dat) + geom_bar(aes(x=CRASH_YEAR))
57
+ggplot(rurhwy) + 
58
+  geom_bar(aes(fill = MULTI_VEH != "Single vehicle", x = CRASH_SEV), position="dodge") +
59
+  scale_fill_brewer("Vehicles involved", type="qual", palette = "Dark2",
60
+                      labels = c("Single vehicle", "All other categories")) +
61
+  scale_x_discrete("Severity", labels = c("Not", "Moderate", "Severe", "Fatal")) +
62
+  theme_classic() + theme(legend.position = "right")