Loading...

Time Series

Time series models are a type of regression on a dataset with a timestamp label.

The following example creates a time series model to predict the number of forest fires in Brazil with the amazon dataset.

from vastorbit.datasets import load_amazon

amazon = load_amazon().groupby("date", "SUM(number) AS number")
amazon.head(100)
📅
date
Date
100%
123
number
Bigint
100%
11998-05-010
21998-10-0123495
32000-01-01778
42001-06-018433
52017-01-012408
62009-04-011078
72003-08-0143736
82004-11-0130763
92004-12-0117524
102012-04-012193
112013-07-017310
122005-05-013210
132005-06-015811
141998-03-010
151999-01-011081
161999-07-018756
171999-12-014376
182001-08-0131887
192001-11-0115639
202016-06-016339
212016-12-018613
222007-04-01415
232008-09-0139445
242009-05-012593
252009-08-0117559
262009-12-019494
272010-02-012386
282003-06-016506
292004-03-012040
302011-02-01973
312011-12-019828
322012-01-012491
332015-05-012384
342006-01-013255
351998-12-014448
362017-07-0122911
372017-08-0149485
382017-09-01110988
392017-10-0142720
402009-02-011140
412002-10-0147722
422011-03-01937
432011-07-018524
442012-11-0113586
452013-11-0112150
462015-03-012202
472015-04-012573
482015-10-0149979
492015-11-0127529
501998-01-010
512001-12-016201
522007-02-011751
532009-06-012962
542010-01-012851
552002-02-011570
562002-06-0110839
572003-11-0123572
582004-09-0183500
592011-06-014578
602012-05-013240
612012-07-0113507
622012-12-016823
632013-09-0131585
642006-12-015027
652016-02-014147
662017-04-011559
672017-05-012506
682008-02-011275
692009-09-0129430
702009-10-0124202
712003-01-015091
722003-07-0111804
732004-05-013535
742013-02-011587
752014-06-016483
762014-12-0110938
772006-02-011666
782016-04-013972
792008-06-011287
802008-11-0112778
812010-03-012417
822010-06-013642
832010-12-016856
842002-05-013818
852003-10-0143295
862005-07-0115663
872006-04-01792
882000-02-01561
892000-04-01537
902000-05-012097
912000-06-016275
922001-03-011268
932016-03-013796
942008-07-014507
952008-12-014995
962010-10-0131485
972004-06-0114262
982011-04-011152
992013-03-011969
1002005-02-012153

The feature date tells us that we should be working with a time series model. To do predictions on time series, we use previous values called lags.

To help visualize the seasonality of forest fires, we’ll draw some autocorrelation plots.

amazon.acf(
    ts = "date",
    column = "number",
    p = 24,
)
amazon.pacf(
    ts = "date",
    column = "number",
    p = 8,
)

Forest fires follow a predictable, seasonal pattern, so it should be easy to predict future forest fires with past data.

vastorbit offers several models, including a multiple time series model. For this example, let’s use a ARIMA model.

from vastorbit.machine_learning.vast import ARIMA

model = ARIMA(order = (12, 0, 0))
model.fit(
    amazon,
    y = "number",
    ts = "date",
)

Just like with other regression models, we’ll evaluate our model with the report() method.

model.report(npredictions = 50, start = 50)
value
explained_variance0.8603952980284197
max_error31495.699489466093
median_absolute_error2584.3152
mean_absolute_error5443.646700206962
mean_squared_error78073484.64492206
root_mean_squared_error8835.920135725653
r20.8500646923570399
r2_adj0.8469410401144781
aic913.0835845138098
bic916.4820986097724

We can also draw our model using one-step ahead and dynamic forecasting.

model.plot(amazon, npredictions = 40,)

In the next lesson, we’ll go over Regression