简体   繁体   English

R:如何在同一时间序列上进行 plot 多个 ARIMA 预测

[英]R: How to plot multiple ARIMA forecasts on the same time-series

I would like to plot several forecasts on the same plot in different colours, however, the scale is off.我想用不同的颜色对同一个 plot 进行 plot 几个预测,但是,比例是关闭的。 I'm open to any other methods.我对任何其他方法持开放态度。

reproducible example:可重现的例子:

require(forecast)     

# MAKING DATA
data <- c(3.86000,  19.55810,  19.51091,  20.74048,  20.71333,  29.04191,  30.28864, 25.64300,  23.33368,  23.70870 , 26.16600  ,27.61286 , 27.88409 , 28.41400 , 24.81957 , 24.60952,  27.49857,  32.08000 , 29.98000, 27.49000 , 237.26150, 266.35478, 338.30000, 377.69476, 528.65905, 780.00000  )
a.ts <- ts(data,start=c(2005,1),frequency=12)

# FORECASTS
arima011_css =stats::arima(x = a.ts, order = c(0, 1, 1), method = "CSS") # css estimate
arima011_forecast = forecast(arima011_css, h=10, level=c(99.5))

arima321_css =stats::arima(x = a.ts, order = c(3, 2, 1), method = "CSS") # css estimate
arima321_forecast = forecast(arima321_css, h=10, level=c(99.5))

# MY ATTEMPT AT PLOTS
plot(arima321_forecast)
par(new=T)
plot(arima011_forecast)

在此处输入图像描述

Here is something similar to @jay.sf but using ggplot2.这是类似于@jay.sf 但使用 ggplot2 的内容。

library(ggplot2)

autoplot(a.ts) +
  autolayer(arima011_forecast, series = "ARIMA(0,1,1)", alpha = 0.5) +
  autolayer(arima321_forecast, series = "ARIMA(3,2,1)", alpha = 0.5) +
  guides(colour = guide_legend("Model"))

Created on 2020-05-19 by the reprex package (v0.3.0)reprex package (v0.3.0) 于 2020 年 5 月 19 日创建

You could do a manual plot using a seq uence of dates.您可以使用日期序列手动执行seq

rn <- format(seq.Date(as.Date("2005-01-01"), by="months", length.out=12*3), "%Y.%m")

Your ARIMAs you'll need as.matrix form.您需要as.matrix形式的 ARIMA。

arima321_mat <- as.matrix(as.data.frame(arima321_forecast))
arima011_mat <- as.matrix(as.data.frame(arima011_forecast))

Some colors with different alpha= .一些具有不同alpha=的 colors 。

col.1 <- rainbow(2, ,.7)
col.2 <- rainbow(2, ,.7, alpha=.2)

For the CIs use polygon .对于 CI,请使用polygon

plot(data, type="l", xlim=c(1, length(rn)), ylim=c(0, 3500), xaxt="n", main="Forecasts")
axis(1, axTicks(1), labels=F)
mtext(rn[(seq(rn)-1) %% 5 == 0], 1, 1, at=axTicks(1))
lines((length(data)+1):length(rn), arima321_mat[,1], col=col.1[1], lwd=2)
polygon(c(27:36, 36:27), c(arima321_mat[,2], rev(arima321_mat[,3])), col=col.2[1], 
        border=NA)

lines((length(data)+1):length(rn), arima011_mat[,1], col=col.1[2], lwd=3)
polygon(c(27:36, 36:27), c(arima011_mat[,2], rev(arima011_mat[,3])), col=col.2[2],
        border=NA)

legend("topleft", legend=c("ARIMA(3,2,1)", "ARIMA(0,1,1)"), col=col.1, lwd=2, cex=.9)

在此处输入图像描述

Edit: To avoid the repetition of lines and polygon calls, you may unite them using Map .编辑:为避免重复linespolygon调用,您可以使用Map将它们联合起来。

mats <- list(arima321_mat, arima011_mat)  ## put matrices into list

plot(.)
axis(.)
mtext(.)
Map(function(i) {
  lines((length(data)+1):length(rn), mats[[i]][,1], col=col.1[i], lwd=2)
  polygon(c(27:36, 36:27), c(mats[[i]][,2], rev(mats[[i]][,3])), col=col.2[i], border=NA)
}, 1:2)
legend(.)

require(forecast)     

data <- c(3.86000,  19.55810,  19.51091,  20.74048,  20.71333,  29.04191,  30.28864, 25.64300,  23.33368,  23.70870 , 26.16600  ,27.61286 , 27.88409 , 28.41400 , 24.81957 , 24.60952,  27.49857,  32.08000 , 29.98000, 27.49000 , 237.26150, 266.35478, 338.30000, 377.69476, 528.65905, 780.00000  )
a.ts <- ts(data,start=c(2005,1),frequency=12)



arima011_css =stats::arima(x = a.ts, order = c(0, 1, 1), method = "CSS") # css estimate
arima011_forecast = predict(arima011_css, n.ahead = 2)$pred


arima321_css =stats::arima(x = a.ts, order = c(3, 2, 1), method = "CSS") # css estimate
arima321_forecast = predict(arima321_css, n.ahead = 2)$pred


plot(a.ts, type = "o", xlim = c(2005, 2007.5) , ylim = c(-1, 1200) , ylab = "price" ,main = "2 month Forecast")



range = c(2007+(3/12), 2007+(4/12)) # adding the dates for the prediction
lines(y = arima011_forecast , x = range ,  type = "o", col = "red")

lines(y = arima321_forecast, x = range ,  type = "o", col = "blue")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM