是否有一种简单的方法可以将预测恢复为时间序列进行绘图? [英] Is there an easy way to revert a forecast back into a time series for plotting?
问题描述
我是R的新手,并且发现此站点非常有用,因此涵盖了我问题的下半部分(每个帖子一个问题).感谢您的提前帮助.
I am new to R and have found this site extremely helpful, so this covers the second half of my question (one issue per post). Thank you for your assistance ahead of time.
背景:我正在绘制历史数据,并叠加了多个预测以进行视觉准确性检查.当在观测"的x轴上显示时,此效果很好.但是,当在x轴上用日期绘制数据时,数据更容易理解,因此我使用ts()将其设置为时间序列,并按预期绘制了时间序列数据.但是,(A)由于它们不是时间序列,因此未按时标绘制预测数据; (B)我不确定如何将x轴强制加1年以允许显示预测.
Background: I was plotting historical data with multiple forecasts overlaid for visual accuracy checks. This worked great when displayed on an x axis of 'observations'. However, the data is more understandable when plotted with dates on the x axis, so I made it a time series using ts() and it plotted the time series data as expected. However, (A) it did not plot the forecast data on the time scale because they are not a time series; and (B) I was unsure how to force the x axis to plus 1 year to permit the forecast to display.
问题:(A)如何将原始时间戳恢复为预测数据?我知道我可以手动重新创建时间序列,但这在每次预测迭代中都需要.我已经考虑过使用Forecast()而不是dictate(),但是其他的预测迭代仍然存在一个不属于时间序列的问题. 是否有一种简单的方法可以将原始时间戳恢复为预测数据?
Question: (A) How do I restore the original time stamps to the forecast data? I know that I could manually recreate the time series, but this would be required in every iteration of the forecast. I have considered using forecast() instead of predict(), but the additional forecast iterations still have the same issue of not being a time series. Is there a simple way to restore the original time stamp to the forecast data?
require(forecast) [EDITED for clarity]
data <- rep(cos(1:52*(3.1416/26)),5)*100+1000
arima.ts <- ts(data,start=c(2009,1),frequency=52) #not plotted as time series
# Create the current fit on data and predict one year out
plot(arima.ts, type="l", xlab="weeks", ylab="counts",
main="Overlay forecasts & actuals",
sub="green=FIT(1-105,by 16) wks back & PREDICT(26) wks, blue=52 wks")
############## This plotted correctly as "Arima(data),..."
arima.fit <- auto.arima(tail(arima.ts,156))
arima.pred <- predict(arima.fit, n.ahead=52)
lines(arima.pred$pred, col="blue")
lines(arima.pred$pred+2*arima.pred$se, col="red")
lines(arima.pred$pred-2*arima.pred$se, col="red")
# Loop back and perform comparison plotting of forecast to actuals
for (j in seq(1,105,by=16)) {
result <- tryCatch({
############## This plotted correctly as "Arima(head(data,-j),..."
arima1.fit <- auto.arima(head(tail(arima.ts,-j),156))
arima1.pred <- predict(arima1.fit, n.ahead=52)
lines(arima1.pred$pred, col="green", lty=(numtests %% 6) + 1 )
}, error = function(e) {return(e$message)}) ## Trap errors
}
推荐答案
要解决的核心问题是如何将原始时间戳恢复为预测数据" .通过反复试验,我了解到的是配置,然后再通过应用以下步骤就不要丢失时间序列属性" :
The core question being addressed is "how to restore the original time stamps to the forecast data". What I have learned with trial and error is "configure, then never loose the time series attribute" by applying these steps:
1:创建时间序列.使用ts()命令并创建时间序列.
2:子集时间序列使用"window()"在"for()"循环中创建时间序列的子集.在数据上使用"start()"和"end()"以显示时间轴位置.
3:预测时间序列.使用按时间序列运行的"forecast()"或"predict()".
4:绘制时间序列:绘制时间序列时,时间轴将使用lines()命令正确对齐以获取其他数据. {绘图选项是用户偏好.}
1: Make a time series Use the ts() command and create a time series.
2: Subset a time series Use 'window()' to create a subset of the time series in 'for()' loop. Use 'start()' and 'end()' on the data to show the time axis positions.
3: Forecast a time series Use 'forecast()' or 'predict()' which operate on time series.
4: Plot a time series When you plot a time series, then the time axis will align correctly for additional data using the lines() command. {Plotting options are user preference.}
这将使预测在正确的时间轴位置上绘制在历史数据上.
This causes the forecasts to be plotted over the historical data in the correct time axis location.
require(forecast) ### [EDITED for clarity]
data <- rep(cos(1:52*(3.1416/26)),5)*100+1000
a.ts <- ts(data,start=c(2009,1),frequency=52)
## Predict from previous '3' years then one year out & generate the plot
a.win <- window(a.ts,start=c(end(a.ts)[1]-3,end(a.ts)[2]),frequency=52)
a.fit <- auto.arima(a.win)
a.pred <- forecast(a.fit, h=52)
plot(a.pred, type="l", xlab="weeks", ylab="counts",
main="Overlay forecasts & actuals",
sub="green=FIT(1-105,by 16) wks back & PREDICT(26) wks, blue=52 wks")
for (j in seq(1, 90, by=8)) { ## Loop to overlay early forecasts
result1 <- tryCatch({
b.end <- c(end(a.ts)[1],end(a.ts)[2]-j) ## Window the time series
b.start <- c(b.end[1]-3,b.end[2])
b.window <- window(a.ts, start=b.start, end=b.end, frequency=52)
b.fit <-auto.arima(b.window)
b.pred <- forecast(b.fit, h=26)
lines(b.pred$mean, col="green", lty="dashed" )
}, error = function(e) {return(e$message)} ) ## Skip Errors
}
这篇关于是否有一种简单的方法可以将预测恢复为时间序列进行绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!