Abstract
This is an undergrad student level instruction for class use.This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
Sugestão de citação: FIGUEIREDO, Adriano Marcos Rodrigues. Séries Temporais: previsão de ações com ETS. Campo Grande-MS,Brasil: RStudio/Rpubs, 2024. Disponível em http://rpubs.com/amrofi/stocks_ETS2.
library(quantmod)
library(ggplot2)
library(scales)
library(ggthemes)
library(gridExtra) # para vaios graficos juntos
library(dygraphs) # para os graficos dinamicos
library(fpp2) # para fazer o ETS
library(Metrics) # para fazer acurácia
env = new.env()
# pegando cotações mensais da petrobras e do ibovespa
getSymbols(c("PETR4.SA", "VALE3.SA"), src = "yahoo", env = env, periodicity = "monthly",
from = "2000-01-01", to = "2019-09-01")
[1] "PETR4.SA" "VALE3.SA"
vale = env$VALE3.SA[, 6]
petro = env$PETR4.SA[, 6]
g1 <- autoplot(vale) + geom_line(size = 0.8, colour = "darkblue") + scale_x_date(date_breaks = "12 months",
labels = date_format("%b/%Y")) + theme(axis.text.x = element_text(angle = 60,
vjust = 1, hjust = 1)) + labs(title = "VALE3.SA", caption = "Fonte: Dados do Yahoo Finance.") +
xlab("Mês/Ano") + ylab("R$")
g2 <- autoplot(petro) + geom_line(size = 0.8, colour = "darkblue") + scale_x_date(date_breaks = "12 months",
labels = date_format("%b/%Y")) + theme(axis.text.x = element_text(angle = 60,
vjust = 1, hjust = 1)) + labs(title = "PETR4.SA", caption = "Fonte: Dados do Yahoo Finance.") +
xlab("Mês/Ano") + ylab("R$")
# os dois graficos de cotacoes juntos
grid.arrange(g1, g2, nrow = 2)
# Checando a periodicidade dos dados 2000-01-01 até 2019-08-01
periodicity(petro)
Monthly periodicity from 2000-02-01 to 2019-08-01
periodicity(vale)
Monthly periodicity from 2000-02-01 to 2019-08-01
r_petro <- monthlyReturn(petro, type = "log")[-1, ]
r_vale <- monthlyReturn(vale, type = "log")[-1, ]
g3 <- autoplot(r_petro) + geom_line(size = 0.8, colour = "darkblue") + scale_x_date(date_breaks = "12 months",
labels = date_format("%b/%Y")) + theme(axis.text.x = element_text(angle = 60,
vjust = 1, hjust = 1)) + labs(title = "Retornos da PETR4.SA", caption = "Fonte: Dados do Yahoo Finance.") +
xlab("Mês/Ano") + ylab("")
g4 <- autoplot(r_vale) + geom_line(size = 0.8, colour = "darkblue") + scale_x_date(date_breaks = "12 months",
labels = date_format("%b/%Y")) + theme(axis.text.x = element_text(angle = 60,
vjust = 1, hjust = 1)) + labs(title = "Retornos da VALE3.SA", caption = "Fonte: Dados do Yahoo Finance.") +
xlab("Mês/Ano") + ylab("")
# os dois graficos de cotacoes juntos
grid.arrange(g3, g4, nrow = 2)
# transformando em ts
r_petro <- ts(r_petro, start = c(2000, 3), frequency = 12)
r_vale <- ts(r_vale, start = c(2000, 3), frequency = 12)
# treino até setembro 2015 (189 meses)
r_petro_treino <- window(r_petro, start = c(2000, 3), end = c(2015, 10))
r_vale_treino <- window(r_vale, start = c(2000, 3), end = c(2015, 10))
# teste (45 meses após outubro de 2019)
r_petro_teste <- window(r_petro, start = c(2015, 11))
r_vale_teste <- window(r_vale, start = c(2015, 11))
Vamos checar se separamos corretamente:
autoplot(r_petro) + labs(title = "Retornos da PETR4.SA", caption = "Fonte: Dados do Yahoo Finance.") +
autolayer(r_petro_treino, series = "Training") + autolayer(r_petro_teste, series = "Test")
autoplot(r_vale) + labs(title = "Retornos da VALE3.SA", caption = "Fonte: Dados do Yahoo Finance.") +
autolayer(r_vale_treino, series = "Training") + autolayer(r_vale_teste, series = "Test")
Farei a previsão pelo método Holt-Winters conforme Hyndman e Athanasopoulos (2018).
# Holt-Winters petro
petro.hw <- forecast::hw(r_petro_treino, h = 60, seasonal = "additive")
summary(petro.hw)
Forecast method: Holt-Winters' additive method
Model Information:
Holt-Winters' additive method
Call:
forecast::hw(y = r_petro_treino, h = 60, seasonal = "additive")
Smoothing parameters:
alpha = 6e-04
beta = 5e-04
gamma = 1e-04
Initial states:
l = 0.0314
b = 2e-04
s = 0.0171 -0.0083 0.0162 -0.0073 -0.0152 -0.0106
0.0347 -0.0134 -0.0227 0.0139 0.0177 -0.0221
sigma: 0.1085
AIC AICc BIC
168.5795 172.1585 223.6892
Error measures:
ME RMSE MAE MPE MAPE MASE
Training set -0.01007944 0.1038463 0.08193888 99.96614 132.6828 0.7078731
ACF1
Training set 0.07285993
Forecasts:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Oct 2015 -0.007775968 -0.1468785 0.13132660 -0.2205150 0.2049630
Nov 2015 -0.033032914 -0.1721356 0.10606973 -0.2457721 0.1797062
Dec 2015 -0.008338471 -0.1474413 0.13076434 -0.2210779 0.2044009
Jan 2016 -0.048215589 -0.1873187 0.09088752 -0.2609554 0.1645243
Feb 2016 -0.009212942 -0.1483165 0.12989061 -0.2219535 0.2035276
Mar 2016 -0.013759614 -0.1528638 0.12534458 -0.2265011 0.1989819
Apr 2016 -0.051037240 -0.1901423 0.08806782 -0.2637801 0.1617056
May 2016 -0.042546585 -0.1816528 0.09655959 -0.2552911 0.1701980
Jun 2016 0.004869241 -0.1342383 0.14397683 -0.2078775 0.2176159
Jul 2016 -0.041227503 -0.1803368 0.09788182 -0.2539769 0.1715218
Aug 2016 -0.046519365 -0.1856308 0.09259205 -0.2592719 0.1662332
Sep 2016 -0.039341979 -0.1784559 0.09977192 -0.2520983 0.1734144
Oct 2016 -0.016559135 -0.1556761 0.12255780 -0.2293201 0.1962019
Nov 2016 -0.041816081 -0.1809364 0.09730422 -0.2545822 0.1709501
Dec 2016 -0.017121638 -0.1562458 0.12200252 -0.2298937 0.1956504
Jan 2017 -0.056998755 -0.1961273 0.08212978 -0.2697775 0.1557800
Feb 2017 -0.017996109 -0.1571296 0.12113737 -0.2307824 0.1947902
Mar 2017 -0.022542781 -0.1616818 0.11659623 -0.2353375 0.1902520
Apr 2017 -0.059820407 -0.1989656 0.07932476 -0.2726246 0.1529838
May 2017 -0.051329752 -0.1904817 0.08782223 -0.2641443 0.1614848
[ reached 'max' / getOption("max.print") -- omitted 40 rows ]
autoplot(r_petro) + forecast::autolayer(petro.hw, series = "Holt-Winters adit", PI = FALSE) +
xlab("Ano") + ylab("Retorno") + ggtitle("Forecasts para PETR4") + guides(colour = guide_legend(title = "Forecast"))
# Holt-Winters vale
vale.hw <- forecast::hw(r_vale_treino, h = 60, seasonal = "additive")
summary(vale.hw)
Forecast method: Holt-Winters' additive method
Model Information:
Holt-Winters' additive method
Call:
forecast::hw(y = r_vale_treino, h = 60, seasonal = "additive")
Smoothing parameters:
alpha = 1e-04
beta = 1e-04
gamma = 1e-04
Initial states:
l = 0.0498
b = -4e-04
s = 0.0246 0.0028 0.0292 -0.0188 0.0057 0.0283
-0.0453 0.0268 -0.0234 0.0171 -0.016 -0.0311
sigma: 0.0916
AIC AICc BIC
104.3911 107.9700 159.5008
Error measures:
ME RMSE MAE MPE MAPE MASE ACF1
Training set -0.002086669 0.08762808 0.06753594 -Inf Inf 0.720988 -0.02451164
Forecasts:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Oct 2015 0.003356254 -0.1140219 0.12073440 -0.1761581 0.1828706
Nov 2015 -0.023438948 -0.1408171 0.09393920 -0.2029533 0.1560754
Dec 2015 -0.002002928 -0.1193811 0.11537523 -0.1815173 0.1775115
Jan 2016 -0.058097853 -0.1754760 0.05928032 -0.2376123 0.1214166
Feb 2016 -0.043399002 -0.1607772 0.07397919 -0.2229135 0.1361155
Mar 2016 -0.010795821 -0.1281740 0.10658240 -0.1903103 0.1687187
Apr 2016 -0.051649801 -0.1690281 0.06572846 -0.2311644 0.1278648
May 2016 -0.001820050 -0.1191984 0.11555827 -0.1813347 0.1776946
Jun 2016 -0.074279551 -0.1916579 0.04309883 -0.2537943 0.1052352
Jul 2016 -0.001166086 -0.1185446 0.11621238 -0.1806810 0.1783488
Aug 2016 -0.024127342 -0.1415059 0.09325122 -0.2036424 0.1553877
Sep 2016 -0.049077977 -0.1664567 0.06830071 -0.2285932 0.1304372
Oct 2016 -0.001493940 -0.1188728 0.11588491 -0.1810094 0.1780215
Nov 2016 -0.028289142 -0.1456682 0.08908987 -0.2078048 0.1512266
Dec 2016 -0.006853122 -0.1242323 0.11052608 -0.1863691 0.1726629
Jan 2017 -0.062948047 -0.1803275 0.05443136 -0.2424644 0.1165683
Feb 2017 -0.048249196 -0.1656288 0.06913045 -0.2277659 0.1312675
Mar 2017 -0.015646015 -0.1330259 0.10173390 -0.1951631 0.1638711
Apr 2017 -0.056499996 -0.1738802 0.06088022 -0.2360175 0.1230176
May 2017 -0.006670244 -0.1240508 0.11071030 -0.1861883 0.1728478
[ reached 'max' / getOption("max.print") -- omitted 40 rows ]
autoplot(r_vale) + forecast::autolayer(vale.hw, series = "Holt-Winters adit", PI = FALSE) +
xlab("Ano") + ylab("Retorno") + ggtitle("Forecasts para VALE3") + guides(colour = guide_legend(title = "Forecast"))
Metrics::smape(r_petro_teste, petro.hw$mean)
[1] 1.473701
Metrics::smape(r_vale_teste, vale.hw$mean)
[1] 1.539472
Metrics::mape(r_petro_teste, petro.hw$mean)
[1] 1.736309
Metrics::mape(r_vale_teste, vale.hw$mean)
[1] 1.227286
Farei a previsão pelo método ETS (Error Trend Seasonal) conforme Hyndman e Athanasopoulos (2018).
# ETS (error trend seasonal) de Hyndman - pacote fpp2
set.seed(123)
petro.ets <- ets(r_petro_treino, model = "ZZZ") # N=none, A=additive, M=multiplicative e Z=automatic
summary(petro.ets)
ETS(A,N,N)
Call:
ets(y = r_petro_treino, model = "ZZZ")
Smoothing parameters:
alpha = 0.0137
Initial states:
l = 0.0089
sigma: 0.1058
AIC AICc BIC
145.5134 145.6431 155.2386
Training set error measures:
ME RMSE MAE MPE MAPE MASE
Training set -0.006332792 0.1052107 0.08227167 82.30262 126.2395 0.7107481
ACF1
Training set 0.07332308
petro.ets.forecasts <- forecast.ets(petro.ets, h = 12)
summary(petro.ets.forecasts)
Forecast method: ETS(A,N,N)
Model Information:
ETS(A,N,N)
Call:
ets(y = r_petro_treino, model = "ZZZ")
Smoothing parameters:
alpha = 0.0137
Initial states:
l = 0.0089
sigma: 0.1058
AIC AICc BIC
145.5134 145.6431 155.2386
Error measures:
ME RMSE MAE MPE MAPE MASE
Training set -0.006332792 0.1052107 0.08227167 82.30262 126.2395 0.7107481
ACF1
Training set 0.07332308
Forecasts:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Oct 2015 -0.007514248 -0.1430663 0.1280378 -0.2148232 0.1997947
Nov 2015 -0.007514248 -0.1430790 0.1280505 -0.2148426 0.1998141
Dec 2015 -0.007514248 -0.1430916 0.1280631 -0.2148620 0.1998335
Jan 2016 -0.007514248 -0.1431043 0.1280758 -0.2148813 0.1998528
Feb 2016 -0.007514248 -0.1431170 0.1280885 -0.2149007 0.1998722
Mar 2016 -0.007514248 -0.1431296 0.1281011 -0.2149201 0.1998916
Apr 2016 -0.007514248 -0.1431423 0.1281138 -0.2149394 0.1999109
May 2016 -0.007514248 -0.1431550 0.1281265 -0.2149588 0.1999303
Jun 2016 -0.007514248 -0.1431676 0.1281391 -0.2149782 0.1999497
Jul 2016 -0.007514248 -0.1431803 0.1281518 -0.2149975 0.1999691
Aug 2016 -0.007514248 -0.1431929 0.1281645 -0.2150169 0.1999884
Sep 2016 -0.007514248 -0.1432056 0.1281771 -0.2150363 0.2000078
plot(petro.ets.forecasts, col = 2)
# ETS (error trend seasonal) de Hyndman - pacote fpp2
set.seed(123)
vale.ets <- ets(r_vale_treino, model = "ZZZ") # N=none, A=additive, M=multiplicative e Z=automatic
summary(vale.ets)
ETS(A,N,N)
Call:
ets(y = r_vale_treino, model = "ZZZ")
Smoothing parameters:
alpha = 0.0266
Initial states:
l = 0.0221
sigma: 0.0931
AIC AICc BIC
97.38815 97.51788 107.11339
Training set error measures:
ME RMSE MAE MPE MAPE MASE ACF1
Training set -0.007819222 0.09263336 0.07108109 -Inf Inf 0.7588347 -0.06733914
vale.ets.forecasts <- forecast.ets(vale.ets, h = 12)
summary(vale.ets.forecasts)
Forecast method: ETS(A,N,N)
Model Information:
ETS(A,N,N)
Call:
ets(y = r_vale_treino, model = "ZZZ")
Smoothing parameters:
alpha = 0.0266
Initial states:
l = 0.0221
sigma: 0.0931
AIC AICc BIC
97.38815 97.51788 107.11339
Error measures:
ME RMSE MAE MPE MAPE MASE ACF1
Training set -0.007819222 0.09263336 0.07108109 -Inf Inf 0.7588347 -0.06733914
Forecasts:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Oct 2015 -0.01722824 -0.1365758 0.1021193 -0.1997546 0.1652981
Nov 2015 -0.01722824 -0.1366181 0.1021617 -0.1998193 0.1653629
Dec 2015 -0.01722824 -0.1366605 0.1022040 -0.1998840 0.1654276
Jan 2016 -0.01722824 -0.1367027 0.1022463 -0.1999487 0.1654922
Feb 2016 -0.01722824 -0.1367450 0.1022885 -0.2000134 0.1655569
Mar 2016 -0.01722824 -0.1367873 0.1023308 -0.2000780 0.1656215
Apr 2016 -0.01722824 -0.1368295 0.1023730 -0.2001426 0.1656861
May 2016 -0.01722824 -0.1368718 0.1024153 -0.2002072 0.1657507
Jun 2016 -0.01722824 -0.1369140 0.1024575 -0.2002718 0.1658153
Jul 2016 -0.01722824 -0.1369562 0.1024997 -0.2003363 0.1658798
Aug 2016 -0.01722824 -0.1369984 0.1025419 -0.2004008 0.1659444
Sep 2016 -0.01722824 -0.1370405 0.1025841 -0.2004653 0.1660089
plot(vale.ets.forecasts, col = 2)
Metrics::smape(r_petro_teste, petro.ets.forecasts$mean)
[1] 1.941631
Metrics::smape(r_vale_teste, vale.ets.forecasts$mean)
[1] 1.782595
Metrics::mape(r_petro_teste, petro.ets.forecasts$mean)
[1] 1.027739
Metrics::mape(r_vale_teste, vale.ets.forecasts$mean)
[1] 1.023852
FIGUEIREDO, A.M.R. Tópicos de econometria: ações. UFMS:Campo Grande,2019. Disponível em: http://rpubs.com/amrofi/acoesBOV_VALE3.
HYNDMAN, Rob. (2018). fpp2: Data for “Forecasting: Principles and Practice” (2nd Edition). R package version 2.3. Disponível em: https://CRAN.R-project.org/package=fpp2.
HYNDMAN, Rob J.; ATHANASOPOULOS, George . Forecasting: principles and practice. Otexts, 2018. Second Edition. Disponível em: https://www.otexts.org/fpp2.