Created
October 28, 2017 18:50
-
-
Save sillasgonzaga/c4e9749e5e6ebc8e8fa346aa8c0c2647 to your computer and use it in GitHub Desktop.
Exemplo de agrupar forecasts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(purrr) | |
library(forecast) | |
library(lubridate) | |
vetor_uf <- c("RJ", "SP", "MG", "ES") | |
vetor_data <- seq.Date(as.Date("2010-01-01"), by = "month", | |
length.out = 60) | |
df <- data.frame( | |
uf = rep(vetor_uf, each = 12 * 5), | |
valor = rnorm(12 * 5 * 4, mean = 100, sd = 5), | |
data = rep(vetor_data, 4) | |
) | |
ggplot(df, aes(x = data, y = valor)) + | |
geom_line() + | |
facet_wrap(~ uf) | |
extrair_previsao <- function(dframe){ | |
# data: dataframe separado por UF | |
# construir objeto ts | |
st_ts_uf <- decimal_date(min(dframe[["data"]])) | |
ts_uf <- ts(dframe$valor, start = st_ts_uf, frequency = 12) | |
# ajustar um modelo sazonal ingenuo | |
mod_saz <- snaive(ts_uf) | |
# retornar previsao obtida pelo modelo | |
mod_saz$mean | |
} | |
lista_uf <- df %>% | |
split(.$uf) | |
lista_uf %>% | |
map(extrair_previsao) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment