Can't see forecast on test set that I chose when using autoplot and autolayer

Hello,

I am trying to show all of my series which is 7 daily observations on a plot.
I formed a training set and a test set
My training set is composed of the 1st 5 observations and my test set of the last 2
I want to use autoplot and autolayer to show my series for the 7 observations and my forecasts
for the last two observations using different methods

# Libraries ### 

library(forecast)
library(ggplot2)

Data

n = c(717,17385, 15201,14954, 13241, 13165, 246)
ts_rep <- ts(n, frequency = 1, start = as.Date("2019-11-17"))
# Autoplot ## 
a_rep <- autoplot(ts_rep) +
  ggtitle("Manual Changes by Day") +
  xlab("Day") +
  ylab("Thousands")

a_rep

Splitting data into training set and test set
Using the subset function I want to show all the series using autoplot
And the forecasts for the last 2 observations
I am getting the following error :
Error in forecast2plotdf(object, PI = PI, showgap = showgap) :
Could not find forecast x axis

mean_method <- meanf(ts_rep[1:5],h=2)
naive_method <- rwf(ts_rep[1:5],h=2)
seasonal_naive_method <- snaive(ts_rep[1:5],h=2)
autoplot(ts_rep) +
  autolayer(mean_method, series="Mean", PI=FALSE) +
  autolayer(naive_method, series="Naïve", PI=FALSE) +
  autolayer(seasonal_naive_method, series="Seasonal naïve", PI=FALSE) +
  xlab("Day") + ylab("Thousands") +
  ggtitle("Forecasts for Daily Manual Changes") +
  guides(colour=guide_legend(title="Forecast"))

I appreciate any help

The first problem here is that ts() does not handle dates like this. So the date is being converted to a large number. Then when you produce forecasts, ts_rep[1:5] will drop the time index information, and so the forecasts will be for times 6 and 7, rather than at the times corresponding to the future of ts_rep. A better approach is to use window() or subset() to create your training data.

A smaller problem is that snaive() will be equivalent to naive() as the frequency is set to 1.

Here is how to do it using the same approach.

library(forecast)
library(ggplot2)

ts_rep <- ts(c(717, 17385, 15201, 14954, 13241, 13165, 246), frequency = 1)
mean_method <- meanf(subset(ts_rep, end = 5), h = 2)
naive_method <- rwf(subset(ts_rep, end = 5), h = 2)
autoplot(ts_rep) +
  autolayer(mean_method, series = "Mean", PI = FALSE) +
  autolayer(naive_method, series = "Naïve", PI = FALSE) +
  xlab("Day") + ylab("Thousands") +
  ggtitle("Forecasts for Daily Manual Changes") +
  guides(colour = guide_legend(title = "Forecast"))

Created on 2020-03-31 by the reprex package (v0.3.0)

However, I would recommend you use the fable rather than forecast package for this, as it is much easier for handling daily data. Here is the same analysis done using fable.

library(tidyverse)
library(tsibble)
library(fable)

tsibble_rep <- tsibble(
  day = seq(as.Date("2019-11-17"), l = 7, by = 1),
  n = c(717, 17385, 15201, 14954, 13241, 13165, 246),
  index = day
)
tsibble_rep %>%
  autoplot(n) +
  ggtitle("Manual Changes by Day") +
  xlab("Day") + ylab("Thousands")

fc <- tsibble_rep %>%
  filter(day <= as.Date("2019-11-21")) %>%
  model(
    mean = MEAN(n),
    naive = NAIVE(n)
  ) %>%
  forecast(h = "2 days")

fc %>%
  autoplot(tsibble_rep, level = NULL) +
  xlab("Day") + ylab("Thousands") +
  ggtitle("Forecasts for Daily Manual Changes") +
  guides(colour = guide_legend(title = "Forecast"))

Created on 2020-03-31 by the reprex package (v0.3.0)

1 Like

Both great solutions. Thanks a lot!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.