简体   繁体   English

使用R的时间序列预测

[英]Time series prediction using R

I have the following R code 我有以下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()

The resulting graph is 结果图是 预测

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)? 有没有办法让预测看起来与之前的价值趋势相似(见图表)?

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. 当您将频率定义为24时,我假设您每个周期24小时(每天)工作,因此您的历史数据集中大约有2个周期。 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. 有限的可用自动算法,如auto.arima,通常默认类似于移动平均线。 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. 像Holt-Winters或其他指数平滑方法之类的东西可能有所帮助。 However, auto.arima is a pretty good bet as well (I would first try to see what I can do with this one). 然而,auto.arima也是一个不错的选择(我会首先尝试看看我能用这个做什么)。

Getting more data and going through the same routine will improve your chart. 获取更多数据并完成相同的例程将改善您的图表。 Personally, I prefer the use of forecast over predict ; 就个人而言,我更倾向于使用forecast不是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

四个时期的图表

auto.arima() returns the best ARIMA model according to either AIC, AICc or BIC value. auto.arima()根据AIC,AICc或BIC值返回最佳ARIMA模型。 Based on your 'value' dataset it has probably chosen an ARMA(1,0) or AR(1) model which as you can see tends to revert back to the mean very quickly. 根据您的“价值”数据集,它可能选择了ARMA(1,0)或AR(1)模型,正如您所看到的那样,它们很快就会恢复到平均值。 This will always happen with an AR(1) model in the long run and so it's not very useful if you want to predict more than a couple of steps ahead. 从长远来看,这种情况总会发生在AR(1)模型中,因此如果您想预测前面的几个步骤,它就不是很有用。

You could look at fitting a different type of model perhaps by analysing the acf and pacf of your value data. 您可以通过分析价值数据的acf和pacf来查看拟合不同类型的模型。 You would then need to check to see if your alternative model is a good fit for the data. 然后,您需要检查您的替代模型是否适合数据。

您可能需要考虑使用移动平均线来更准确地绘制预测。

我建议看LTP包在https://code.google.com/p/ltp/你也可以看看在web前端在https://github.com/matteoredaelli/predictoR

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

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