Washington Rural Interstate

Subasish Das and Chaolun Ma

2018-11-10

Washington Conflated Data (Interstate Roadways)

library(data.table)
library(dplyr)
library(wavelets)
library(tidyr)
library(naniar)
library(stringr)
library(ggplot2)
library(DT)
library(lubridate)
library(ggpubr)



setwd("/scratch/user/cma16/Task4_Deliverable2/Process4/AllCrash/FacilityBased/")
load("WA_Rural_Interstate_41_TMC_TT_SI_reduce_withCrash.rData")
mytype = 'RI'
setwd(paste0("/scratch/user/cma16/Task4_Deliverable2/Process4/AllCrash/FacilityBased/",mytype))

df_RI <- b02a
dim(df_RI)
## [1] 9303840      64
### Calculating Speed
df_RI$spd_av = 3600*df_RI$DISTANCE/df_RI$Travel_TIME_ALL_VEHICLES
df_RI$spd_pv = 3600*df_RI$DISTANCE/df_RI$Travel_TIME_PASSENGER_VEHICLES
df_RI$spd_ft = 3600*df_RI$DISTANCE/df_RI$Travel_TIME_FREIGHT_TRUCKS


### Month, Day
df_RI$date <- as.character(df_RI$DATE)
df_RI$date <- str_pad(df_RI$DATE, 8, pad = "0")
df_RI$Month <- substr(df_RI$date, start = 1, stop = 2)
df_RI$Day   <- substr(df_RI$date, start = 3, stop = 4)
df_RI$Year  <- substr(df_RI$date, start = 5, stop = 8)

head(df_RI,2)
##           TimeStamp       TMC V1    DATE EPOCH15 Travel_TIME_ALL_VEHICLES
## 1: 114N04098_0101_0 114N04098  1 1012015       0                       NA
## 2: 114N04098_0101_1 114N04098  2 1012015       1                      222
##    Travel_TIME_PASSENGER_VEHICLES Travel_TIME_FREIGHT_TRUCKS NP ADMIN_LEVE
## 1:                             NA                         NA  N        USA
## 2:                             NA                        222  N        USA
##    ADMIN_LE_1 ADMIN_LE_2 DISTANCE ROAD_NUMBE ROAD_NAME LATITUDE LONGITUDE
## 1: Washington       King  2.79553       I-90           47.44331 -121.6681
## 2: Washington       King  2.79553       I-90           47.44331 -121.6681
##    ROAD_DIREC  ORN_FID    FID_1 ACCESS LSHL_TY2 LSHL_TYP MED_TYPE NHS_IND
## 1:  Eastbound 17787.59 6232.662      F        A        A        S       Y
## 2:  Eastbound 17787.59 6232.662      F        A        A        S       Y
##    PRK_ZNE RSHL_TY2 RSHL_TYP SURF_TYP SURF_TY2 TERRAIN COMP_DIR COUNTY
## 1:                A        A        P        P       M        E     17
## 2:                A        A        P        P       M        E     17
##    FUNC_CLS MEDBARTY ST_FUNC RTE_NBR          HPMS ROAD_INV SPD_LIMT BEGMP
## 1:       41       DE      R5      90 2.514882e-311       90       70 33.36
## 2:       41       DE      R5      90 2.514882e-311       90       70 33.36
##    ENDMP LSHLDWID   MEDWID NO_LANE1 NO_LANE2 NO_LANES RSHLDWID RSHL_WD2
## 1: 36.16 7.779324 100.0268        4        3        7 9.655901 9.584419
## 2: 36.16 7.779324 100.0268        4        3        7 9.655901 9.584419
##      SEG_LNG  lanewid rdwy_wd1 rdwy_wd2 rdwy_wid     AADT     mvmt
## 1: 0.5697374 12.25326  48.6882 37.13331 85.82151 32974.55 6.874649
## 2: 0.5697374 12.25326  48.6882 37.13331 85.82151 32974.55 6.874649
##    rodwycls ORN_FID_1 Total Fatal Injury PDO DAYMTH Crash   spd_av spd_pv
## 1:        6  17787.59    17     0      6  11   0101     0       NA     NA
## 2:        6  17787.59    17     0      6  11   0101     0 45.33292     NA
##      spd_ft     date Month Day Year
## 1:       NA 01012015    01  01 2015
## 2: 45.33292 01012015    01  01 2015
#####
df1= df_RI[,c("spd_av","spd_pv","spd_ft")]
df2= df_RI[,c("date","spd_av","spd_pv","spd_ft")]
df3= df_RI[,c("Month","spd_av","spd_pv","spd_ft")]
df4= df_RI[,c("Day","spd_av","spd_pv","spd_ft")]
df5= df_RI[,c("Year","spd_av","spd_pv","spd_ft")]


######################################################
ConvEpoc2HM <- function(x) {
  # for a given epoc number, get its hour:min
  yy <- x*15
  y.hr <- yy %/% 60
  y.min <- yy %% 60
  x <- paste(str_pad(y.hr, 2, side = 'left', pad='0'), 
                     str_pad(y.min, 2, side = 'left', pad='0'), 
                     '00', sep = ':')
}

df_RI$Hour1 <- ConvEpoc2HM(df_RI$EPOCH15)
DATE4 <- paste(strptime(df_RI$date, format = "%m%d%Y", tz =""), df_RI$Hour1, sep = ' ')
df_RI$PCT_TIME <- as.POSIXct(DATE4, tz ="", format = "%Y-%m-%d %H:%M:%OS")
df_RI$Hour <- strftime(df_RI$PCT_TIME, format="%H")
df_RI$DOW <- wday(df_RI$PCT_TIME, label = TRUE)


df6= df_RI[,c("Hour","spd_av","spd_pv","spd_ft")]
df7= df_RI[,c("DOW","spd_av","spd_pv","spd_ft")]

################################################################





#### Operating Speed by Month
long <- melt(df3, id.vars = c("Month"))
ggviolin(long, "Month", "value", fill = "Month",
   add = "boxplot", add.params = list(fill = "white"))+coord_flip()+facet_grid(. ~ "variable")+
geom_hline(yintercept=61, linetype="dashed", 
                color = "red", size=1)+labs(title="Operating Speed by Month", x="Month",y="Speed (mph)")+ theme(legend.position="none")
## Warning: Removed 5315397 rows containing non-finite values (stat_ydensity).
## Warning: Removed 5315397 rows containing non-finite values (stat_boxplot).

#### Operating Speed by Month [All Vehicles]
long <- melt(df3[,c(1,2)], id.vars = c("Month"))
ggviolin(long, "Month", "value", fill = "Month",
         add = "boxplot", add.params = list(fill = "white"))+coord_flip()+
  geom_hline(yintercept=61, linetype="dashed", 
             color = "red", size=1)+labs(title="Operating Speed (All vehicles) by Month", x="Month",y="Speed (mph)")+ theme(legend.position="none")
## Warning: Removed 972706 rows containing non-finite values (stat_ydensity).
## Warning: Removed 972706 rows containing non-finite values (stat_boxplot).

#### Operating Speed by Month [Passenger Cars]
long <- melt(df3[,c(1,3)], id.vars = c("Month"))
ggviolin(long, "Month", "value", fill = "Month",
         add = "boxplot", add.params = list(fill = "white"))+coord_flip()+
  geom_hline(yintercept=64, linetype="dashed", 
             color = "red", size=1)+labs(title="Operating Speed (Passenger Cars) by Month", x="Month",y="Speed (mph)")+ theme(legend.position="none")
## Warning: Removed 2549878 rows containing non-finite values (stat_ydensity).
## Warning: Removed 2549878 rows containing non-finite values (stat_boxplot).

#### Operating Speed by Month [Fright Trucks]
long <- melt(df3[,c(1,4)], id.vars = c("Month"))
ggviolin(long, "Month", "value", fill = "Month",
         add = "boxplot", add.params = list(fill = "white"))+coord_flip()+
  geom_hline(yintercept=60, linetype="dashed", 
             color = "red", size=1)+labs(title="Operating Speed (Freight Trucks) by Month", x="Month",y="Speed (mph)")+ theme(legend.position="none")
## Warning: Removed 1792813 rows containing non-finite values (stat_ydensity).
## Warning: Removed 1792813 rows containing non-finite values (stat_boxplot).

gg_miss_upset(df1)

gg_miss_var(df3, facet = Month, show_pct = TRUE)

gg_miss_var(df4, facet = Day, show_pct = TRUE)

gg_miss_var(df5, facet = Year, show_pct = TRUE)

gg_miss_var(df2, facet = date, show_pct = TRUE)

### Missingness by Date
df2a <- df2 %>%
  group_by(date) %>%
  miss_var_summary()
datatable(
  df2a, extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = c('csv', 'excel', 'print')
  )
)
### Missingness by Month
df3a <- df3 %>%
  group_by(Month) %>%
  miss_var_summary()
datatable(
  df3a, extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = c('csv', 'excel', 'print')
  )
)
### Missingness by Day
df4a <- df4 %>%
  group_by(Day) %>%
  miss_var_summary()
datatable(
  df4a, extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = c('csv', 'excel', 'print')
  )
)
### Missingness by Year
df5a <- df5 %>%
  group_by(Year) %>%
  miss_var_summary()
datatable(
  df5a, extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = c('csv', 'excel', 'print')
  )
)
### TMC Level
tmc1 <- df_RI[,c('TMC')] %>%
  group_by(TMC) %>%
  summarize(Count=n())
datatable(
  tmc1, extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = c('csv', 'excel', 'print')
  )
)
#### Operating Speed by Hour
long <- melt(df6, id.vars = c("Hour"))
long$Hour <- as.factor(long$Hour)
ggviolin(long, "Hour", "value", fill = "Hour",
   add = "boxplot", add.params = list(fill = "white"))+coord_flip()+
geom_hline(yintercept=61, linetype="dashed", 
                color = "red", size=1)+labs(title="Operating Speed by Hour", x="Hour",y="Speed (mph)")+ theme(legend.position="none")
## Warning: Removed 5315397 rows containing non-finite values (stat_ydensity).
## Warning: Removed 5315397 rows containing non-finite values (stat_boxplot).

#### Operating Speed by DOW
long <- melt(df7, id.vars = c("DOW"))
ggviolin(long, "DOW", "value", fill = "DOW",
   add = "boxplot", add.params = list(fill = "white"))+coord_flip()+
geom_hline(yintercept=61, linetype="dashed", 
                color = "red", size=1)+labs(title="Operating Speed by DOW", x="Day of Week",y="Speed (mph)")+ theme(legend.position="none")
## Warning: Removed 5315397 rows containing non-finite values (stat_ydensity).

## Warning: Removed 5315397 rows containing non-finite values (stat_boxplot).

gg_miss_var(df6, facet = Hour, show_pct = TRUE)

### Missingness by Hour
df6a <- df6 %>%
  group_by(Hour) %>%
  miss_var_summary()
datatable(
  df6a, extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = c('csv', 'excel', 'print')
  )
)
### Missingness by DOW
df7a <- df7 %>%
  group_by(DOW) %>%
  miss_var_summary()
datatable(
  df7a, extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = c('csv', 'excel', 'print')
  )
)