You are on page 1of 3

library(fpp2)

library(quantmod)
library(dygraphs)
library(forecastHybrid)
library(seasonal)
library(rugarch)

set.seed(1)
getSymbols(c("GGBR3.SA"), src = "yahoo", periodicity = "monthly", from = "2014-11-
01", to = "2019-10-02")
x = GGBR3.SA[, 4]
x.ts = ts(x, start = c(2014, 11), freq = 12)
periodicity(x.ts)
summary(x.ts)
print(x.ts)
dygraph(x.ts, main = "Fechamento das A��es ON da GERDAU", ylab = "R$") %>%
dyAxis("x", drawGrid = TRUE) %>% dyEvent("2015-1-01", "2015", labelLoc = "bottom")
%>% dyEvent("2016-1-01", "2016", labelLoc = "bottom") %>% dyEvent("2017-1-01",
"2017", labelLoc = "bottom") %>% dyEvent("2018-1-01", "2018", labelLoc = "bottom")
%>% dyEvent("2019-1-01", "2019", labelLoc = "bottom") %>% dyOptions(drawPoints =
TRUE, strokeWidth = 3, pointSize = 4)

Treino = window(x.ts, end = c(2018, 12))


Teste = window(x.ts, start = c(2019, 01))
h = length(Teste)

ETS = forecast(ets(Treino), h = h)[["mean"]]


dygraph(cbind(Treino, ETS), main = "Previs�o das A��es ON da GERDAU", ylab = "R$")
%>% dyAxis("x", drawGrid = TRUE) %>% dyEvent("2019-01-01", "Teste", labelLoc =
"bottom") %>% dyOptions(drawPoints = TRUE, strokeWidth = 3, pointSize = 4)

ARIMA = forecast(auto.arima(Treino), h = h)[["mean"]]


dygraph(cbind(Treino, ARIMA), main = "Previs�o das A��es ON da GERDAU", ylab =
"R$") %>% dyAxis("x", drawGrid = TRUE) %>% dyEvent("2019-01-01", "Teste", labelLoc
= "bottom") %>% dyOptions(drawPoints = TRUE, strokeWidth = 3, pointSize = 4)

STL = stlf(Treino, h = h)[["mean"]]


dygraph(cbind(Treino, STL), main = "Previs�o das A��es ON da GERDAU", ylab = "R$")
%>% dyAxis("x", drawGrid = TRUE) %>% dyEvent("2019-01-01", "Teste", labelLoc =
"bottom") %>% dyOptions(drawPoints = TRUE, strokeWidth = 3, pointSize = 4)

NNAR = forecast(nnetar(Treino), h = h)[["mean"]]


dygraph(cbind(Treino, NNAR), main = "Previs�o das A��es ON da GERDAU", ylab = "R$")
%>% dyAxis("x", drawGrid = TRUE) %>% dyEvent("2019-01-01", "Teste", labelLoc =
"bottom") %>% dyOptions(drawPoints = TRUE, strokeWidth = 3, pointSize = 4)

TBATS = forecast(tbats(Treino), h = h)[["mean"]]


dygraph(cbind(Treino, TBATS), main = "Previs�o das A��es ON da GERDAU", ylab =
"R$") %>% dyAxis("x", drawGrid = TRUE) %>% dyEvent("2019-01-01", "Teste", labelLoc
= "bottom") %>% dyOptions(drawPoints = TRUE, strokeWidth = 3, pointSize = 4)

THETA = thetaf(Treino, h = h)[["mean"]]


dygraph(cbind(Treino, THETA), main = "Previs�o das A��es ON da GERDAU", ylab =
"R$") %>% dyAxis("x", drawGrid = TRUE) %>% dyEvent("2019-01-01", "Teste", labelLoc
= "bottom") %>% dyOptions(drawPoints = TRUE, strokeWidth = 3, pointSize = 4)

COMB1 = (ETS + ARIMA + STL + NNAR + TBATS + THETA)/6


dygraph(cbind(Treino, COMB1), main = "Previs�o das A��es ON da GERDAU", ylab =
"R$") %>% dyAxis("x", drawGrid = TRUE) %>% dyEvent("2019-01-01", "Teste", labelLoc
= "bottom") %>% dyOptions(drawPoints = TRUE, strokeWidth = 3, pointSize = 4)

HIBRIDO = forecast(hybridModel(Treino), h = h)[["mean"]]


dygraph(cbind(Treino, HIBRIDO), main = "Previs�o das A��es ON da GERDAU", ylab =
"R$") %>% dyAxis("x", drawGrid = TRUE) %>% dyEvent("2019-01-01", "Teste", labelLoc
= "bottom") %>% dyOptions(drawPoints = TRUE, strokeWidth = 3, pointSize = 4)

dygraph(cbind(COMB1, HIBRIDO), main = "Previs�o das A��es ON da GERDAU", ylab =


"R$") %>% dyOptions(drawPoints = TRUE, strokeWidth = 3, pointSize = 4)

X13 = ts(series(seas(Treino, forecast.save = "forecasts"), c("forecast.forecasts"))


[1:h, 1], start = c(2019, 1), freq = 12)
dygraph(cbind(Treino, X13), main = "Previs�o das A��es ON da GERDAU", ylab = "R$")
%>% dyAxis("x", drawGrid = TRUE) %>% dyEvent("2019-01-01", "Teste", labelLoc =
"bottom") %>% dyOptions(drawPoints = TRUE, strokeWidth = 3, pointSize = 4)

print(autoarfima(Treino, ar.max = 3, ma.max = 3, criterion = "AIC", method =


"full"))
GARCH = ts(ugarchforecast(ugarchfit(ugarchspec(mean.model = list(armaOrder = c(3,
3))), Treino), n.ahead = h)@forecast[["seriesFor"]], start = c(2019, 1), freq = 12)
dygraph(cbind(Treino, GARCH), main = "Previs�o das A��es ON da GERDAU", ylab =
"R$") %>% dyAxis("x", drawGrid = TRUE) %>% dyEvent("2019-01-01", "Teste", labelLoc
= "bottom") %>% dyOptions(drawPoints = TRUE, strokeWidth = 3, pointSize = 4)

COMB2 = (STL + NNAR + TBATS + X13 + GARCH)/5


dygraph(cbind(Treino, COMB2), main = "Previs�o das A��es ON da GERDAU", ylab =
"R$") %>% dyAxis("x", drawGrid = TRUE) %>% dyEvent("2019-01-01", "Teste", labelLoc
= "bottom") %>% dyOptions(drawPoints = TRUE, strokeWidth = 3, pointSize = 4)

dygraph(cbind(Teste, STL, NNAR, TBATS, COMB1, HIBRIDO, X13, GARCH, COMB2), main =
"Previs�o das A��es ON da GERDAU", ylab = "R$") %>% dyOptions(drawPoints = TRUE,
strokeWidth = 3) %>% dySeries("Teste", color = "#000000", strokeWidth = 4,
strokePattern = "dashed") %>% dySeries("STL", color = "#FFFFFF") %>%
dySeries("NNAR", color = "#FFFFFF") %>% dySeries("TBATS", color = "#FFFFFF") %>%
dySeries("HIBRIDO", color = "#FFFFFF") %>% dySeries("COMB1", color = "#FFFFFF") %>%
dySeries("X13", color = "#FFFFFF") %>% dySeries("GARCH", color = "#FFFFFF") %>%
dySeries("COMB2", color = "#FFFFFF") %>% dyLegend(width = 600)
dygraph(cbind(Teste, STL, NNAR, TBATS, COMB1, HIBRIDO, X13, GARCH, COMB2), main =
"Previs�o das A��es ON da GERDAU", ylab = "R$") %>% dyOptions(drawPoints = TRUE,
strokeWidth = 3) %>% dySeries("Teste", color = "#000000", strokeWidth = 4,
strokePattern = "dashed") %>% dySeries("STL", color = "#808080") %>%
dySeries("NNAR", color = "#FFFFFF") %>% dySeries("TBATS", color = "#FFFFFF") %>%
dySeries("HIBRIDO", color = "#FFFFFF") %>% dySeries("COMB1", color = "#FFFFFF") %>%
dySeries("X13", color = "#FFFFFF") %>% dySeries("GARCH", color = "#FFFFFF") %>%
dySeries("COMB2", color = "#FFFFFF") %>% dyLegend(width = 600)
dygraph(cbind(Teste, STL, NNAR, TBATS, COMB1, HIBRIDO, X13, GARCH, COMB2), main =
"Previs�o das A��es ON da GERDAU", ylab = "R$") %>% dyOptions(drawPoints = TRUE,
strokeWidth = 3) %>% dySeries("Teste", color = "#000000", strokeWidth = 4,
strokePattern = "dashed") %>% dySeries("STL", color = "#FFFFFF") %>%
dySeries("NNAR", color = "#0000FF") %>% dySeries("TBATS", color = "#FFFFFF") %>%
dySeries("HIBRIDO", color = "#FFFFFF") %>% dySeries("COMB1", color = "#FFFFFF") %>%
dySeries("X13", color = "#FFFFFF") %>% dySeries("GARCH", color = "#FFFFFF") %>%
dySeries("COMB2", color = "#FFFFFF") %>% dyLegend(width = 600)
dygraph(cbind(Teste, STL, NNAR, TBATS, COMB1, HIBRIDO, X13, GARCH, COMB2), main =
"Previs�o das A��es ON da GERDAU", ylab = "R$") %>% dyOptions(drawPoints = TRUE,
strokeWidth = 3) %>% dySeries("Teste", color = "#000000", strokeWidth = 4,
strokePattern = "dashed") %>% dySeries("STL", color = "#FFFFFF") %>%
dySeries("NNAR", color = "#FFFFFF") %>% dySeries("TBATS", color = "#008000") %>%
dySeries("HIBRIDO", color = "#FFFFFF") %>% dySeries("COMB1", color = "#FFFFFF") %>%
dySeries("X13", color = "#FFFFFF") %>% dySeries("GARCH", color = "#FFFFFF") %>%
dySeries("COMB2", color = "#FFFFFF") %>% dyLegend(width = 600)
dygraph(cbind(Teste, STL, NNAR, TBATS, COMB1, HIBRIDO, X13, GARCH, COMB2), main =
"Previs�o das A��es ON da GERDAU", ylab = "R$") %>% dyOptions(drawPoints = TRUE,
strokeWidth = 3) %>% dySeries("Teste", color = "#000000", strokeWidth = 4,
strokePattern = "dashed") %>% dySeries("STL", color = "#FFFFFF") %>%
dySeries("NNAR", color = "#FFFFFF") %>% dySeries("TBATS", color = "#FFFFFF") %>%
dySeries("HIBRIDO", color = "#8B4513") %>% dySeries("COMB1", color = "#FFFFFF") %>%
dySeries("X13", color = "#FFFFFF") %>% dySeries("GARCH", color = "#FFFFFF") %>%
dySeries("COMB2", color = "#FFFFFF") %>% dyLegend(width = 600)
dygraph(cbind(Teste, STL, NNAR, TBATS, COMB1, HIBRIDO, X13, GARCH, COMB2), main =
"Previs�o das A��es ON da GERDAU", ylab = "R$") %>% dyOptions(drawPoints = TRUE,
strokeWidth = 3) %>% dySeries("Teste", color = "#000000", strokeWidth = 4,
strokePattern = "dashed") %>% dySeries("STL", color = "#FFFFFF") %>%
dySeries("NNAR", color = "#FFFFFF") %>% dySeries("TBATS", color = "#FFFFFF") %>%
dySeries("HIBRIDO", color = "#FFFFFF") %>% dySeries("COMB1", color = "#9932CC") %>%
dySeries("X13", color = "#FFFFFF") %>% dySeries("GARCH", color = "#FFFFFF") %>%
dySeries("COMB2", color = "#FFFFFF") %>% dyLegend(width = 600)
dygraph(cbind(Teste, STL, NNAR, TBATS, COMB1, HIBRIDO, X13, GARCH, COMB2), main =
"Previs�o das A��es ON da GERDAU", ylab = "R$") %>% dyOptions(drawPoints = TRUE,
strokeWidth = 3) %>% dySeries("Teste", color = "#000000", strokeWidth = 4,
strokePattern = "dashed") %>% dySeries("STL", color = "#FFFFFF") %>%
dySeries("NNAR", color = "#FFFFFF") %>% dySeries("TBATS", color = "#FFFFFF") %>%
dySeries("HIBRIDO", color = "#FFFFFF") %>% dySeries("COMB1", color = "#FFFFFF") %>%
dySeries("X13", color = "#FF0000") %>% dySeries("GARCH", color = "#FFFFFF") %>%
dySeries("COMB2", color = "#FFFFFF") %>% dyLegend(width = 600)
dygraph(cbind(Teste, STL, NNAR, TBATS, COMB1, HIBRIDO, X13, GARCH, COMB2), main =
"Previs�o das A��es ON da GERDAU", ylab = "R$") %>% dyOptions(drawPoints = TRUE,
strokeWidth = 3) %>% dySeries("Teste", color = "#000000", strokeWidth = 4,
strokePattern = "dashed") %>% dySeries("STL", color = "#FFFFFF") %>%
dySeries("NNAR", color = "#FFFFFF") %>% dySeries("TBATS", color = "#FFFFFF") %>%
dySeries("HIBRIDO", color = "#FFFFFF") %>% dySeries("COMB1", color = "#FFFFFF") %>%
dySeries("X13", color = "#FFFFFF") %>% dySeries("GARCH", color = "#FFA500") %>%
dySeries("COMB2", color = "#FFFFFF") %>% dyLegend(width = 600)
dygraph(cbind(Teste, STL, NNAR, TBATS, COMB1, HIBRIDO, X13, GARCH, COMB2), main =
"Previs�o das A��es ON da GERDAU", ylab = "R$") %>% dyOptions(drawPoints = TRUE,
strokeWidth = 3) %>% dySeries("Teste", color = "#000000", strokeWidth = 4,
strokePattern = "dashed") %>% dySeries("STL", color = "#FFFFFF") %>%
dySeries("NNAR", color = "#FFFFFF") %>% dySeries("TBATS", color = "#FFFFFF") %>%
dySeries("HIBRIDO", color = "#FFFFFF") %>% dySeries("COMB1", color = "#FFFFFF") %>%
dySeries("X13", color = "#FFFFFF") %>% dySeries("GARCH", color = "#FFFFFF") %>%
dySeries("COMB2", color = "#FF00FF") %>% dyLegend(width = 600)
dygraph(cbind(Teste, STL, NNAR, TBATS, COMB1, HIBRIDO, X13, GARCH, COMB2), main =
"Previs�o das A��es ON da GERDAU", ylab = "R$") %>% dyOptions(drawPoints = TRUE,
strokeWidth = 3) %>% dySeries("Teste", color = "#000000", strokeWidth = 4,
strokePattern = "dashed") %>% dySeries("STL", color = "#808080") %>%
dySeries("NNAR", color = "#0000FF") %>% dySeries("TBATS", color = "#008000") %>%
dySeries("HIBRIDO", color = "#8B4513") %>% dySeries("COMB1", color = "#9932CC") %>%
dySeries("X13", color = "#FF0000") %>% dySeries("GARCH", color = "#FFA500") %>%
dySeries("COMB2", color = "#FF00FF") %>% dyLegend(width = 600)

c(ETS = accuracy(ETS, Teste)[,"RMSE"], ARIMA = accuracy(ARIMA, Teste)[,"RMSE"], STL


= accuracy(STL, Teste)[,"RMSE"], NNAR = accuracy(NNAR, Teste)[,"RMSE"], TBATS =
accuracy(TBATS, Teste)[,"RMSE"], THETA = accuracy(THETA, Teste)[,"RMSE"], HIBRIDO =
accuracy(HIBRIDO, Teste)[,"RMSE"], COMB1 = accuracy(COMB1, Teste)[,"RMSE"], X13 =
accuracy(X13, Teste)[,"RMSE"], GARCH = accuracy(GARCH, Teste)[,"RMSE"], COMB2 =
accuracy(COMB2, Teste)[,"RMSE"])

You might also like