使用R进行时间序列预测 [英] Time series prediction using R

查看:439
本文介绍了使用R进行时间序列预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下R代码

library(forecast)
value <- c(1.2, 1.7, 1.6, 1.2, 1.6, 1.3, 1.5, 1.9, 5.4, 4.2, 5.5, 6, 5.6, 
6.2, 6.8, 7.1, 7.1, 5.8, 0, 5.2, 4.6, 3.6, 3, 3.8, 3.1, 3.4, 
2, 3.1, 3.2, 1.6, 0.6, 3.3, 4.9, 6.5, 5.3, 3.5, 5.3, 7.2, 7.4, 
7.3, 7.2, 4, 6.1, 4.3, 4, 2.4, 0.4, 2.4)

sensor<-ts(value,frequency=24)
fit <- auto.arima(sensor)
LH.pred<-predict(fit,n.ahead=24)
plot(sensor,ylim=c(0,10),xlim=c(0,5),type="o", lwd="1")
lines(LH.pred$pred,col="red",type="o",lwd="1")
grid()

结果图是

但是我对这个预测不满意.有什么方法可以使预测看起来像之前的价值趋势一样(见图)?

But I am not satisfied with the prediction. Is there any way to make the prediction look similar to the value trends preceding it (see graph)?

推荐答案

当您将频率定义为24时,我假设您每个周期工作24天(每天),因此历史数据集中大约有2个周期.一般来说,这是启动时间序列预测的有限样本数据.我建议获取更多数据,然后可以再次进行预测模型.您拥有的数据越多,它越能捕获季节性,从而预测未来价值.由于可用的自动算法有限,例如auto.arima通常默认为类似于移动平均值的东西.您的数据集应该比移动平均数更好,因为周期中存在一些季节性.有许多预测算法可以帮助您更好地形成正向曲线.诸如Holt-Winters或其他指数平滑方法之类的方法可能会有所帮助.但是,auto.arima也是一个不错的选择(我首先会尝试看看我能用这个做些什么).

As you defined the frequency as 24, I assume that you are working with 24 hours (daily) per cycle and thus have approximately 2 cycles in your historical dataset. Generally speaking this is limited sample data to initiate a time series forecast. I would recommend to get a little more data and then you can do the forecasting model again. The more data you have the better it will capture the seasonality and thus forecast future values. With limited available automatic algorithms like auto.arima often default to something similar to moving averages. Your data set deserves something better than moving averages as there is some seasonality in the cycle. There are a number of forecasting algorithms that could help you to get the forward curve shaped better; things like Holt-Winters or other exponential smoothing methods might help. However, auto.arima is a pretty good bet as well (I would first try to see what I can do with this one).

获取更多数据并执行相同的例程将改善您的图表.就个人而言,我更喜欢使用forecast而不是predict;数据似乎比图表更好,因为它显示了您的置信区间.在代码中,我还通过复制两个时间段来扩展了数据集,因此我们得到了四个时间段.看到下面的结果:

Getting more data and going through the same routine will improve your chart. Personally, I prefer the use of forecast over predict; the data seems to come out a bit nicer as well as the chart as it shows your confidence intervals. In the code, I have also expanded the data set a bit by copying the two periods so we got four periods. See the result below:

library(forecast)
value <- c(1.2,1.7,1.6, 1.2, 1.6, 1.3, 1.5, 1.9, 5.4, 4.2, 5.5, 6.0, 5.6, 6.2, 6.8, 7.1, 7.1, 5.8, 0.0, 5.2, 4.6, 3.6, 3.0, 3.8, 3.1, 3.4, 2.0, 3.1, 3.2, 1.6, 0.6, 3.3, 4.9, 6.5, 5.3, 3.5, 5.3, 7.2, 7.4, 7.3, 7.2, 4.0, 6.1, 4.3, 4.0, 2.4, 0.4, 2.4, 1.2,1.7,1.6, 1.2, 1.6, 1.3, 1.5, 1.9, 5.4, 4.2, 5.5, 6.0, 5.6, 6.2, 6.8, 7.1, 7.1, 5.8, 0.0, 5.2, 4.6, 3.6, 3.0, 3.8, 3.1, 3.4, 2.0, 3.1, 3.2, 1.6, 0.6, 3.3, 4.9, 6.5, 5.3, 3.5, 5.3, 7.2, 7.4, 7.3, 7.2, 4.0, 6.1, 4.3, 4.0, 2.4, 0.4, 2.4)
sensor <- ts(value,frequency=24) # consider adding a start so you get nicer labelling on your chart. 
fit <- auto.arima(sensor)
fcast <- forecast(fit)
plot(fcast)
grid()
fcast
         Point Forecast     Lo 80    Hi 80      Lo 95    Hi 95
3.000000       2.867879 0.8348814 4.900877 -0.2413226 5.977081
3.041667       3.179447 0.7369338 5.621961 -0.5560547 6.914950
3.083333       3.386926 0.7833486 5.990503 -0.5949021 7.368754
3.125000       3.525089 0.8531946 6.196984 -0.5612211 7.611400
3.166667       3.617095 0.9154577 6.318732 -0.5147025 7.748892

这篇关于使用R进行时间序列预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆