R:预测时间序列

时间:2018-04-20 15:48:29

标签: r time-series forecasting

我想对我已经拥有的数据的价值进行预测,并绘制实际与预测的关系,以了解它的效果,然后预测未来30个时期。截至目前,我对未来有所预测。

数据是2014年1月1日至2月28日,包含广告位收入

我想对2018年2月进行预测,即使我有这些数据,也要测试模型能力与实际相比。然后我想绘制三月的预测值,但要让它更漂亮。

#Load Data
datats <- read.csv("ProjectTS1.CSV")

datats$SlotTS <- ts(datats$slots, start=2014,frequency=365)

acf(datats$SlotTS, lag.max = 1, plot = FALSE)
acf(datats$SlotTS)

AR <- arima(datats$SlotTS, order = c(3, 1, 2)) #2,1,3
AR_fitted <- datats$SlotTS - residuals(AR)
points(AR_fitted, type = "l", col = 2, lty = 2)
ts.plot(AR_fitted)
predict_AR <- predict(AR)
predict(AR, n.ahead = 10)
AR_forecast <- predict(AR, n.ahead = 30)$pred

ts.plot(datats$SlotTS,xlim=c(2018,2018.2))
AR_se <- predict(AR, n.ahead = 30)$se
points(AR_forecast, type = "l", col = 2)
points(AR_forecast - 2*AR_se, type = "l", col = 2, lty = 2)
points(AR_forecast + 2*AR_se, type = "l", col = 2, lty = 2)

1 个答案:

答案 0 :(得分:0)

以下链接使用预测,tseries和ggplot2包提供了一个关于R中时间序列预测的优秀教程:https://www.datascience.com/blog/introduction-to-forecasting-with-arima-in-r-learn-data-science-tutorials