## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4)

## -----------------------------------------------------------------------------
library(foresightr)
path <- system.file("extdata", "piaui_revenue.csv", package = "foresightr")
data <- read.csv(path, comment.char = "#")
# BRL million, monthly from March 2017
fpe <- ts(data$fpe / 1e6, start = c(2017, 3), frequency = 12)

## -----------------------------------------------------------------------------
bt <- backtest(fpe, origins = 36, horizon = 12)
bt$origins
bt$first_origin   # position of the first period forecast

## -----------------------------------------------------------------------------
acc <- bt$candidates[[bt$best]]$accuracy
acc[c(1, 3, 6, 12), ]

## -----------------------------------------------------------------------------
autoplot(bt, "accuracy")

## -----------------------------------------------------------------------------
bt$ranking[order(bt$ranking$score), c("name", "score")][1:5, ]

## ----fig.height = 4.5---------------------------------------------------------
autoplot(bt, "ranking")

## -----------------------------------------------------------------------------
bt$candidates[[bt$best]]$bands[c(1, 6, 12), ]
bt$forecast[c(1, 6, 12), ]

## -----------------------------------------------------------------------------
dim(bt$candidates[[bt$best]]$trajectories)

## -----------------------------------------------------------------------------
total_forecast(bt, 6)

## -----------------------------------------------------------------------------
colSums(bt$forecast[1:6, c("mean", "lower_80", "upper_80")])

## -----------------------------------------------------------------------------
autoplot(bt)

## -----------------------------------------------------------------------------
autoplot(bt, candidate = "seasonal_naive") +
  ggplot2::labs(caption = "Source: Siconfi/STN, RREO Anexo 03")

