0

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.

Noob3000
  • 53
  • 5
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Apr 08 '22 at 00:49
  • Thanks! I have edited this and tried to add some code to make this reproducible. – Noob3000 Apr 08 '22 at 01:01

1 Answers1

1

The problem is that using rdts[1:4] removes all time series attributes, so the forecasts do not have any information about the time. You can see this by looking at the resulting objects:

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)

rdts
#> Time Series:
#> Start = 1999 
#> End = 2006 
#> Frequency = 1 
#> [1]  4  5  7 24 36 37 47 56
rdts[1:4]
#> [1]  4  5  7 24

Created on 2022-04-08 by the reprex package (v2.0.1)

The solution is to retain the time series attributes using a function such as head(), window() or subset(). Here is an example using head():

library(forecast)
library(ggplot2)

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)
train <- head(rdts, 4)

autoplot(rdts) +
  autolayer(meanf(train, 4),
            series="Mean", PI=FALSE) +
  autolayer(naive(train, 4),
            series="Naive", PI=FALSE) +
  autolayer(rwf(train, 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"))
#> Warning: Ignoring unknown parameters: PI

Created on 2022-04-08 by the reprex package (v2.0.1)

Rob Hyndman
  • 30,301
  • 7
  • 73
  • 85