O pacote R fable fornece métodos e ferramentas para visualizar e analisar previsões de séries temporais univariadas incluindo os modelos “Exponential smoothing via state space models” e “automatic ARIMA modelling”. Dados, modelos e objetos são todos armazenados em formato tidy(arrumado).
Fable package
O pacote ainda não está no CRAN e foi apresentado recentemente pelo Hyndman no ISF 20181.
remove(list = ls())
library(tidyverse)
if(!require(fpp2)) install.packages("fpp2")
library(fpp2)
head(fpp2::auscafe)
## Apr May Jun Jul Aug Sep
## 1982 0.3424 0.3421 0.3287 0.3385 0.3315 0.3419
if(!require(tsibble)) install.packages("tsibble") # Tidy Temporal Data Frames and Tools
library(tsibble)
cafe <- as_tsibble(fpp2::auscafe)
cafe
## # A tsibble: 426 x 2 [1MONTH]
## index value
## <mth> <dbl>
## 1 1982 abr 0.342
## 2 1982 mai 0.342
## 3 1982 jun 0.329
## 4 1982 jul 0.338
## 5 1982 ago 0.332
## 6 1982 set 0.342
## 7 1982 out 0.358
## 8 1982 nov 0.375
## 9 1982 dez 0.433
## 10 1983 jan 0.369
## # ... with 416 more rows
if(!require(devtools)) install.packages("devtools")
library(devtools)
if(!require(fable)) install_github("tidyverts/fable", force = TRUE) # antes de instala-lom instale o pacote tidyverse
library(fable)
cafe %>% ETS(formula = value)
## # A mable: 1 model [1MONTH]
## data model
## <list> <model>
## 1 <tsibble [426 x 2]> ETS(M,A,M)
cafe %>% ETS(value) %>% summary()
## ETS(M,A,M)
##
## Call:
## ETS(data = ., formula = value)
##
## Smoothing parameters:
## alpha = 0.6263
## beta = 0.0065
## gamma = 0.0755
##
## Initial states:
## l = 0.3477
## b = 0.0038
## s = 0.996 0.9357 1.0124 1.1498 1.0105 1.0106
## 0.9831 0.9911 0.9918 0.9509 0.9968 0.9714
##
## sigma: 0.0249
##
## AIC AICc BIC
## -319.1016 -317.6016 -250.1762
##
## Training set error measures:
## ME RMSE MAE MPE MAPE
## Training set 0.002601818 0.03997611 0.02952838 0.1121434 1.982609
## MASE ACF1
## Training set 0.2826048 0.1249781
cafe %>% ETS(value) %>% forecast() %>% summary()
## # A tsibble: 24 x 4 [1MONTH]
## index mean `80%` `95%`
## <mth> <dbl> <hilo> <hilo>
## 1 2017 out 3.83 [3.710682, 3.955486]80 [3.645886, 4.020282]95
## 2 2017 nov 3.81 [3.669399, 3.957380]80 [3.593176, 4.033603]95
## 3 2017 dez 4.22 [4.036861, 4.398464]80 [3.941150, 4.494175]95
## 4 2018 jan 3.83 [3.644415, 4.007830]80 [3.548225, 4.104020]95
## 5 2018 fev 3.51 [3.332694, 3.696526]80 [3.236394, 3.792827]95
## 6 2018 mar 3.87 [3.654231, 4.085779]80 [3.540007, 4.200003]95
## 7 2018 abr 3.78 [3.558549, 4.009072]80 [3.439302, 4.128318]95
## 8 2018 mai 3.81 [3.570956, 4.052222]80 [3.443573, 4.179605]95
## 9 2018 jun 3.68 [3.433373, 3.923180]80 [3.303729, 4.052825]95
## 10 2018 jul 3.88 [3.604236, 4.146000]80 [3.460840, 4.289396]95
## # ... with 14 more rows
cafe %>% ETS(value) %>% forecast() %>% summary(level = 90)
## # A tsibble: 24 x 3 [1MONTH]
## index mean `90%`
## <mth> <dbl> <hilo>
## 1 2017 out 3.83 [3.675982, 3.990186]90
## 2 2017 nov 3.81 [3.628580, 3.998199]90
## 3 2017 dez 4.22 [3.985606, 4.449719]90
## 4 2018 jan 3.83 [3.592904, 4.059341]90
## 5 2018 fev 3.51 [3.281124, 3.748097]90
## 6 2018 mar 3.87 [3.593062, 4.146948]90
## 7 2018 abr 3.78 [3.494690, 4.072931]90
## 8 2018 mai 3.81 [3.502740, 4.120438]90
## 9 2018 jun 3.68 [3.363946, 3.992607]90
## 10 2018 jul 3.88 [3.527445, 4.222791]90
## # ... with 14 more rows
cafe %>% ETS(value) %>% forecast() %>% autoplot()
cafe %>% ETS(value) %>% forecast() %>% autoplot()
cafe %>% ARIMA(log(value)) %>% forecast() %>% summary()
## # A tsibble: 24 x 4 [1MONTH]
## index mean `80%` `95%`
## <mth> <dbl> <hilo> <hilo>
## 1 2017 out 3.81 [3.695757, 3.925596]80 [3.637208, 3.988787]95
## 2 2017 nov 3.79 [3.652255, 3.926338]80 [3.582969, 4.002265]95
## 3 2017 dez 4.17 [3.994774, 4.338391]80 [3.908470, 4.434187]95
## 4 2018 jan 3.73 [3.550551, 3.903099]80 [3.462690, 4.002135]95
## 5 2018 fev 3.40 [3.222464, 3.571614]80 [3.135906, 3.670200]95
## 6 2018 mar 3.77 [3.563436, 3.984984]80 [3.459526, 4.104676]95
## 7 2018 abr 3.70 [3.483283, 3.925831]80 [3.374741, 4.052098]95
## 8 2018 mai 3.76 [3.520066, 3.996561]80 [3.403747, 4.133139]95
## 9 2018 jun 3.66 [3.413722, 3.903254]80 [3.294761, 4.044185]95
## 10 2018 jul 3.88 [3.607122, 4.151691]80 [3.475347, 4.309111]95
## # ... with 14 more rows
Quarterly time series of prisoner numbers in Australia from 2005 to 2016, split by sex, state and legal status2.
fpp2::prisonLF
## # A tibble: 1,536 x 5
## state gender legal t count
## <fct> <fct> <fct> <date> <dbl>
## 1 ACT Female Remanded 2005-03-01 2
## 2 ACT Female Remanded 2005-06-01 4
## 3 ACT Female Remanded 2005-09-01 1
## 4 ACT Female Remanded 2005-12-01 4
## 5 ACT Female Remanded 2006-03-01 4
## 6 ACT Female Remanded 2006-06-01 6
## 7 ACT Female Remanded 2006-09-01 9
## 8 ACT Female Remanded 2006-12-01 6
## 9 ACT Female Remanded 2007-03-01 4
## 10 ACT Female Remanded 2007-06-01 4
## # ... with 1,526 more rows
autoplot(prison)
head(fpp2::prisonLF)
## # A tibble: 6 x 5
## state gender legal t count
## <fct> <fct> <fct> <date> <dbl>
## 1 ACT Female Remanded 2005-03-01 2
## 2 ACT Female Remanded 2005-06-01 4
## 3 ACT Female Remanded 2005-09-01 1
## 4 ACT Female Remanded 2005-12-01 4
## 5 ACT Female Remanded 2006-03-01 4
## 6 ACT Female Remanded 2006-06-01 6
prison <- fpp2::prisonLF %>%
mutate(qtr = yearquarter(t)) %>%
select(-t) %>%
as_tsibble(index = qtr, key = id(state, gender,legal))
prison
## # A tsibble: 1,536 x 5 [1QUARTER]
## # Key: state, gender, legal [32]
## state gender legal count qtr
## <fct> <fct> <fct> <dbl> <qtr>
## 1 ACT Female Remanded 2 2005 Q1
## 2 ACT Female Remanded 4 2005 Q2
## 3 ACT Female Remanded 1 2005 Q3
## 4 ACT Female Remanded 4 2005 Q4
## 5 ACT Female Remanded 4 2006 Q1
## 6 ACT Female Remanded 6 2006 Q2
## 7 ACT Female Remanded 9 2006 Q3
## 8 ACT Female Remanded 6 2006 Q4
## 9 ACT Female Remanded 4 2007 Q1
## 10 ACT Female Remanded 4 2007 Q2
## # ... with 1,526 more rows
prison %>% ETS(formula = count) # cria um modelo para cada uma das possíveis combinações (e.g. estado ACT, mulher e reencarcerada)
## # A mable: 32 models [1QUARTER]
## # Key: state, gender, legal [32]
## state gender legal data model
## <fct> <fct> <fct> <list> <model>
## 1 ACT Female Remanded <tsibble [48 x 2]> ETS(M,A,N)
## 2 ACT Female Sentenced <tsibble [48 x 2]> ETS(A,A,N)
## 3 ACT Male Remanded <tsibble [48 x 2]> ETS(M,N,N)
## 4 ACT Male Sentenced <tsibble [48 x 2]> ETS(A,N,N)
## 5 NSW Female Remanded <tsibble [48 x 2]> ETS(M,N,M)
## 6 NSW Female Sentenced <tsibble [48 x 2]> ETS(M,N,M)
## 7 NSW Male Remanded <tsibble [48 x 2]> ETS(M,A,A)
## 8 NSW Male Sentenced <tsibble [48 x 2]> ETS(M,A,A)
## 9 NT Female Remanded <tsibble [48 x 2]> ETS(M,N,N)
## 10 NT Female Sentenced <tsibble [48 x 2]> ETS(M,A,A)
## # ... with 22 more rows
prison %>% ETS(count) %>% forecast()
## # A fable: 32 forecasts [1QUARTER]
## # Key: state, gender, legal [32]
## state gender legal data model forecast
## <fct> <fct> <fct> <list> <model> <fc>
## 1 ACT Female Remanded <tsibble [48 x 2]> ETS(M,A,N) ~N [h=8]
## 2 ACT Female Sentenced <tsibble [48 x 2]> ETS(A,A,N) ~N [h=8]
## 3 ACT Male Remanded <tsibble [48 x 2]> ETS(M,N,N) ~N [h=8]
## 4 ACT Male Sentenced <tsibble [48 x 2]> ETS(A,N,N) ~N [h=8]
## 5 NSW Female Remanded <tsibble [48 x 2]> ETS(M,N,M) ~N [h=8]
## 6 NSW Female Sentenced <tsibble [48 x 2]> ETS(M,N,M) ~N [h=8]
## 7 NSW Male Remanded <tsibble [48 x 2]> ETS(M,A,A) ~N [h=8]
## 8 NSW Male Sentenced <tsibble [48 x 2]> ETS(M,A,A) ~N [h=8]
## 9 NT Female Remanded <tsibble [48 x 2]> ETS(M,N,N) ~N [h=8]
## 10 NT Female Sentenced <tsibble [48 x 2]> ETS(M,A,A) ~N [h=8]
## # ... with 22 more rows
prison <- fpp2::prisonLF %>%
mutate(qtr = yearquarter(t)) %>%
select(-t) %>%
as_tsibble(index = qtr, key = id(state, gender,legal))
prison
## # A tsibble: 1,536 x 5 [1QUARTER]
## # Key: state, gender, legal [32]
## state gender legal count qtr
## <fct> <fct> <fct> <dbl> <qtr>
## 1 ACT Female Remanded 2 2005 Q1
## 2 ACT Female Remanded 4 2005 Q2
## 3 ACT Female Remanded 1 2005 Q3
## 4 ACT Female Remanded 4 2005 Q4
## 5 ACT Female Remanded 4 2006 Q1
## 6 ACT Female Remanded 6 2006 Q2
## 7 ACT Female Remanded 9 2006 Q3
## 8 ACT Female Remanded 6 2006 Q4
## 9 ACT Female Remanded 4 2007 Q1
## 10 ACT Female Remanded 4 2007 Q2
## # ... with 1,526 more rows
if(!require(tsibbledata)) install_github("tidyverts/tsibbledata", force = TRUE) # antes de instala-lom instale o pacote tidyverse
## Loading required package: tsibbledata
##
## Attaching package: 'tsibbledata'
## The following object is masked from 'package:fpp2':
##
## elecdemand
library(tsibbledata)
tsibbledata::elecdemand
## # A tsibble: 17,520 x 4 [30MINUTE]
## index Demand WorkDay Temperature
## <dttm> <dbl> <dbl> <dbl>
## 1 2014-01-01 00:00:00 3.91 0 18.2
## 2 2014-01-01 00:30:00 3.67 0 17.9
## 3 2014-01-01 01:00:00 3.50 0 17.6
## 4 2014-01-01 01:30:00 3.34 0 16.8
## 5 2014-01-01 02:00:00 3.20 0 16.3
## 6 2014-01-01 02:30:00 3.10 0 16.6
## 7 2014-01-01 03:00:00 3.04 0 16.6
## 8 2014-01-01 03:30:00 3.01 0 16.7
## 9 2014-01-01 04:00:00 3.02 0 16.2
## 10 2014-01-01 04:30:00 3.03 0 16.6
## # ... with 17,510 more rows
fit2 <- ARIMA(tsibbledata::elecdemand, formula = Demand ~ Temperature + I(Temperature^2) + WorkDay)
summary(fit2)
## Series: Demand
## Regression with ARIMA(1,1,0)(2,0,2)[2] errors
##
## Coefficients:
## ar1 sar1 sar2 sma1 sma2 Temperature
## 0.8527 -0.1809 0.5231 -0.0657 -0.7922 -0.0086
## s.e. 0.0048 0.0153 0.0125 0.0120 0.0112 0.0025
## I(Temperature^2) WorkDay
## 2e-04 0.0163
## s.e. 1e-04 0.0055
##
## sigma^2 estimated as 0.008459: log likelihood=16949.3
## AIC=-33880.61 AICc=-33880.6 BIC=-33810.67
##
## Training set error measures:
## ME RMSE MAE MPE MAPE
## Training set 6.509344e-06 0.09195127 0.06338631 0.006327664 1.390059
## MASE ACF1
## Training set 0.2918306 0.1027364