Repeated troubles using autoplot() with time-series data and forecast objects

Greetings,

While practicing forecasting time-series, I have encountered problems with autoplot(). When trying to plot a forecast object or moving average against the original time series data, I consistently encounter errors. I use incredibly similar code to examples provided by textbooks and professors, but receive errors. Below is a quick example of some code:

"""
library(quantmod)
library(forecast)
library(tseries)
library(ggplot2)
library(ggfortify)
library(GGally)
library(timeDate)

getSymbols.yahoo('NOC', from = '2009-01-01', periodicity = 'monthly', auto.assign = FALSE)[,6] %>%
na.omit() %>% ts(start = 2009, frequency = 12) -> NOC_prices

autoplot(NOC_prices)
ma(NOC_prices, 5) %>% na.omit() %>% autoplot()

autoplot(NOC_prices, series = 'Data') +
autolayer(ma(NOC_ts, 5), series = '5-MA') +
xlab('Year') + ylab('NOC Share Price') +
ggtitle('Northrop Grumman Monthly Share Price Data') +
scale_colour_manual(values = c('Data'='grey', '5-MA'='red'),
breaks = c('Data', '5-MA'))
"""
Autoplot works as expected when plotting NOC_prices and the moving average, however when combining the two (using the exact syntax my textbook provides but with a different time series) I receive an error stating: "Error: Invalid input: date_trans works with objects of class Date only." This error repeatedly occurs regardless of the class of object (i.e., data.frame, xts, numeric) used in the code ('Error: Objects of type data.frame not supported by autoplot.').

If there is anybody with insight on how to fix this issue, I will forever be in their debt. Do not be afraid to tell me I am missing something incredibly simple. I am only learning and solving this problem would help immensely!

Thanks!

Referred here by Forecasting: Principles and Practice, by Rob J Hyndman and George Athanasopoulos

Unfortunately, the ggfortify package does not play nicely with other packages and in this case, it is over-writing autoplot() from the forecast package so your code does not work.

I recommend that you don't use ggfortify and just use the autoplot() functions in the forecast package.

You also load several other packages that you don't use.

The following code will do what you want.

library(quantmod)
library(forecast)
library(ggplot2)

getSymbols.yahoo('NOC', from = '2009-01-01', periodicity = 'monthly', auto.assign = FALSE)[,6] %>%
  na.omit() %>% ts(start = 2009, frequency = 12) -> NOC_prices

autoplot(NOC_prices)

ma(NOC_prices, 5) %>% na.omit() %>% autoplot()

autoplot(NOC_prices, series = 'Data') +
  autolayer(ma(NOC_prices, 5), series = '5-MA') +
  xlab('Year') + ylab('NOC Share Price') +
  ggtitle('Northrop Grumman Monthly Share Price Data') +
  scale_colour_manual(values = c('Data'='grey', '5-MA'='red'),
                      breaks = c('Data', '5-MA'))

Created on 2019-07-24 by the reprex package (v0.3.0)

2 Likes

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