Browse Source

Exploratory data analysis

Petra Lamborn 4 years ago
parent
commit
3e818c994e
4 changed files with 10 additions and 1 deletions
  1. BIN
      Missing2018.png
  2. BIN
      OpenRoadSHSeverity.png
  3. BIN
      Severity.png
  4. 10
    1
      expl.R

BIN
Missing2018.png View File


BIN
OpenRoadSHSeverity.png View File


BIN
Severity.png View File


+ 10
- 1
expl.R View File

1
 # Exploration on data
1
 # Exploration on data
2
+# Export images 700x400
2
 
3
 
3
 # Libraries
4
 # Libraries
4
 library(ggplot2)
5
 library(ggplot2)
5
 library(dplyr)
6
 library(dplyr)
6
 library(RColorBrewer)
7
 library(RColorBrewer)
8
+library(scales)
7
 
9
 
8
 # Load data from Rdata file
10
 # Load data from Rdata file
9
 load("crashdata.Rdata")
11
 load("crashdata.Rdata")
38
 
40
 
39
 # Graph crash severity
41
 # Graph crash severity
40
 ggplot(dat) + geom_bar(aes(x=CRASH_SEV), fill=NA, col="black") +
42
 ggplot(dat) + geom_bar(aes(x=CRASH_SEV), fill=NA, col="black") +
41
-  theme_classic() + labs(x = "Crash severity", y = "Number of crashes")
43
+  theme_classic() + labs(x = "Crash severity", y = "Number of crashes") +
44
+  scale_y_continuous(labels = scales::comma)
42
 
45
 
43
 # Not all openroad crashes are on state highways
46
 # Not all openroad crashes are on state highways
44
 table(dat$URBAN, dat$CRASH_SH_DESC)
47
 table(dat$URBAN, dat$CRASH_SH_DESC)
60
                       labels = c("Single vehicle", "All other categories")) +
63
                       labels = c("Single vehicle", "All other categories")) +
61
   scale_x_discrete("Severity", labels = c("Not", "Moderate", "Severe", "Fatal")) +
64
   scale_x_discrete("Severity", labels = c("Not", "Moderate", "Severe", "Fatal")) +
62
   theme_classic() + theme(legend.position = "right")
65
   theme_classic() + theme(legend.position = "right")
66
+
67
+rurtype <- as.data.frame.matrix(table(rurhwy$CRASH_SEV, rurhwy$MULTI_VEH))
68
+rurtypefrac <- rurtype / rowSums(rurtype)
69
+rurtypefrac["Single vehicle"]
70
+
71
+colSums(rurtype)/sum(rurtype)