North Carolina Rural Multi-lane Undivided – Daily Speed (ARIMA)

Subasish Das and Choalun Ma

2018-11-12

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

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

### Month, Day
df_RMU$date <- as.character(df_RMU$DATE)
df_RMU$date <- str_pad(df_RMU$DATE, 8, pad = "0")
df_RMU$Month <- substr(df_RMU$date, start = 1, stop = 2)
df_RMU$Day   <- substr(df_RMU$date, start = 3, stop = 4)
df_RMU$Year  <- substr(df_RMU$date, start = 5, stop = 8)
df_RMU$MonthDay <- paste0(df_RMU$Month,"_", df_RMU$Day)
head(df_RMU)
##            TimeStamp       TMC    DATE EPOCH15 Travel_TIME_ALL_VEHICLES
## 1:  110N17806_0701_0 110N17806 7012015       0                       NA
## 2:  110N17806_0701_1 110N17806 7012015       1                       NA
## 3: 110N17806_0701_10 110N17806 7012015      10                       NA
## 4: 110N17806_0701_11 110N17806 7012015      11                       NA
## 5: 110N17806_0701_12 110N17806 7012015      12                       NA
## 6: 110N17806_0701_13 110N17806 7012015      13                       NA
##    Travel_TIME_PASSENGER_VEHICLES Travel_TIME_FREIGHT_TRUCKS TMC_length
## 1:                             NA                         NA   24722.67
## 2:                             NA                         NA   24722.67
## 3:                             NA                         NA   24722.67
## 4:                             NA                         NA   24722.67
## 5:                             NA                         NA   24722.67
## 6:                             NA                         NA   24722.67
##    ave_aadt ave_wtdsgspd ave_medwid ave_peaklane  ave_row ave_sur_wid
## 1: 5249.505           65         NA           NA 71.37837    23.10593
## 2: 5249.505           65         NA           NA 71.37837    23.10593
## 3: 5249.505           65         NA           NA 71.37837    23.10593
## 4: 5249.505           65         NA           NA 71.37837    23.10593
## 5: 5249.505           65         NA           NA 71.37837    23.10593
## 6: 5249.505           65         NA           NA 71.37837    23.10593
##    ave_no_lanes ave_spd_limt ave_rodwycls ave_rshldwid FC TER ACC MED
## 1:     2.098732     40.52784     8.098732            6  6   2   F  Cu
## 2:     2.098732     40.52784     8.098732            6  6   2   F  Cu
## 3:     2.098732     40.52784     8.098732            6  6   2   F  Cu
## 4:     2.098732     40.52784     8.098732            6  6   2   F  Cu
## 5:     2.098732     40.52784     8.098732            6  6   2   F  Cu
## 6:     2.098732     40.52784     8.098732            6  6   2   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   0701     0     NA     NA     NA 07012015    07  01
## 2:     0 0 0 0 0 0   0701     0     NA     NA     NA 07012015    07  01
## 3:     0 0 0 0 0 0   0701     0     NA     NA     NA 07012015    07  01
## 4:     0 0 0 0 0 0   0701     0     NA     NA     NA 07012015    07  01
## 5:     0 0 0 0 0 0   0701     0     NA     NA     NA 07012015    07  01
## 6:     0 0 0 0 0 0   0701     0     NA     NA     NA 07012015    07  01
##    Year MonthDay
## 1: 2015    07_01
## 2: 2015    07_01
## 3: 2015    07_01
## 4: 2015    07_01
## 5: 2015    07_01
## 6: 2015    07_01
day1<- df_RMU[,-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.8
##  2 01_02              33.7
##  3 01_03              33.0
##  4 01_04              34.1
##  5 01_05              34.7
##  6 01_06              34.2
##  7 01_07              34.3
##  8 01_08              34.7
##  9 01_09              33.8
## 10 01_10              34.5
## # ... 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.7146, Lag order = 7, p-value = 0.02354
## alternative hypothesis: stationary
adf.test(deseasonal_cnt, alternative = "stationary")
## 
##  Augmented Dickey-Fuller Test
## 
## data:  deseasonal_cnt
## Dickey-Fuller = -3.2578, Lag order = 7, p-value = 0.07838
## 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 = -11.685, 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.45  0.45
##   [2,]  0.34  0.16
##   [3,]  0.32  0.15
##   [4,]  0.33  0.15
##   [5,]  0.23  0.00
##   [6,]  0.39  0.28
##   [7,]  0.49  0.29
##   [8,]  0.38  0.04
##   [9,]  0.30  0.00
##  [10,]  0.30  0.00
##  [11,]  0.26  0.00
##  [12,]  0.21 -0.02
##  [13,]  0.28  0.01
##  [14,]  0.33  0.04
##  [15,]  0.27 -0.02
##  [16,]  0.21 -0.06
##  [17,]  0.24  0.03
##  [18,]  0.19 -0.03
##  [19,]  0.16 -0.01
##  [20,]  0.18 -0.02
##  [21,]  0.35  0.21
##  [22,]  0.25  0.01
##  [23,]  0.17 -0.07
##  [24,]  0.21  0.05
##  [25,]  0.17 -0.03
##  [26,]  0.11  0.00
##  [27,]  0.18  0.02
##  [28,]  0.28  0.05
##  [29,]  0.22  0.01
##  [30,]  0.19  0.01
##  [31,]  0.20  0.01
##  [32,]  0.20  0.06
##  [33,]  0.15  0.02
##  [34,]  0.18 -0.02
##  [35,]  0.28  0.09
##  [36,]  0.16 -0.12
##  [37,]  0.15  0.00
##  [38,]  0.18 -0.02
##  [39,]  0.21  0.05
##  [40,]  0.17  0.05
##  [41,]  0.18 -0.02
##  [42,]  0.25  0.05
##  [43,]  0.14 -0.10
##  [44,]  0.10 -0.03
##  [45,]  0.12 -0.04
##  [46,]  0.15  0.01
##  [47,]  0.16  0.09
##  [48,]  0.14 -0.06
##  [49,]  0.23  0.08
##  [50,]  0.14 -0.03
##  [51,]  0.03 -0.13
##  [52,]  0.10  0.06
##  [53,]  0.11 -0.02
##  [54,]  0.11  0.03
##  [55,]  0.13  0.05
##  [56,]  0.25  0.07
##  [57,]  0.07 -0.10
##  [58,]  0.00 -0.07
##  [59,]  0.04 -0.02
##  [60,]  0.07  0.01
##  [61,]  0.03 -0.05
##  [62,]  0.09 -0.03
##  [63,]  0.19  0.06
##  [64,]  0.06 -0.05
##  [65,]  0.00  0.01
##  [66,]  0.02 -0.02
##  [67,]  0.05  0.04
##  [68,]  0.04  0.05
##  [69,]  0.06 -0.06
##  [70,]  0.16  0.08
##  [71,]  0.07 -0.05
##  [72,]  0.02  0.04
##  [73,]  0.01 -0.02
##  [74,]  0.05  0.00
##  [75,]  0.02  0.03
##  [76,]  0.07 -0.01
##  [77,]  0.13 -0.04
##  [78,]  0.07  0.02
##  [79,]  0.04  0.04
##  [80,]  0.06  0.02
##  [81,]  0.01 -0.08
##  [82,] -0.01 -0.04
##  [83,]  0.04  0.04
##  [84,]  0.16  0.05
##  [85,]  0.07  0.00
##  [86,]  0.01 -0.06
##  [87,]  0.02 -0.05
##  [88,] -0.01 -0.02
##  [89,]  0.02  0.06
##  [90,]  0.03  0.02
##  [91,]  0.13  0.01
##  [92,]  0.03 -0.04
##  [93,]  0.00  0.01
##  [94,] -0.02 -0.04
##  [95,]  0.02  0.03
##  [96,]  0.02  0.04
##  [97,]  0.07  0.06
##  [98,]  0.07 -0.03
##  [99,]  0.04  0.03
## [100,]  0.06  0.11
## [101,]  0.02 -0.06
## [102,]  0.02 -0.03
## [103,]  0.03  0.01
## [104,]  0.05  0.02
## [105,]  0.09  0.00
## [106,]  0.06 -0.02
## [107,] -0.03 -0.12
## [108,]  0.01  0.02
## [109,]  0.03  0.01
## [110,]  0.05  0.03
## [111,]  0.03  0.00
## [112,]  0.14  0.03
## [113,] -0.01 -0.08
## [114,] -0.06 -0.06
## [115,] -0.04  0.02
## [116,] -0.01 -0.01
## [117,] -0.04 -0.06
## [118,]  0.05  0.08
## [119,]  0.08 -0.04
## [120,] -0.04 -0.03
acf2(deseasonal_cnt)

##          ACF  PACF
##   [1,]  0.47  0.47
##   [2,]  0.36  0.18
##   [3,]  0.34  0.15
##   [4,]  0.35  0.16
##   [5,]  0.27  0.01
##   [6,]  0.42  0.28
##   [7,]  0.52  0.30
##   [8,]  0.42  0.06
##   [9,]  0.32 -0.02
##  [10,]  0.29 -0.03
##  [11,]  0.28  0.00
##  [12,]  0.24 -0.02
##  [13,]  0.30  0.01
##  [14,]  0.37  0.05
##  [15,]  0.31 -0.02
##  [16,]  0.23 -0.05
##  [17,]  0.26  0.05
##  [18,]  0.21 -0.03
##  [19,]  0.17 -0.05
##  [20,]  0.18 -0.06
##  [21,]  0.37  0.22
##  [22,]  0.27  0.01
##  [23,]  0.18 -0.08
##  [24,]  0.23  0.06
##  [25,]  0.19 -0.03
##  [26,]  0.12  0.00
##  [27,]  0.19  0.05
##  [28,]  0.30  0.06
##  [29,]  0.23 -0.02
##  [30,]  0.15 -0.07
##  [31,]  0.20  0.05
##  [32,]  0.21  0.09
##  [33,]  0.15  0.01
##  [34,]  0.19  0.00
##  [35,]  0.31  0.10
##  [36,]  0.17 -0.11
##  [37,]  0.16  0.03
##  [38,]  0.20  0.01
##  [39,]  0.22  0.03
##  [40,]  0.16  0.01
##  [41,]  0.19  0.00
##  [42,]  0.27  0.06
##  [43,]  0.15 -0.13
##  [44,]  0.11 -0.02
##  [45,]  0.15 -0.04
##  [46,]  0.17  0.00
##  [47,]  0.17  0.11
##  [48,]  0.16 -0.06
##  [49,]  0.23  0.03
##  [50,]  0.14 -0.05
##  [51,]  0.04 -0.09
##  [52,]  0.12  0.09
##  [53,]  0.13 -0.04
##  [54,]  0.12  0.03
##  [55,]  0.14  0.04
##  [56,]  0.26  0.05
##  [57,]  0.07 -0.08
##  [58,]  0.01 -0.07
##  [59,]  0.04 -0.05
##  [60,]  0.05 -0.06
##  [61,]  0.03 -0.02
##  [62,]  0.10  0.02
##  [63,]  0.20  0.04
##  [64,]  0.07 -0.03
##  [65,]  0.01  0.03
##  [66,]  0.02 -0.01
##  [67,]  0.05  0.07
##  [68,]  0.06  0.07
##  [69,]  0.06 -0.08
##  [70,]  0.15  0.04
##  [71,]  0.08 -0.03
##  [72,]  0.02  0.06
##  [73,]  0.01 -0.05
##  [74,]  0.06 -0.02
##  [75,]  0.04  0.04
##  [76,]  0.08 -0.01
##  [77,]  0.14 -0.01
##  [78,]  0.09  0.02
##  [79,]  0.04  0.01
##  [80,]  0.04 -0.01
##  [81,]  0.01 -0.07
##  [82,]  0.00 -0.01
##  [83,]  0.04  0.02
##  [84,]  0.17  0.05
##  [85,]  0.08  0.00
##  [86,]  0.00 -0.06
##  [87,]  0.02 -0.02
##  [88,]  0.00 -0.01
##  [89,]  0.02  0.03
##  [90,]  0.00 -0.03
##  [91,]  0.13  0.03
##  [92,]  0.03 -0.02
##  [93,]  0.00  0.01
##  [94,] -0.01 -0.02
##  [95,]  0.04  0.06
##  [96,]  0.02  0.05
##  [97,]  0.07  0.08
##  [98,]  0.09 -0.01
##  [99,]  0.05  0.03
## [100,]  0.05  0.09
## [101,]  0.02 -0.08
## [102,]  0.03 -0.02
## [103,]  0.03 -0.02
## [104,]  0.06  0.03
## [105,]  0.11  0.01
## [106,]  0.07 -0.03
## [107,] -0.03 -0.10
## [108,]  0.02 -0.01
## [109,]  0.02  0.00
## [110,]  0.04  0.03
## [111,]  0.02 -0.02
## [112,]  0.14  0.04
## [113,]  0.00 -0.10
## [114,] -0.06 -0.06
## [115,] -0.03  0.04
## [116,]  0.00 -0.01
## [117,] -0.03 -0.06
## [118,]  0.07  0.09
## [119,]  0.08 -0.06
## [120,] -0.06 -0.09

Seasonility Not in Consideration

# Step 6: Fitting an ARIMA model
auto.arima(deseasonal_cnt, seasonal=FALSE)
## Series: deseasonal_cnt 
## ARIMA(1,1,1) 
## 
## Coefficients:
##          ar1      ma1
##       0.1402  -0.9258
## s.e.  0.0566   0.0204
## 
## sigma^2 estimated as 0.2174:  log likelihood=-238.63
## AIC=483.25   AICc=483.32   BIC=494.94
# 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(0,1,1)(2,0,0)[30] with drift 
## 
## Coefficients:
##           ma1     sar1     sar2    drift
##       -0.9227  -0.0910  -0.0804  -0.0037
## s.e.   0.0246   0.0566   0.0607   0.0017
## 
## sigma^2 estimated as 0.2176:  log likelihood=-238.19
## AIC=486.38   AICc=486.55   BIC=505.86
# 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)