Ohio Rural Multilane 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)


mytype = 'RMU'
setwd("/scratch/user/cma16/Task4_Deliverable2/OHprocess4/AllCrash/FacilityBased/")
load("./multi-lane_undivided_OH_reduce_withCrash.rData")

setwd(paste0("/scratch/user/cma16/Task4_Deliverable2/OHprocess4/AllCrash/FacilityBased/",mytype))
df_RMU <- OH_mun_nomed
df_RMU$spd_av = 3600*df_RMU$DISTANCE/df_RMU$Travel_TIME_ALL_VEHICLES
df_RMU$spd_pv = 3600*df_RMU$DISTANCE/df_RMU$Travel_TIME_PASSENGER_VEHICLES
df_RMU$spd_ft = 3600*df_RMU$DISTANCE/df_RMU$Travel_TIME_FREIGHT_TRUCKS

### 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 EPOCH1h Travel_TIME_ALL_VEHICLES
## 1:  108N06000_0101_0 108N06000 1012015       0                       NA
## 2:  108N06000_0101_1 108N06000 1012015       1                      299
## 3: 108N06000_0101_10 108N06000 1012015      10                      321
## 4: 108N06000_0101_11 108N06000 1012015      11                      253
## 5: 108N06000_0101_12 108N06000 1012015      12                      321
## 6: 108N06000_0101_13 108N06000 1012015      13                      294
##    Travel_TIME_PASSENGER_VEHICLES Travel_TIME_FREIGHT_TRUCKS ADMIN_LEVE
## 1:                             NA                         NA        USA
## 2:                             NA                        299        USA
## 3:                             NA                        321        USA
## 4:                             NA                        253        USA
## 5:                             NA                        321        USA
## 6:                             NA                        294        USA
##    ADMIN_LE_1 ADMIN_LE_2 DISTANCE ROAD_NUMBE ROAD_NAME LATITUDE LONGITUDE
## 1:       Ohio   Williams  2.26922      OH-15           41.44172 -84.55062
## 2:       Ohio   Williams  2.26922      OH-15           41.44172 -84.55062
## 3:       Ohio   Williams  2.26922      OH-15           41.44172 -84.55062
## 4:       Ohio   Williams  2.26922      OH-15           41.44172 -84.55062
## 5:       Ohio   Williams  2.26922      OH-15           41.44172 -84.55062
## 6:       Ohio   Williams  2.26922      OH-15           41.44172 -84.55062
##    ROAD_DIREC  ORN_FID COUNTY divided SURF_TYP NHS_CDE HPMS ACCESS AADT_YR
## 1: Southbound 32246.93    WIL       U        G       N           N      14
## 2: Southbound 32246.93    WIL       U        G       N           N      14
## 3: Southbound 32246.93    WIL       U        G       N           N      14
## 4: Southbound 32246.93    WIL       U        G       N           N      14
## 5: Southbound 32246.93    WIL       U        G       N           N      14
## 6: Southbound 32246.93    WIL       U        G       N           N      14
##    FED_FACI PK_LANES MED_TYPE FED_MEDW BEGMP ENDMP   SEG_LNG cnty_rte
## 1:        2        4        1       NA  1.05     2 0.3891861 WIL0127R
## 2:        2        4        1       NA  1.05     2 0.3891861 WIL0127R
## 3:        2        4        1       NA  1.05     2 0.3891861 WIL0127R
## 4:        2        4        1       NA  1.05     2 0.3891861 WIL0127R
## 5:        2        4        1       NA  1.05     2 0.3891861 WIL0127R
## 6:        2        4        1       NA  1.05     2 0.3891861 WIL0127R
##    rte_nbr     aadt  aadt_bc  aadt_pt surf_wid no_lanes func_cls rodwycls
## 1:   0127R 8274.383 520.5784 7753.805 49.15682        4        2       10
## 2:   0127R 8274.383 520.5784 7753.805 49.15682        4        2       10
## 3:   0127R 8274.383 520.5784 7753.805 49.15682        4        2       10
## 4:   0127R 8274.383 520.5784 7753.805 49.15682        4        2       10
## 5:   0127R 8274.383 520.5784 7753.805 49.15682        4        2       10
## 6:   0127R 8274.383 520.5784 7753.805 49.15682        4        2       10
##    Total K A B C O DAYMTH Crash   spd_av spd_pv   spd_ft     date Month
## 1:     1 0 0 0 0 1   0101     0       NA     NA       NA 01012015    01
## 2:     1 0 0 0 0 1   0101     0 27.32171     NA 27.32171 01012015    01
## 3:     1 0 0 0 0 1   0101     0 25.44920     NA 25.44920 01012015    01
## 4:     1 0 0 0 0 1   0101     0 32.28930     NA 32.28930 01012015    01
## 5:     1 0 0 0 0 1   0101     0 25.44920     NA 25.44920 01012015    01
## 6:     1 0 0 0 0 1   0101     0 27.78637     NA 27.78637 01012015    01
##    Day Year MonthDay
## 1:  01 2015    01_01
## 2:  01 2015    01_01
## 3:  01 2015    01_01
## 4:  01 2015    01_01
## 5:  01 2015    01_01
## 6:  01 2015    01_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              42.5
##  2 01_02              40.1
##  3 01_03              38.9
##  4 01_04              40.6
##  5 01_05              38.3
##  6 01_06              36.2
##  7 01_07              36.7
##  8 01_08              38.0
##  9 01_09              35.1
## 10 01_10              40.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.6131, Lag order = 7, p-value = 0.03196
## alternative hypothesis: stationary
adf.test(deseasonal_cnt, alternative = "stationary")
## 
##  Augmented Dickey-Fuller Test
## 
## data:  deseasonal_cnt
## Dickey-Fuller = -3.5278, Lag order = 7, p-value = 0.04015
## 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 = -10.846, 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.34  0.34
##   [2,]  0.04 -0.09
##   [3,]  0.02  0.04
##   [4,] -0.05 -0.07
##   [5,] -0.03  0.01
##   [6,]  0.23  0.27
##   [7,]  0.58  0.51
##   [8,]  0.25 -0.09
##   [9,] -0.02 -0.13
##  [10,] -0.04 -0.05
##  [11,] -0.06  0.06
##  [12,]  0.00  0.09
##  [13,]  0.26  0.15
##  [14,]  0.51  0.16
##  [15,]  0.27  0.02
##  [16,]  0.00 -0.06
##  [17,] -0.03  0.01
##  [18,] -0.06  0.02
##  [19,] -0.01  0.03
##  [20,]  0.29  0.14
##  [21,]  0.46  0.07
##  [22,]  0.24 -0.02
##  [23,]  0.01 -0.01
##  [24,] -0.04  0.00
##  [25,] -0.06  0.03
##  [26,]  0.04  0.09
##  [27,]  0.24 -0.05
##  [28,]  0.46  0.15
##  [29,]  0.23 -0.05
##  [30,] -0.05 -0.12
##  [31,] -0.09 -0.05
##  [32,] -0.11 -0.04
##  [33,] -0.04 -0.04
##  [34,]  0.17 -0.04
##  [35,]  0.41  0.06
##  [36,]  0.25  0.07
##  [37,] -0.03 -0.03
##  [38,] -0.09 -0.06
##  [39,] -0.05  0.07
##  [40,]  0.01  0.03
##  [41,]  0.17 -0.03
##  [42,]  0.41  0.07
##  [43,]  0.26  0.02
##  [44,] -0.07 -0.08
##  [45,] -0.10  0.03
##  [46,] -0.08 -0.06
##  [47,] -0.04 -0.01
##  [48,]  0.16  0.01
##  [49,]  0.39  0.07
##  [50,]  0.20 -0.08
##  [51,] -0.03  0.06
##  [52,] -0.08  0.00
##  [53,] -0.07  0.05
##  [54,] -0.03 -0.02
##  [55,]  0.17  0.02
##  [56,]  0.39  0.04
##  [57,]  0.19 -0.01
##  [58,] -0.08 -0.06
##  [59,] -0.11 -0.03
##  [60,] -0.10 -0.02
##  [61,] -0.06  0.02
##  [62,]  0.15 -0.02
##  [63,]  0.39  0.04
##  [64,]  0.19 -0.02
##  [65,] -0.07 -0.01
##  [66,] -0.12 -0.06
##  [67,] -0.10  0.02
##  [68,] -0.09 -0.09
##  [69,]  0.14  0.05
##  [70,]  0.35  0.05
##  [71,]  0.17 -0.05
##  [72,] -0.07  0.01
##  [73,] -0.14 -0.03
##  [74,] -0.11  0.02
##  [75,] -0.08 -0.01
##  [76,]  0.13  0.00
##  [77,]  0.37  0.08
##  [78,]  0.15 -0.07
##  [79,] -0.03  0.05
##  [80,] -0.12 -0.01
##  [81,] -0.12  0.00
##  [82,] -0.09 -0.05
##  [83,]  0.09 -0.02
##  [84,]  0.32  0.01
##  [85,]  0.11 -0.05
##  [86,] -0.09  0.00
##  [87,] -0.13  0.00
##  [88,] -0.13  0.01
##  [89,] -0.12 -0.09
##  [90,]  0.06 -0.06
##  [91,]  0.29 -0.01
##  [92,]  0.09 -0.06
##  [93,] -0.15 -0.09
##  [94,] -0.16 -0.04
##  [95,] -0.15  0.02
##  [96,] -0.11  0.03
##  [97,]  0.07 -0.01
##  [98,]  0.27 -0.03
##  [99,]  0.08 -0.06
## [100,] -0.13  0.01
## [101,] -0.16  0.02
## [102,] -0.14  0.01
## [103,] -0.13 -0.07
## [104,]  0.04 -0.04
## [105,]  0.26  0.04
## [106,]  0.07 -0.02
## [107,] -0.13  0.05
## [108,] -0.16 -0.02
## [109,] -0.18  0.00
## [110,] -0.16 -0.06
## [111,]  0.01  0.04
## [112,]  0.22 -0.03
## [113,]  0.05  0.04
## [114,] -0.12  0.01
## [115,] -0.18 -0.04
## [116,] -0.17 -0.01
## [117,] -0.15  0.01
## [118,]  0.00  0.00
## [119,]  0.20  0.01
## [120,]  0.08  0.04
acf2(deseasonal_cnt)

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

Seasonility Not in Consideration

# Step 6: Fitting an ARIMA model
auto.arima(deseasonal_cnt, seasonal=FALSE)
## Series: deseasonal_cnt 
## ARIMA(2,1,1) 
## 
## Coefficients:
##          ar1      ar2      ma1
##       0.2855  -0.1739  -0.9480
## s.e.  0.0534   0.0532   0.0167
## 
## sigma^2 estimated as 1.018:  log likelihood=-519.3
## AIC=1046.6   AICc=1046.72   BIC=1062.19
# 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,1,3)(2,0,2)[30] 
## 
## Coefficients:
##          ar1      ar2      ma1     ma2      ma3     sar1     sar2     sma1
##       -0.580  -0.9230  -0.1916  0.2381  -0.8226  -0.0428  -0.1978  -0.3302
## s.e.   0.039   0.0358   0.0439  0.0434   0.0424   0.1580   0.2335   0.1657
##          sma2
##       -0.2569
## s.e.   0.2415
## 
## sigma^2 estimated as 0.7644:  log likelihood=-474.59
## AIC=969.18   AICc=969.8   BIC=1008.15
# 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)