I am trying to graph 4 different outputs:
(1) Mean Method (2) Naive Method (3) Drift Method (4) Actual Data
The point of this is to show what the individual projections are against the actual data. In this case, I have a time series object (rdts) of 1999-2021. For the forecasts, I used 1999-2015, with projections from 2016-2021 to compare to the real data.
This is how I have coded the graph:
autoplot(rdts) +
autolayer(meanf(rdts[1:4], 4),
series="Mean", PI=FALSE) +
autolayer(naive(rdts[1:4], 4),
series="Naive", PI=FALSE) +
autolayer(rwf(rdts[1:4], 4, drift=TRUE),
series="Drift", PI=FALSE) +
autolayer(rdts,
series="True Data", PI=FALSE) +
ggtitle("Forcasting Comparison") +
xlab("Year") +
ylab("Afghan Refugees (and Projections)") +
guides(colour=guide_legend(title="Forecast"))
The libraries I am using are:
forecast ggplot2 tidyverse
This is the error I am getting:
Error in forecast2plotdf(object, PI = PI, showgap = showgap) :
Could not find forecast x axis
I can't figure out what exactly I am doing wrong.
EDIT:
Suppose I have the following:
Year <- c("1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006")
Amount <- c(4, 5, 7, 24, 36, 37, 47, 56)
df <- data.frame(Year, Amount)
rdts <- ts(df$Amount, start=c(1999), end=c(2006), frequency=1)
This should (hopefully) allow one to run the code above.