North Carolina Rural Multi-lane Divided

Subasish Das and Chaolun Ma

2018-11-12

North Carolina Conflated Data (Rural Multi-lane Divided)

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/NCprocess4/AllCrash/FacilityBased/")
load("./multi-lane_divided_NC_reduce_withCrash_no_intersection.rData")
mytype = 'RMD'
setwd(paste0("/scratch/user/cma16/Task4_Deliverable2/NCprocess4/AllCrash/FacilityBased/",mytype))

df_RMD <- N_mun_med
dim(df_RMD)
## [1] 6157824      30
### Calculating Speed
df_RMD$spd_av = 3600*df_RMD$TMC_length/df_RMD$Travel_TIME_ALL_VEHICLES/5280
df_RMD$spd_pv = 3600*df_RMD$TMC_length/df_RMD$Travel_TIME_PASSENGER_VEHICLES/5280
df_RMD$spd_ft = 3600*df_RMD$TMC_length/df_RMD$Travel_TIME_FREIGHT_TRUCKS/5280


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

head(df_RMD,2)
##           TimeStamp       TMC    DATE EPOCH15 Travel_TIME_ALL_VEHICLES
## 1: 110P16085_0101_0 110P16085 1012015       0                       NA
## 2: 110P16085_0101_1 110P16085 1012015       1                       NA
##    Travel_TIME_PASSENGER_VEHICLES Travel_TIME_FREIGHT_TRUCKS TMC_length
## 1:                             NA                         NA   15234.63
## 2:                             NA                         NA   15234.63
##    ave_aadt ave_wtdsgspd ave_medwid ave_peaklane ave_row ave_sur_wid
## 1:    18000           70         15           NA 178.605    29.42966
## 2:    18000           70         15           NA 178.605    29.42966
##    ave_no_lanes ave_spd_limt ave_rodwycls ave_rshldwid FC TER ACC MED
## 1:     3.856655     61.20598     4.750853      7.71331  6   3   F  Gr
## 2:     3.856655     61.20598     4.750853      7.71331  6   3   F  Gr
##    Total K A B C O DAYMTH Crash spd_av spd_pv spd_ft     date Month Day
## 1:     0 0 0 0 0 0   0101     0     NA     NA     NA 01012015    01  01
## 2:     0 0 0 0 0 0   0101     0     NA     NA     NA 01012015    01  01
##    Year
## 1: 2015
## 2: 2015
#####
df1= df_RMD[,c("spd_av","spd_pv","spd_ft")]
df2= df_RMD[,c("date","spd_av","spd_pv","spd_ft")]
df3= df_RMD[,c("Month","spd_av","spd_pv","spd_ft")]
df4= df_RMD[,c("Day","spd_av","spd_pv","spd_ft")]
df5= df_RMD[,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_RMD$Hour1 <- ConvEpoc2HM(df_RMD$EPOCH15)
DATE4 <- paste(strptime(df_RMD$date, format = "%m%d%Y", tz =""), df_RMD$Hour1, sep = ' ')
df_RMD$PCT_TIME <- as.POSIXct(DATE4, tz ="", format = "%Y-%m-%d %H:%M:%OS")
df_RMD$Hour <- strftime(df_RMD$PCT_TIME, format="%H")
df_RMD$DOW <- wday(df_RMD$PCT_TIME, label = TRUE)


df6= df_RMD[,c("Hour","spd_av","spd_pv","spd_ft")]
df7= df_RMD[,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=57, linetype="dashed", 
                color = "red", size=1)+labs(title="Operating Speed by Month", x="Month",y="Speed (mph)")+ theme(legend.position="none")
## Warning: Removed 8693326 rows containing non-finite values (stat_ydensity).
## Warning: Removed 8693326 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=56, 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 2240817 rows containing non-finite values (stat_ydensity).
## Warning: Removed 2240817 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=56, 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 2817469 rows containing non-finite values (stat_ydensity).
## Warning: Removed 2817469 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=59, 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 3635040 rows containing non-finite values (stat_ydensity).
## Warning: Removed 3635040 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_RMD[,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=57, linetype="dashed", 
                color = "red", size=1)+labs(title="Operating Speed by Hour", x="Hour",y="Speed (mph)")+ theme(legend.position="none")
## Warning: Removed 8693326 rows containing non-finite values (stat_ydensity).
## Warning: Removed 8693326 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=58, 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 8693326 rows containing non-finite values (stat_ydensity).

## Warning: Removed 8693326 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')
  )
)