Can't use autolayer in timeseries

Hello,

I am trying to do time-series forecasting (forecasting some volume by day).
My original data frame looks something like this:

# Libraries ### 
library(ggplot2)
library(zoo) ## Use package zoo to convert to time series 
library(forecast)


# Data ####
TimeModified = c('11/17/19','11/18/19','11/19/20')
n = c(717,17385, 15201)

ts_al1 <- data.frame(TimeModified, n) 
# I convert the data frame to a time series object using:

z <- read.zoo(ts_al1, format = '%m/%d/%y') 

# Generate an autoplot that works

a <- autoplot(z[,'n']) +
  ggtitle("Manual Changes by Day")+
  xlab('Day') +
  ylab('Thousands')

a
# Now I start forecasting
# Say I want to use Naive Bayes and an average method####
meanf(z[,'n'], 2)
naive(z[,'n'],2)     ## it works
# Problem is when I want to use the autolayer to show my forecasts graphically
autoplot(z[,'n']) +
  autolayer(meanf(z[,'n'], h=3),
            series="Mean", PI=FALSE) +
  autolayer(naive(z[,'n'], h=3),
            series="Naïve", PI=FALSE) +
  ggtitle("Title") +
  xlab("Day") + ylab("Volume") +
  guides(colour=guide_legend(title="Forecast"))

## autolayer isn't working. I get the following errors: 
## Error in forecast2plotdf(object, PI = PI, showgap = showgap) : 
 ## Could not find forecast x axis

I appreciate any help.

Error in forecast2plotdf(object, PI = PI, showgap = showgap) :
Could not find forecast x axis

Hard to say without TimeModified. Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers.

Hi Technocrat,

I apologize. I added the variable TimeModified. Does this work? I ran the code in R and the last part of it is giving me the error. Please let me know if you have any ideas on how to solve this.

Thank you.

1 Like

I think this is what you want

library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
library(ggplot2)
# not needed; can do directly with ts()
# library(zoo) ## Use package zoo to convert to time series

# not needed
# TimeModified = c('11/17/19','11/18/19','11/20/20')

n <- c(717, 17385, 15201)

# not needed
# ts_al1 <- data.frame(TimeModified, n)
# z <- read.zoo(ts_al1, format = '%m/%d/%y')

ts_all <- ts(n, frequency = 1, start = as.Date("2019-11-17"))
# Generate an autoplot that works

a <- autoplot(ts_all) +
  ggtitle("Manual Changes by Day") +
  xlab("Day") +
  ylab("Thousands")

a


# meanf(z, h = 2)

a +
  autolayer(meanf(ts_all, h = 3),
    series = "Mean", PI = FALSE
  ) +
  ggtitle("Title") +
  xlab("Day") + ylab("Volume") +
  guides(colour = guide_legend(title = "Forecast"))

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

1 Like

Thank you! I thought you have to convert the data frame to time series first using zoo. I like the ts() approach. Thank you! I am wondering how you would go about changing the x-axis labels. I see 18220, 18230,.. even though you defined that we are starting at a date '2019-11-17'

For computational simplicity, I guess, ts converts date object into sequential integers. Fortunately, forecast speaks ggplot2, which allows the axes to be scaled.

See this recent post and see if you can adapt it. If not, c'mon back

Hey, I was having the same issue however, could not find a way to edit it using autoplot() or ts(). Eventually, I ended up using scale_x_continuous() and provided breaks and labels arguments manually. Hope this helps!

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