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