如何使用R预测每日股票的未来价格?

时间:2018-05-08 17:43:21

标签: r time-series forecasting

我试图用R中的auto.arima模型来预测未来的股票价格。我能够预测结果,但我无法得到它的日期。我只看到数字。这是我的代码

library(quantmod)

Stock_df<-as.data.frame(getSymbols(Symbols = "AAPL", src = "yahoo", from = "2017-05-08", env = NULL))

Stock_dates<- as.data.frame(as.POSIXlt(rownames(Stock_df)))
colnames(Stock_dates) <- "Dates"

Stock_df$Open = Stock_df[,1]
Stock_df$High = Stock_df[,2]
Stock_df$Low = Stock_df[,3]
Stock_df$Close = Stock_df[,4]
Stock_df$Volume = Stock_df[,5]
Stock_df$Adj = Stock_df[,6]
Stock_df <- Stock_df[,c(7,8,9,10,11,12)]

x <- as.data.frame(xts(Stock_df$Close,Stock_dates$Date))
fit <- auto.arima(x,ic="bic")
fit.forecast <- forecast(fit)

fit.forecast

我得到以下结果:

    Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
253         185.16 182.2090 188.1110 180.6468 189.6732
254         185.16 180.9866 189.3334 178.7773 191.5427
255         185.16 180.0487 190.2714 177.3429 192.9771
256         185.16 179.2579 191.0621 176.1336 194.1865
257         185.16 178.5613 191.7587 175.0681 195.2519
258         185.16 177.9315 192.3885 174.1049 196.2151
259         185.16 177.3523 192.9677 173.2191 197.1009
260         185.16 176.8132 193.5068 172.3947 197.9253
261         185.16 176.3069 194.0131 171.6203 198.6997
262         185.16 175.8280 194.4920 170.8879 199.4321

我想要显示以下结果

    Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
2018-05-08  185.16 182.2090 188.1110 180.6468 189.6732
2018-05-09  185.16 180.9866 189.3334 178.7773 191.5427
2018-05-10  185.16 180.0487 190.2714 177.3429 192.9771
2018-05-11  185.16 179.2579 191.0621 176.1336 194.1865
2018-05-14  185.16 178.5613 191.7587 175.0681 195.2519
2018-05-15  185.16 177.9315 192.3885 174.1049 196.2151
2018-05-16  185.16 177.3523 192.9677 173.2191 197.1009
2018-05-17  185.16 176.8132 193.5068 172.3947 197.9253
2018-05-18  185.16 176.3069 194.0131 171.6203 198.6997
2018-05-21  185.16 175.8280 194.4920 170.8879 199.4321

部分解决方案

以下是Ryan的部分解决方案

forecast.data<- as.data.frame(fit.forecast)
forecast.data$newcolname <- Sys.Date() + seq_len(nrow(forecast.data)) - 1

这是部分解决方案的原因是因为绘制fit.forecast对象提供了多个图形。我想要一个图表,其中时间在x轴上。enter image description here

0 个答案:

没有答案