Skip to content

Instantly share code, notes, and snippets.

@gomesfellipe
Created May 2, 2019 21:58
Show Gist options
  • Save gomesfellipe/ea0e9b41d811a5e014aa4c7b57c283db to your computer and use it in GitHub Desktop.
Save gomesfellipe/ea0e9b41d811a5e014aa4c7b57c283db to your computer and use it in GitHub Desktop.
# Codigo inspirado neste post:
# https://www.business-science.io/code-tools/2017/10/24/demo_week_timetk.html
# Testando se o ajuste funciona para a serie AirPassenger
# Nao sei ate que ponto este metodo esta correto... Vamos estudar!
library(tsibble)
library(lubridate)
library(dplyr)
library(timetk) # Toolkit for working with time series in R
library(tidyquant) # Loads tidyverse, financial pkgs, used to get data
# Obtendo serie:
serie <- AirPassengers
# Serie no formato "tidy"
serie <-
serie %>%
as_tsibble() %>%
mutate(date = as.Date(index)) %>%
as_tibble() %>%
select(-index)
# Removendo ultimos 6 valores para teste:
actuals_tbl <- tail(serie, 6)
serie <- serie %>% .[1:(nrow(.)-6),]
# Plot Serie
serie %>%
ggplot(aes(date, value)) +
geom_line(col = palette_light()[1]) +
geom_point(col = palette_light()[1]) +
geom_ma(ma_fun = SMA, n = 12, size = 1) +
theme_tq() +
scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
labs(title = "Airpassenger")
# Obtendo variaveis de calendario:
serie <-
serie %>%
tk_augment_timeseries_signature()
# linear regression model used, but can use any model
fit_lm <- lm(value ~ ., data = select(serie, -one_of(c("date", "diff"))))
# Resultado do modelo:
summary(fit_lm)
# Retrieves the timestamp information
serie_idx <-
serie %>%
tk_index()
tail(serie_idx)
# Make future index
future_idx <-
serie_idx %>%
tk_make_future_timeseries(n_future = 6)
future_idx
new_data_tbl <-
future_idx %>%
tk_get_timeseries_signature()
new_data_tbl
# Make predictions
pred <- predict(fit_lm, newdata = select(new_data_tbl, -one_of(c("index", "diff"))))
predictions_tbl <- tibble(
date = future_idx,
value = pred
)
predictions_tbl
actuals_tbl
# Plot da previsao + valores reais:
serie %>%
ggplot(aes(x = date, y = value)) +
# Training data
geom_line(color = palette_light()[[1]]) +
geom_point(color = palette_light()[[1]]) +
# Predictions
geom_line(aes(y = value), color = palette_light()[[2]], data = predictions_tbl) +
geom_point(aes(y = value), color = palette_light()[[2]], data = predictions_tbl) +
# Actuals
geom_line(color = palette_light()[[1]], data = actuals_tbl) +
geom_point(color = palette_light()[[1]], data = actuals_tbl) +
# Aesthetics
theme_tq() +
labs(title = "AirPassenger: Time Series Machine Learning",
subtitle = "Using basic multivariate linear regression can yield accurate results")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment