-
Notifications
You must be signed in to change notification settings - Fork 0
/
(Code) Road Traffic Fatalities Prediction Model through Time Series Analysis.R
207 lines (169 loc) · 4.82 KB
/
(Code) Road Traffic Fatalities Prediction Model through Time Series Analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#Developed by MD. ZUBAYER
plot(UKDriverDeaths)
acf(UKDriverDeaths)
ggsubseriesplot(UKDriverDeaths)
length(UKDriverDeaths)
View(UKDriverDeaths)
#train and test split
traindata=ts(UKDriverDeaths[1:134], frequency = 12, start=c(1969,1))
length(traindata)
testdata=ts(UKDriverDeaths[135:192], frequency = 12, start=c(1980,3))
length(testdata)
#stationary check
library(tseries)
adf.test(traindata)##p-value = 0.01 stationary
kpss.test(traindata)##p-value = 0.04793, non-stationary
##taking seasonal and non seasonal differences
Dtraindata=diff(traindata, differences = 1, lag = 1)
adf.test(Dtraindata)##p-value = 0.01 stationary
kpss.test(Dtraindata)##p-value = 0.1 stationary
#Model_1
library(forecast)
tsdisplay(Dtraindata)
M1=Arima(traindata, order = c(1, 1, 0), seasonal = list(order = c(1, 1, 1), period = 12))
M1 ##AIC=1566.68 AICc=1567.03 BIC=1577.87
### Residuals Diagnostic
library(itsmr)
test(M1$residuals)
##Forecasting
n=length(testdata)
n
forecast_M1 <- forecast::forecast(M1,h=n)
plot(forecast_M1)
forecast_M1$mean
### Calculating Forecasting Accuracy Measures
MM1=forecast::forecast(M1, h=n)$mean
err=testdata - MM1
ME=sum(err)/n
print(ME)
MSE=sum(err^2)/n
print(MSE)
RMSE=sqrt(MSE)
print(RMSE)##176.9536
MAE=sum(abs(err))/n
print(MAE)
PE=(err/testdata)*100
MPE=sum(PE)/n
print(MPE)
MAPE=sum(abs(PE))/n
print(MAPE)
plot(traindata)
### Using Log transformation
Ltraindata=log(traindata)
plot(Ltraindata)
adf.test(Ltraindata)## P_val=0.01 stationary
kpss.test(Ltraindata)##p-value = 0.04191 non - stationary
#Taking Difference
DLtraindata=diff(Ltraindata, differences = 1, lag=1)
kpss.test(DLtraindata)##p-value = 0.1
#Model_2
library(forecast)
tsdisplay(DLtraindata)
M2=Arima(Ltraindata, order = c(2, 1, 1), seasonal = list(order = c(1, 0, 1), period = 12))
M2 ##AIC=-262.65 AICc=-261.98 BIC=-245.31
### Residuals Diagnostic
library(itsmr)
test(exp(M2$residuals))
##Forecasting
n=length(testdata)
n
forecast_M2 <- forecast::forecast(M2,h=n)
plot(forecast_M2)
#Back Transformation for RMSE
forecast_MM2 <- forecast::forecast(M2,h=n)$mean
MM2=exp(forecast_MM2)
forecast_M2_backtransformed <- exp(forecast_M2$mean)
forecast_M2_backtransformed
round(forecast_M2_backtransformed,2)
### Calculating Forecasting Accuracy Measures
err=testdata - MM2
ME=sum(err)/n
print(ME)
MSE=sum(err^2)/n
print(MSE)
RMSE=sqrt(MSE)
print(RMSE)##192.5335
MAE=sum(abs(err))/n
print(MAE)
PE=(err/testdata)*100
MPE=sum(PE)/n
print(MPE)
MAPE=sum(abs(PE))/n
print(MAPE)
#Box-Cox Transformation
BXtraindata=BoxCox(traindata,lambda = BoxCox.lambda(traindata))
plot(BXtraindata)
acf(BXtraindata)
#stationary check
library(tseries)
adf.test(BXtraindata)##p-value = 0.01 stationary
kpss.test(BXtraindata)##p-value = 0.04726, non-stationary
##taking seasonal and non seasonal differences
DBXtraindata=diff(BXtraindata, differences = 1)
kpss.test(DBXtraindata)##p-value = 0.1 stationary
adf.test(DBXtraindata)
#Model_3
library(forecast)
tsdisplay(DBXtraindata)
M3=Arima(BXtraindata, order = c(1, 1, 1), seasonal = list(order = c(1, 0, 3), period = 12))
M3 ##AIC=1506.52 AICc=1507.42 BIC=1526.76
### Residuals Diagnostic
library(itsmr)
test(M3$residuals)
##Forecasting
forecast_M3 <- forecast::forecast(M3,h=n)
plot(forecast_M3)
forecast_M3_back <- forecast_M3
forecast_M3_back$mean <- InvBoxCox(forecast_M3$mean, lambda = BoxCox.lambda(traindata))
forecast_M3_back$mean
#Back Transformation for RMSE
MM3 <- forecast::InvBoxCox(forecast_M3$mean, lambda = BoxCox.lambda(traindata))
### Calculating Forecasting Accuracy Measures
err=testdata - MM3
ME=sum(err)/n
print(ME)
MSE=sum(err^2)/n
print(MSE)
RMSE=sqrt(MSE)
print(RMSE)##202.9743
MAE=sum(abs(err))/n
print(MAE)
PE=(err/testdata)*100
MPE=sum(PE)/n
print(MPE)
MAPE=sum(abs(PE))/n
print(MAPE)
#Model 4: Auto Arima
M4=auto.arima(traindata)
M4
#ARIMA(0,1,1)(2,1,0)[12] AIC=1569.65 AICc=1570 BIC=1580.84
### Residuals Diagnostic
library(itsmr)
test(M4$residuals)
##Forecasting
forecast_M4 <- forecast::forecast(M4,h=n)
plot(forecast_M4)
forecast_M4$mean
### Calculating Forecasting Accuracy Measures
MM4=forecast::forecast(M4, h=n)$mean
err=testdata - MM4
ME=sum(err)/n
print(ME)
MSE=sum(err^2)/n
print(MSE)
RMSE=sqrt(MSE)
print(RMSE)##184.5308
MAE=sum(abs(err))/n
print(MAE)
PE=(err/testdata)*100
MPE=sum(PE)/n
print(MPE)
MAPE=sum(abs(PE))/n
print(MAPE)
# Train the best model on the entire dataset
RM_M1 <- Arima(UKDriverDeaths, order = c(1, 1, 0), seasonal = list(order = c(1, 1, 1), period = 12))
# Forecast 10 points ahead
forecast_result <- forecast::forecast(RM_M1,h=10)
# Plot original data and forecasted values
plot(forecast_result, main = "Forecast for UKDriverDeaths Dataset", xlab = "Year", ylab = "Deaths")
forecast_result$mean