2017-02-24 4 views
0

時系列の平均をプロットしてから、メディアン、ナイーブ、ETS、STL 、Arimaの色はそれぞれ異なります。時系列の平均をプロットして、メジアン、ナイーブ、ETS、STL、Arimaをそれぞれ異なる色で追加する方法

私は、次のコードをしました:

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) 

をしかし、次のエラーをrecive:

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

あなたは間違って何イムアドバイスしてくださいことはできますか?

プロット:

Plot1

+0

はStackOverflowのへようこそ! [良い質問をする方法](http://stackoverflow.com/help/how-to-ask)と[再現可能な例を与える方法](http://stackoverflow.com/questions/)の情報をお読みください。 5963269)。これは他の人があなたを助けることをはるかに容易にします。 – Jaap

+0

質問を投稿する前にこの[How to Ask](http://stackoverflow.com/help/how-to-ask)を見て、ツアーを受けてください – Prasad

答えて

0

主な問題は、fc1_ETSがリストではなくベクトルであるということでした。以下のコードのガイドラインに従ってください。お役に立てれば!

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

+0

ありがとうございますEligijus!私はすぐにそれをテストするつもりです! – tommi46

+0

もしこれがうまくいくなら、あなたはこの答えを与えることができますか? :) –

関連する問題