-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts.rmd
630 lines (436 loc) · 17.3 KB
/
ts.rmd
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
---
title: "Traffic Forecasting"
author: "BRACU"
date: '`r Sys.Date()`'
output:
rmdformats::material:
highlight: kate
self_contained: true
code_folding: show
thumbnails: true
gallery: true
fig_width: 4
fig_height: 4
df_print: kable
runtime: shiny
---
```{r global, include=FALSE}
# import some libraries
library(knitr)
library(rmdformats)
library(reactable)
library(shinyWidgets)
library(shiny)
library(apexcharter)
library(dygraphs)
library(xts)
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(tibble)
library(lubridate)
library(forecast)
library(tsibble)
library(fable)
library(fabletools)
library(fable.prophet)
library(readr)
library(feasts)
library(fasster)
# mute dplyr messages
options(dplyr.summarise.inform = FALSE)
# load saved ( save.image("my_work_space.RData")) workspace with all data and models early computed to make the report fast
#
load("my_work_space.RData")
```
```{css, echo=FALSE}
/* some css modification */
.col-xs-9 {
width: 85%;
}
.col-xs-3 {
width: 15%;
}
```
<!-- import fontawesome icons -->
<link href="https://use.fontawesome.com/releases/v5.0.7/css/all.css" rel="stylesheet">
# <i class="fas fa-play-circle"></i> Introduction
<!-- include the image in introduction -->
<div
<div align="center" >
<img src="coverimage.png">
</div>
<div
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
min-height: 155px;
position: relative;
z-index: 3;">
</div>
## Traffic Forecasting Using Time-series Analysis
The goal of this project is to implement forecasting models to predict the volume of road traffic within 24 hours.
We will develop several models (including ARIMA) then we compare the performances to select the best algorithm for each route
# <i class="fas fa-cogs"></i> Data Preparation
## The dataset
We use R [readr](https://readr.tidyverse.org/) package which is part of tidyverse, then we display the first 20 rows :
```{r eval=FALSE, message=FALSE, warning=FALSE}
# read the data and store it in the dataframe named dataset
dataset <- read_csv("dataset (1).csv")
```
In this project we are using the [reactable](https://glin.github.io/reactable/) package to display all tables
```{r}
# display using reactable with some options
reactable(dataset %>% head(20),
bordered = TRUE, highlight = TRUE,
defaultColDef = colDef( headerStyle = list(background = "#4285F4")),
defaultPageSize = 4 )
```
lets check the colums types
```{r}
# overview of tha data
glimpse(dataset)
```
at first sight we find :
- There is __60 168__ observations and __29__ columns
- The columns `date` and `time` are of type `charachter`, so we need to concactenat them then convert to `timestamp`
- There is no ID column for the road : we need to create one.
- We identifie the column `Total Vehicles` as target variable
Let inspect the `road_name` variable and see the list of unique road names
```{r}
dataset %>%
distinct(road_name) %>% # get distinct road name
arrange(road_name) %>% # order by names then display
reactable(highlight = TRUE,defaultPageSize = 5, # display the dable
defaultColDef = colDef( headerStyle = list(background = "#4285F4")))
```
As we can see this column contains 111 road name but we notice that some names are implicitly duplicated because of abbreviations and writing errors: for example : Abbotsford Street vs Abbottsford Street, Domain St vs Domain Street
In total, we have identified 4 errors that we will correct manually, in biggest dataset we can use the Levenshtein distance or advanced NLP techniques can be used to do this work.
To process the dates we use the lubridate package
We build our target variable y as the aggregation of road traffic per road and per hour
```{r}
dataset %>%
mutate(road_name= case_when(
road_name%in%c("Abbotsford Street", "Abbottsford Street")~"Abbottsford Street", # equivalent if then
road_name%in%c("Domain St", "Domain Street")~"Domain Street", # equivalent if then
road_name%in%c("Stubbs street", "Stubbs Street")~"Stubbs Street", # equivalent if then
road_name%in%c("Wells St", "Walsh Street")~"Walsh Street", # equivalent if then
T~road_name # in all other cases render the correspanding road name
) ) %>%
mutate(date= dmy_hms(paste0(date, time) )) %>% # create timestamp column using the function dmy_hms from lubridate and paste0 allow concatenation of strings
select(road_name, date, `Total Vehicles`) %>% # selecting important columns
group_by(road_name, date) %>%
summarise(y= sum(`Total Vehicles`,na.rm = T )) %>% # calculate target variable by road and date
head(10) %>% # show only 10 first row
reactable(highlight = T,defaultPageSize = 5,
defaultColDef = colDef( headerStyle = list(background = "#4285F4")))
```
Now lets check the historical data for each road :
- we calculate the minimum and maximum date per route
- the number of hours expected between these two dates ( the diffrence in hours unite)
- the number of hours in the dataset
- finnaly we select only roads with complete data ( `expected_rows`==`n_rows` )
```{r eval=FALSE}
dataset %>%
mutate(road_name= case_when(
road_name%in%c("Abbotsford Street", "Abbottsford Street")~"Abbottsford Street", # equivalent if then
road_name%in%c("Domain St", "Domain Street")~"Domain Street", # equivalent if then
road_name%in%c("Stubbs street", "Stubbs Street")~"Stubbs Street", # equivalent if then
road_name%in%c("Wells St", "Walsh Street")~"Walsh Street", # equivalent if then
T~road_name # in all other cases render the correspanding road name
) ) %>%
mutate(date= dmy_hms(paste0(date, time) )) %>% # create timestamp column using the function dmy_hms from lubridate and paste0 allow concatenation of strings
select(road_name, date, `Total Vehicles`) %>% # selecting important columns
group_by(road_name, date) %>%
summarise(y= sum(`Total Vehicles`,na.rm = T )) %>% # calculate target variable by road and date
ungroup() %>% # we ungroup the dataframe then group by road name
group_by(road_name) %>%
summarise(
min_date = min(date), # minimum date by road
max_date = max(date), # maximum date by road
n_rows = n() # total number of records
) %>%
mutate(expected_rows =as.integer(difftime(max_date,min_date, units = "hours"))+1) %>% #expect number of hours
filter(expected_rows==n_rows) -> clean_road # get only roads with complete data and save the result in the dataframe clean_road
```
```{r}
# display the clean dataframe
reactable(clean_road,highlight = T,defaultPageSize = 5,
defaultColDef = colDef( headerStyle = list(background = "#4285F4")))
```
## Visualiziation
After this first stage of data cleaning, we can visualize the temporal evolution of the traffic by road using the `apexchart` package and the `shiny` package to interaction purpose.
You can change the road using the selector in the top of the graph
```{r echo=FALSE}
df<- dataset %>%
mutate(road_name= case_when(
road_name%in%c("Abbotsford Street", "Abbottsford Street")~"Abbottsford Street",
road_name%in%c("Domain St", "Domain Street")~"Domain Street",
road_name%in%c("Stubbs street", "Stubbs Street")~"Stubbs Street",
road_name%in%c("Wells St", "Walsh Street")~"Walsh Street",
T~road_name
) ) %>%
mutate(date= dmy_hms(paste0(date, time) )) %>%
filter(road_name%in%clean_road$road_name) %>% # keep only the the road with clean data
select(road_name, date, `Total Vehicles`) %>%
group_by(road_name, date) %>%
summarise(y= sum(`Total Vehicles`,na.rm = T ))
# select input to choose the road name
fluidRow(
pickerInput(
inputId = "road_name",
label = "Choose a road name",
choices = unique(df$road_name)
)
)
# render the interactive plot
fluidRow(
apexchartOutput('plot',width = "100%")
)
# build the interactive chart
output$plot <- renderApexchart({
apex(data = df[df$road_name==input$road_name,] %>% mutate(date=as.POSIXct(date)),
type = "area", mapping = aes(x = date, y = y)) %>%
ax_fill(
type='gradient',
gradient=list (
shadeIntensity= 1,
inverseColors=F,
opacityFrom=0.5,
opacityTo= 0,
stops= c(0, 90, 100)
)
)
})
```
As we can see on the different graphs, the seasonality is very strong, and all the series seem stationary
# <i class="fas fa-chart-line"></i> Forecasting {.tabset}
In this part we will forecast traffic by road using __5 models__: `ARIMA`, `ETS`, `SNAIVE`, `PROPHET` and the last one is the combinaison of all models.
We will use the following packages :
<div align="center" >
<img src="packages.png">
</div>
- [fable](https://github.com/tidyverts/fable) : provides a collection of commonly used univariate and multivariate time series forecasting models including exponential smoothing via state space models and automatic ARIMA modelling. These models work within the fable framework, which provides the tools to evaluate, visualise, and combine models in a workflow consistent with the tidyverse.
- [prophet](https://facebook.github.io/prophet/) : is open source software released by Facebook's Core Data Science team.It works best with time series that have strong seasonal effects and several seasons of historical data
We start by converting the data table to tsibble format and initiate a `future` with `multisession` plan to parallelize calculations
```{r eval=F}
# setup parallel backend
library(future)
plan(multisession)
```
```{r eval=FALSE}
# we convert the dataframe to a tsibble where the index is the date and the key id the road name
sdf <- df %>%
ungroup() %>%
as_tsibble(key = "road_name", index = 'date' )
```
## 📈 Single time series
Although the _fable_ package is designed to handle many time series, we will be begin by demonstrating its use on a single time series. For this purpose, we will extract the traffic data of Agnes Street
```{r eval=FALSE}
Agnes <- sdf %>%
filter(
road_name == "Agnes Street",
)
```
For this data set, we include all models in a single call to the `model()` function like this.
```{r eval=FALSE}
Agnes %>%
model(
arima = ARIMA(y), # fit best ARIMA model using auto.arima
ets = ETS(y) , # fit the best ETS model
snaive = SNAIVE(y), # fit seasonal naive model
prophet = prophet(y~ season("day")+season("week")+season("year")) # fit prophet with multiple seasons
) %>%
mutate(
mixed = (ets + arima + snaive +prophet) / 4
)-> models
```
```{r}
print(models)
```
The returned object is called a _mable_ or model table, where each cell corresponds to a fitted model. Because we have only fitted models to one time series, this mable has only one row.
To forecast all models, we pass the object to the `forecast` function with the horizon of 24 hours
```{r eval=FALSE}
fc <- models %>%
forecast(h = 24)
```
```{r}
print(fc)
```
The return object is a "fable" or forecast table with the following characteristics:
- the __.model__ column becomes an additional key;
- the __y__ column contains the estimated probability distribution of the response variable in future time periods;
- the __.mean__ column contains the point forecasts equal to the mean of the probability distribution.
Now, lets plot 24h forecasts provided by all models ( you can select/deselect models in the plot )
```{r echo=FALSE}
apex(data = fc %>% as.data.frame() %>% select(-y) %>%
mutate(.mean=round(.mean)) %>%
rbind(Agnes %>% as.data.frame() %>%
mutate(.model='historical') %>%
rename( .mean=y)
), type = "area", mapping = aes(x = date, y = .mean, group=.model)) %>%
ax_fill(
type='gradient',
gradient=list (
shadeIntensity= 1,
inverseColors=F,
opacityFrom=0.5,
opacityTo= 0,
stops= c(0, 90, 100)
)
) %>%
ax_legend(position = "right") %>%
ax_tooltip(
enabled= T,
shared= T
)
```
## ⚡ All time series
To scale this up to include all series in the traffic data set requires no more work — we use exactly the same code.
```{r eval=FALSE}
sdf %>%
model(
arima = ARIMA(y),
ets = ETS(y) ,
snaive = SNAIVE(y),
prophet = prophet(y~ season("day")+season("week")+season("year")),
) %>%
mutate(
mixed = (ets + arima + snaive +prophet) / 4
)-> all_models
```
Now the mable includes models for every road
```{r}
print(all_models)
```
```{r eval=FALSE}
all_fc <- all_models %>%
forecast(h = 24)
```
```{r}
print(all_fc)
```
```{r echo=FALSE}
fluidRow(
pickerInput(
inputId = "road_name2",
label = "Choose a road name",
selected ="Abbottsford Street",
choices = unique(df$road_name)
)
)
fluidRow(
apexchartOutput('plot2',width = "100%")
)
output$plot2 <- renderApexchart({
apex(data = all_fc %>% as.data.frame() %>%
filter(road_name==input$road_name2) %>%
select(-y) %>%
mutate(.mean=round(.mean)) %>%
rbind(sdf %>% as.data.frame() %>%
filter(road_name==input$road_name2) %>%
mutate(.model='historical') %>%
rename( .mean=y)
), type = "area", mapping = aes(x = date, y = .mean, group=.model)) %>%
ax_fill(
type='gradient',
gradient=list (
shadeIntensity= 1,
inverseColors=F,
opacityFrom=0.5,
opacityTo= 0,
stops= c(0, 90, 100)
)
) %>%
ax_legend(position = "right") %>%
ax_tooltip(
enabled= T,
shared= T
)
})
```
## 🎯 Accuracy calculations
<div align="center" >
<img src="traintest.png">
</div>
To compare the forecast accuracy of these models, we will create a training data set containing all data except the last 24 hours. We will then forecast the remaining hours in the data set and compare the results with the actual values.
```{r eval=FALSE}
sdf %>%
group_by(road_name) %>%
slice(1:(n()-24)) %>% # train the models in the data exept the last 24 hours
ungroup() %>%
model(
arima = ARIMA(y),
ets = ETS(y) ,
snaive= SNAIVE(y),
prophet = prophet(y~ season("day")+season("week")+season("year"))
) %>%
mutate(
mixed = (ets + arima + snaive +prophet) / 4
)-> models_train
# Forecast the remaning 24 hours in each road
fabletools::forecast( object = models_train, sdf %>%
group_by(road_name) %>%
slice((n()-24):n()) %>% ungroup() ) -> fc_train
```
```{r echo=FALSE}
# choosing the road interactively
fluidRow(
pickerInput(
inputId = "road_name3",
label = "Choose a road name",
selected ="Abbottsford Street",
choices = unique(df$road_name)
)
)
fluidRow(
apexchartOutput('plot3',width = "100%")
)
output$plot3 <- renderApexchart({
apex(data = fc_train %>% as.data.frame() %>%
filter(road_name==input$road_name3) %>%
select(-y) %>%
mutate(.mean=round(.mean)) %>%
rbind(sdf %>%
ungroup() %>%
as.data.frame() %>%
filter(road_name==input$road_name3) %>%
mutate(.model='historical') %>%
rename( .mean=y)
), type = "area", mapping = aes(x = date, y = .mean, group=.model)) %>%
ax_fill(
type='gradient',
gradient=list (
shadeIntensity= 1,
inverseColors=F,
opacityFrom=0.5,
opacityTo= 0,
stops= c(0, 90, 100)
)
) %>%
ax_legend(position = "right") %>%
ax_tooltip(
enabled= T,
shared= T
)
})
```
### Forecast errors
Now to check the accuracy, we use the `accuracy()` function.
A forecast "error" is the difference between an observed value and its forecast. Here "error" does not mean a mistake, it means the unpredictable part of an observation.
We can measure forecast accuracy by summarising the forecast errors in different ways.
The two most commonly used scale-dependent measures are based on the absolute errors or squared errors:
Mean absolute error: __MAE__ and Root mean squared error: __RMSE__
When comparing forecast methods applied to a single time series, or to several time series with the same units, the MAE is popular as it is easy to both understand and compute. A forecast method that minimises the MAE will lead to forecasts of the median, while minimising the RMSE will lead to forecasts of the mean. Consequently, the RMSE is also widely used, despite being more difficult to interpret.
__Percentage errors__
The Percentage errors have the advantage of being unit-free, and so are frequently used to compare forecast performances between data sets.
Measures based on percentage errors have the disadvantage of being infinite or undefined if $yt=0$ for any $t$ in the period of interest, and having extreme values if any $yt$ is close to zero.
```{r eval=F}
fabletools::accuracy(fc_train, sdf) %>% select( .model, road_name, .type, RMSE, MAE, MAPE) -> res
```
You can filter road and get the metrics by typing the name in `search`
```{r}
reactable(
res %>% mutate(MAE= round(MAE, 2),RMSE= round(RMSE, 2)) %>% arrange(road_name),
defaultColDef = colDef( headerStyle = list(background = "#4285F4")),
searchable = T,defaultPageSize = 5,highlight = T)
```