# Step 1: Load R Packages
### options(repos='http://cran.rstudio.com/')
#install.packages("astsa")
#install.packages('ggplot2')
#install.packages('forecast')
#install.packages('tseries')
#install.packages("data.table")
library(astsa)
library(forecast)
library(tseries)
library(zoo)
library(tseries)
library(data.table)
library(dplyr)
library(tidyr)
library(naniar)
library(stringr)
library(ggplot2)
library(DT)
library(lubridate)
library(ggpubr)
setwd("/scratch/user/cma16/Task4_Deliverable2/NCprocess4/AllCrash/FacilityBased/")
load("./two-lane_undivided_NC_reduce_withCrash_no_intersection.rData")
mytype = 'R2'
setwd(paste0("/scratch/user/cma16/Task4_Deliverable2/NCprocess4/AllCrash/FacilityBased/",mytype))
df_R2 <- N_2un_nomed
dim(df_R2)
## [1] 16229280 30
### Calculating Speed
df_R2$spd_av = 3600*df_R2$TMC_length/df_R2$Travel_TIME_ALL_VEHICLES/5280
df_R2$spd_pv = 3600*df_R2$TMC_length/df_R2$Travel_TIME_PASSENGER_VEHICLES/5280
df_R2$spd_ft = 3600*df_R2$TMC_length/df_R2$Travel_TIME_FREIGHT_TRUCKS/5280
### Month, Day
df_R2$date <- as.character(df_R2$DATE)
df_R2$date <- str_pad(df_R2$DATE, 8, pad = "0")
df_R2$Month <- substr(df_R2$date, start = 1, stop = 2)
df_R2$Day <- substr(df_R2$date, start = 3, stop = 4)
df_R2$Year <- substr(df_R2$date, start = 5, stop = 8)
df_R2$MonthDay <- paste0(df_R2$Month,"_", df_R2$Day)
head(df_R2)
## TimeStamp TMC DATE EPOCH15 Travel_TIME_ALL_VEHICLES
## 1: 110N07650_0101_0 110N07650 1012015 0 NA
## 2: 110N07650_0101_1 110N07650 1012015 1 NA
## 3: 110N07650_0101_10 110N07650 1012015 10 NA
## 4: 110N07650_0101_11 110N07650 1012015 11 NA
## 5: 110N07650_0101_12 110N07650 1012015 12 NA
## 6: 110N07650_0101_13 110N07650 1012015 13 NA
## Travel_TIME_PASSENGER_VEHICLES Travel_TIME_FREIGHT_TRUCKS TMC_length
## 1: NA NA 25859.83
## 2: NA NA 25859.83
## 3: NA NA 25859.83
## 4: NA NA 25859.83
## 5: NA NA 25859.83
## 6: NA NA 25859.83
## ave_aadt ave_wtdsgspd ave_medwid ave_peaklane ave_row ave_sur_wid
## 1: 14778.71 70 NA NA 122.6387 60
## 2: 14778.71 70 NA NA 122.6387 60
## 3: 14778.71 70 NA NA 122.6387 60
## 4: 14778.71 70 NA NA 122.6387 60
## 5: 14778.71 70 NA NA 122.6387 60
## 6: 14778.71 70 NA NA 122.6387 60
## ave_no_lanes ave_spd_limt ave_rodwycls ave_rshldwid FC TER ACC MED
## 1: 2 36.3579 8 5 6 3 F Cu
## 2: 2 36.3579 8 5 6 3 F Cu
## 3: 2 36.3579 8 5 6 3 F Cu
## 4: 2 36.3579 8 5 6 3 F Cu
## 5: 2 36.3579 8 5 6 3 F Cu
## 6: 2 36.3579 8 5 6 3 F Cu
## 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
## 3: 0 0 0 0 0 0 0101 0 NA NA NA 01012015 01 01
## 4: 0 0 0 0 0 0 0101 0 NA NA NA 01012015 01 01
## 5: 0 0 0 0 0 0 0101 0 NA NA NA 01012015 01 01
## 6: 0 0 0 0 0 0 0101 0 NA NA NA 01012015 01 01
## Year MonthDay
## 1: 2015 01_01
## 2: 2015 01_01
## 3: 2015 01_01
## 4: 2015 01_01
## 5: 2015 01_01
## 6: 2015 01_01
day1<- df_R2[,-c(1)] %>% group_by(MonthDay) %>% summarize(Speed_All_Mean=mean(spd_av, na.rm=TRUE))
day1
## # A tibble: 365 x 2
## MonthDay Speed_All_Mean
## <chr> <dbl>
## 1 01_01 33.4
## 2 01_02 32.3
## 3 01_03 32.4
## 4 01_04 33.7
## 5 01_05 31.8
## 6 01_06 31.5
## 7 01_07 31.4
## 8 01_08 31.9
## 9 01_09 31.6
## 10 01_10 32.4
## # ... with 355 more rows
# Step 2: Examine Data
speed_clean <- tsclean(ts(day1$Speed_All_Mean))
plot.ts(speed_clean)
# ggplot() + geom_line(data = Q1, aes(x = TimeStamp, y = speed_clean)) + ylab('Cleaned Speed Records')
day1$cnt_ma = ma(speed_clean, order=7) # using the clean count with no outliers
day1$cnt_ma30 = ma(speed_clean, order=30)
# Step 3: Decompose Your Data
count_ma = ts(na.omit(speed_clean), frequency=30)
decomp = stl(count_ma, s.window="periodic")
deseasonal_cnt <- seasadj(decomp)
plot(decomp)
# Step 4: Stationarity
# statinary test
adf.test(count_ma, alternative = "stationary")
##
## Augmented Dickey-Fuller Test
##
## data: count_ma
## Dickey-Fuller = -3.1466, Lag order = 7, p-value = 0.09718
## alternative hypothesis: stationary
adf.test(deseasonal_cnt, alternative = "stationary")
##
## Augmented Dickey-Fuller Test
##
## data: deseasonal_cnt
## Dickey-Fuller = -3.0898, Lag order = 7, p-value = 0.117
## alternative hypothesis: stationary
d1 = diff(deseasonal_cnt)
adf.test(d1, alternative = "stationary")
## Warning in adf.test(d1, alternative = "stationary"): p-value smaller than
## printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: d1
## Dickey-Fuller = -9.3223, Lag order = 7, p-value = 0.01
## alternative hypothesis: stationary
# Step 5: Autocorrelations and Choosing Model Order
# check ACF and PACF
acf2(count_ma)
## ACF PACF
## [1,] 0.30 0.30
## [2,] -0.14 -0.25
## [3,] -0.19 -0.08
## [4,] -0.21 -0.18
## [5,] -0.18 -0.13
## [6,] 0.23 0.30
## [7,] 0.82 0.76
## [8,] 0.24 -0.27
## [9,] -0.18 -0.14
## [10,] -0.22 -0.06
## [11,] -0.24 0.01
## [12,] -0.20 0.01
## [13,] 0.21 0.08
## [14,] 0.76 0.25
## [15,] 0.20 -0.21
## [16,] -0.21 -0.07
## [17,] -0.24 0.01
## [18,] -0.24 0.07
## [19,] -0.20 -0.01
## [20,] 0.20 0.07
## [21,] 0.77 0.24
## [22,] 0.20 -0.13
## [23,] -0.20 0.03
## [24,] -0.24 -0.08
## [25,] -0.23 0.05
## [26,] -0.19 0.03
## [27,] 0.21 0.07
## [28,] 0.77 0.16
## [29,] 0.22 -0.05
## [30,] -0.19 -0.03
## [31,] -0.24 -0.06
## [32,] -0.25 -0.03
## [33,] -0.21 0.00
## [34,] 0.20 0.03
## [35,] 0.74 0.02
## [36,] 0.18 -0.10
## [37,] -0.22 -0.06
## [38,] -0.25 0.01
## [39,] -0.24 0.13
## [40,] -0.19 0.06
## [41,] 0.21 0.03
## [42,] 0.74 0.07
## [43,] 0.18 -0.06
## [44,] -0.21 0.04
## [45,] -0.26 -0.06
## [46,] -0.26 -0.07
## [47,] -0.21 -0.04
## [48,] 0.18 -0.06
## [49,] 0.71 0.02
## [50,] 0.16 -0.08
## [51,] -0.20 0.10
## [52,] -0.24 0.05
## [53,] -0.26 -0.04
## [54,] -0.22 -0.03
## [55,] 0.17 -0.01
## [56,] 0.67 -0.07
## [57,] 0.15 0.02
## [58,] -0.21 0.06
## [59,] -0.25 -0.04
## [60,] -0.27 -0.02
## [61,] -0.23 -0.07
## [62,] 0.16 -0.02
## [63,] 0.64 -0.02
## [64,] 0.15 0.09
## [65,] -0.21 0.00
## [66,] -0.25 0.02
## [67,] -0.26 -0.05
## [68,] -0.22 -0.03
## [69,] 0.16 -0.03
## [70,] 0.64 -0.02
## [71,] 0.14 0.01
## [72,] -0.21 -0.03
## [73,] -0.25 -0.02
## [74,] -0.27 0.01
## [75,] -0.22 0.03
## [76,] 0.14 -0.03
## [77,] 0.63 0.10
## [78,] 0.13 -0.02
## [79,] -0.21 -0.02
## [80,] -0.24 0.00
## [81,] -0.25 0.01
## [82,] -0.21 0.01
## [83,] 0.14 -0.07
## [84,] 0.59 -0.09
## [85,] 0.11 -0.05
## [86,] -0.22 0.00
## [87,] -0.26 -0.02
## [88,] -0.28 -0.04
## [89,] -0.25 -0.09
## [90,] 0.11 -0.01
## [91,] 0.56 -0.01
## [92,] 0.09 0.01
## [93,] -0.22 -0.01
## [94,] -0.27 -0.08
## [95,] -0.27 0.07
## [96,] -0.24 0.03
## [97,] 0.09 -0.09
## [98,] 0.54 0.00
## [99,] 0.09 -0.03
## [100,] -0.22 -0.06
## [101,] -0.25 0.09
## [102,] -0.27 0.02
## [103,] -0.23 0.01
## [104,] 0.10 -0.03
## [105,] 0.55 0.02
## [106,] 0.08 -0.10
## [107,] -0.21 0.02
## [108,] -0.25 -0.02
## [109,] -0.27 -0.03
## [110,] -0.23 -0.01
## [111,] 0.11 0.05
## [112,] 0.51 -0.11
## [113,] 0.07 0.01
## [114,] -0.22 0.04
## [115,] -0.25 0.01
## [116,] -0.26 0.01
## [117,] -0.23 -0.01
## [118,] 0.08 -0.05
## [119,] 0.48 -0.07
## [120,] 0.06 0.03
## ACF PACF
## [1,] 0.30 0.30
## [2,] -0.15 -0.26
## [3,] -0.20 -0.08
## [4,] -0.22 -0.19
## [5,] -0.18 -0.13
## [6,] 0.24 0.30
## [7,] 0.83 0.78
## [8,] 0.24 -0.29
## [9,] -0.19 -0.16
## [10,] -0.22 -0.05
## [11,] -0.24 0.02
## [12,] -0.20 0.01
## [13,] 0.21 0.08
## [14,] 0.79 0.26
## [15,] 0.21 -0.21
## [16,] -0.21 -0.03
## [17,] -0.24 -0.04
## [18,] -0.24 0.08
## [19,] -0.21 0.00
## [20,] 0.21 0.09
## [21,] 0.78 0.19
## [22,] 0.20 -0.12
## [23,] -0.20 0.03
## [24,] -0.24 -0.07
## [25,] -0.24 0.06
## [26,] -0.20 0.02
## [27,] 0.22 0.08
## [28,] 0.78 0.16
## [29,] 0.22 -0.05
## [30,] -0.21 -0.09
## [31,] -0.24 -0.01
## [32,] -0.25 -0.03
## [33,] -0.21 0.01
## [34,] 0.20 0.02
## [35,] 0.75 0.01
## [36,] 0.18 -0.11
## [37,] -0.22 0.00
## [38,] -0.26 0.00
## [39,] -0.24 0.11
## [40,] -0.19 0.07
## [41,] 0.21 0.01
## [42,] 0.75 0.05
## [43,] 0.18 -0.06
## [44,] -0.21 0.07
## [45,] -0.27 -0.09
## [46,] -0.26 -0.04
## [47,] -0.22 -0.09
## [48,] 0.19 -0.04
## [49,] 0.72 0.03
## [50,] 0.16 -0.06
## [51,] -0.21 0.07
## [52,] -0.25 0.07
## [53,] -0.27 -0.05
## [54,] -0.22 -0.05
## [55,] 0.17 0.00
## [56,] 0.68 -0.09
## [57,] 0.15 0.04
## [58,] -0.21 0.06
## [59,] -0.26 -0.03
## [60,] -0.28 -0.08
## [61,] -0.23 0.00
## [62,] 0.17 -0.04
## [63,] 0.66 0.01
## [64,] 0.15 0.11
## [65,] -0.21 -0.02
## [66,] -0.25 0.01
## [67,] -0.26 -0.02
## [68,] -0.23 -0.07
## [69,] 0.17 -0.04
## [70,] 0.65 0.04
## [71,] 0.14 -0.03
## [72,] -0.21 -0.02
## [73,] -0.26 -0.01
## [74,] -0.27 0.03
## [75,] -0.23 0.00
## [76,] 0.15 0.00
## [77,] 0.64 0.04
## [78,] 0.13 -0.01
## [79,] -0.21 -0.01
## [80,] -0.25 -0.01
## [81,] -0.26 -0.02
## [82,] -0.21 0.05
## [83,] 0.14 -0.08
## [84,] 0.59 -0.13
## [85,] 0.11 -0.02
## [86,] -0.23 -0.04
## [87,] -0.26 0.01
## [88,] -0.29 -0.02
## [89,] -0.26 -0.10
## [90,] 0.10 -0.08
## [91,] 0.57 0.06
## [92,] 0.10 -0.02
## [93,] -0.22 0.01
## [94,] -0.27 -0.04
## [95,] -0.27 0.06
## [96,] -0.25 0.02
## [97,] 0.09 -0.07
## [98,] 0.55 -0.02
## [99,] 0.09 -0.05
## [100,] -0.22 0.01
## [101,] -0.25 0.06
## [102,] -0.27 0.01
## [103,] -0.23 0.04
## [104,] 0.11 0.01
## [105,] 0.56 -0.02
## [106,] 0.09 -0.09
## [107,] -0.22 -0.01
## [108,] -0.25 0.01
## [109,] -0.27 -0.02
## [110,] -0.23 -0.03
## [111,] 0.10 0.02
## [112,] 0.51 -0.10
## [113,] 0.07 0.01
## [114,] -0.23 -0.01
## [115,] -0.26 0.02
## [116,] -0.27 0.00
## [117,] -0.24 0.03
## [118,] 0.08 -0.04
## [119,] 0.48 -0.09
## [120,] 0.05 -0.02
Seasonility Not in Consideration
# Step 6: Fitting an ARIMA model
auto.arima(deseasonal_cnt, seasonal=FALSE)
## Series: deseasonal_cnt
## ARIMA(3,0,3) with non-zero mean
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2 ma3 mean
## 0.5344 -0.4038 0.8244 -0.174 0.1657 -0.8609 31.9432
## s.e. 0.0373 0.0457 0.0383 0.036 0.0397 0.0315 0.1188
##
## sigma^2 estimated as 0.7106: log likelihood=-453.18
## AIC=922.37 AICc=922.77 BIC=953.57
# Step 7: Evaluate and Iterate
# (try different model)
fit<-auto.arima(deseasonal_cnt, seasonal=FALSE)
tsdisplay(residuals(fit), lag.max=45, main='Model Residuals [Seasonality not considered]')
# step 8 forcast
fcast <- forecast(fit, h=30)
plot(fcast)
Seasonility in Consideration
# Step 6: Fitting an ARIMA model
auto.arima(deseasonal_cnt, seasonal=TRUE)
## Series: deseasonal_cnt
## ARIMA(2,0,2)(2,0,0)[30] with non-zero mean
##
## Coefficients:
## ar1 ar2 ma1 ma2 sar1 sar2 mean
## 0.2351 0.7410 -0.0247 -0.8555 -0.3305 -0.3906 31.9443
## s.e. 0.0557 0.0546 0.0340 0.0301 0.0530 0.0518 0.1169
##
## sigma^2 estimated as 0.6948: log likelihood=-453.93
## AIC=923.86 AICc=924.26 BIC=955.05
# Step 7: Evaluate and Iterate
# (try different model)
fit<-auto.arima(deseasonal_cnt, seasonal=TRUE)
tsdisplay(residuals(fit), lag.max=45, main='Model Residuals [Seasonality considered]')
# step 8 forcast
fcast <- forecast(fit, h=30)
plot(fcast)