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)
📅 dateDate | 123 numberBigint | |
|---|---|---|
| 1 | 1998-05-01 | 0 |
| 2 | 1998-10-01 | 23495 |
| 3 | 2000-01-01 | 778 |
| 4 | 2001-06-01 | 8433 |
| 5 | 2017-01-01 | 2408 |
| 6 | 2009-04-01 | 1078 |
| 7 | 2003-08-01 | 43736 |
| 8 | 2004-11-01 | 30763 |
| 9 | 2004-12-01 | 17524 |
| 10 | 2012-04-01 | 2193 |
| 11 | 2013-07-01 | 7310 |
| 12 | 2005-05-01 | 3210 |
| 13 | 2005-06-01 | 5811 |
| 14 | 1998-03-01 | 0 |
| 15 | 1999-01-01 | 1081 |
| 16 | 1999-07-01 | 8756 |
| 17 | 1999-12-01 | 4376 |
| 18 | 2001-08-01 | 31887 |
| 19 | 2001-11-01 | 15639 |
| 20 | 2016-06-01 | 6339 |
| 21 | 2016-12-01 | 8613 |
| 22 | 2007-04-01 | 415 |
| 23 | 2008-09-01 | 39445 |
| 24 | 2009-05-01 | 2593 |
| 25 | 2009-08-01 | 17559 |
| 26 | 2009-12-01 | 9494 |
| 27 | 2010-02-01 | 2386 |
| 28 | 2003-06-01 | 6506 |
| 29 | 2004-03-01 | 2040 |
| 30 | 2011-02-01 | 973 |
| 31 | 2011-12-01 | 9828 |
| 32 | 2012-01-01 | 2491 |
| 33 | 2015-05-01 | 2384 |
| 34 | 2006-01-01 | 3255 |
| 35 | 1998-12-01 | 4448 |
| 36 | 2017-07-01 | 22911 |
| 37 | 2017-08-01 | 49485 |
| 38 | 2017-09-01 | 110988 |
| 39 | 2017-10-01 | 42720 |
| 40 | 2009-02-01 | 1140 |
| 41 | 2002-10-01 | 47722 |
| 42 | 2011-03-01 | 937 |
| 43 | 2011-07-01 | 8524 |
| 44 | 2012-11-01 | 13586 |
| 45 | 2013-11-01 | 12150 |
| 46 | 2015-03-01 | 2202 |
| 47 | 2015-04-01 | 2573 |
| 48 | 2015-10-01 | 49979 |
| 49 | 2015-11-01 | 27529 |
| 50 | 1998-01-01 | 0 |
| 51 | 2001-12-01 | 6201 |
| 52 | 2007-02-01 | 1751 |
| 53 | 2009-06-01 | 2962 |
| 54 | 2010-01-01 | 2851 |
| 55 | 2002-02-01 | 1570 |
| 56 | 2002-06-01 | 10839 |
| 57 | 2003-11-01 | 23572 |
| 58 | 2004-09-01 | 83500 |
| 59 | 2011-06-01 | 4578 |
| 60 | 2012-05-01 | 3240 |
| 61 | 2012-07-01 | 13507 |
| 62 | 2012-12-01 | 6823 |
| 63 | 2013-09-01 | 31585 |
| 64 | 2006-12-01 | 5027 |
| 65 | 2016-02-01 | 4147 |
| 66 | 2017-04-01 | 1559 |
| 67 | 2017-05-01 | 2506 |
| 68 | 2008-02-01 | 1275 |
| 69 | 2009-09-01 | 29430 |
| 70 | 2009-10-01 | 24202 |
| 71 | 2003-01-01 | 5091 |
| 72 | 2003-07-01 | 11804 |
| 73 | 2004-05-01 | 3535 |
| 74 | 2013-02-01 | 1587 |
| 75 | 2014-06-01 | 6483 |
| 76 | 2014-12-01 | 10938 |
| 77 | 2006-02-01 | 1666 |
| 78 | 2016-04-01 | 3972 |
| 79 | 2008-06-01 | 1287 |
| 80 | 2008-11-01 | 12778 |
| 81 | 2010-03-01 | 2417 |
| 82 | 2010-06-01 | 3642 |
| 83 | 2010-12-01 | 6856 |
| 84 | 2002-05-01 | 3818 |
| 85 | 2003-10-01 | 43295 |
| 86 | 2005-07-01 | 15663 |
| 87 | 2006-04-01 | 792 |
| 88 | 2000-02-01 | 561 |
| 89 | 2000-04-01 | 537 |
| 90 | 2000-05-01 | 2097 |
| 91 | 2000-06-01 | 6275 |
| 92 | 2001-03-01 | 1268 |
| 93 | 2016-03-01 | 3796 |
| 94 | 2008-07-01 | 4507 |
| 95 | 2008-12-01 | 4995 |
| 96 | 2010-10-01 | 31485 |
| 97 | 2004-06-01 | 14262 |
| 98 | 2011-04-01 | 1152 |
| 99 | 2013-03-01 | 1969 |
| 100 | 2005-02-01 | 2153 |
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_variance | 0.8603952980284197 |
| max_error | 31495.699489466093 |
| median_absolute_error | 2584.3152 |
| mean_absolute_error | 5443.646700206962 |
| mean_squared_error | 78073484.64492206 |
| root_mean_squared_error | 8835.920135725653 |
| r2 | 0.8500646923570399 |
| r2_adj | 0.8469410401144781 |
| aic | 913.0835845138098 |
| bic | 916.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