0

I'm struggling to plot an average on time series and then add: median, Naive, ETS, STL, Arima each with different colours.

I've the following code:

dataWk=ts(data=myDataW$QTY, start = c(2014,1),end = c(2016,1), frequency =   52)

fc_Base1=meanf(dataWk,h=52)
plot(fc_Base1)

fc1_ETS=forecast(dataWk,h=52)
lines(fc1_ETS)

but recive the following error:

Error in xy.coords(x, y) : 'x' is a list, but does not have components 'x' and 'y'

Can you please advise what im doing wrong?

Plot:

Plot1

Jaap
  • 81,064
  • 34
  • 182
  • 193
tommi46
  • 3
  • 2
  • Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269) . This will make it much easier for others to help you. – Jaap Feb 24 '17 at 10:23
  • Go through this [How to Ask ](http://stackoverflow.com/help/how-to-ask) and take a tour before posting a query – Prasad Feb 24 '17 at 10:23

1 Answers1

0

The main problem was that fc1_ETS is a list and not a vector. You should follow the guidelines in the code bellow. Hope this helps!

library(rdatamarket)
library(Quandl)
library(forecast)
library(dplyr)
library(graphics)

# Reading the data

ausgdp <- as.ts(dmseries("http://data.is/1jDQwpr")[,1])

# You should always calculate the forecasts before ploting. This will save you the adjustments in x axis scales

orig.points  <- ausgdp
ets.points   <- forecast(ausgdp,h=5)
arima.points <- auto.arima(ausgdp) %>% forecast(h=5)

# Note that ets.poins and arima.points are lists

ets.points <- ets.points$mean

# for arima, you have to combine the forecasts with the original data

arima.points <- c(orig.points, arima.points$mean)

# Now we are ready to plot

dt <- cbind( ets.points, arima.points, orig.points)

matplot(y=dt, x = seq(1960, length.out = length(arima.points)), 
        col=c("salmon1", "cornflowerblue", "darkblue"), lwd=2, pch=20, type="o", xlab="time",
        ylab="Forecasts", lty = 1, cex = 1.25)
grid()

# adding a legend

legend <- paste("-", c("ETS", "ARIMA", "original"))
col=c("salmon1", "cornflowerblue", "darkblue") 
olegend <- legend
for (i in 1:length(olegend)) {
  xlegend <- legend
  xlegend <- paste0("'", legend, "'")
  xlegend[-i] <- paste0("phantom(`", xlegend[-i], "`)")
  xlegend <- paste0(xlegend, collapse = " * ")
  xlegend <- paste0("expression(", xlegend, ")")
  xlegend <- paste0("mtext(", xlegend, ",col = '", col[i], 
                    "',line = ", 0, ",side = ", 3, ",adj = ", 1, 
                    ",cex = ", 0.8, ")")
  eval(parse(text = xlegend))

}

Output of the code