将每日库存数据转换为时间序列对象时的问题

时间:2018-08-16 22:32:54

标签: r time-series xts forecasting

我使用Quantmod软件包下载了MSFT的历史每日库存数据。我得到的是xts / zoo对象。我想将其转换为ts对象,以便可以使用Forecast Package进行每日价格预测。

library(quantmod)
library(forecast)
library(xts)
library(zoo)
start <- as.Date('2018-01-01')
end <- as.Date('2018-08-14')
getSymbols('MSFT', src='yahoo', from=start, to=end)

#msft is xts/zoo object
msft <- MSFT[, 'MSFT.Close']

#convert msft to ts object
msft.ts <-ts(as.numeric(msft), 
            start=c(2018, yday(start(msft))), 
            frequency = 365)
msft的

索引(xts对象)如下所示。它们是每周的数据,而周末丢失了。显然,股票只在工作日交易。

[1] "2018-01-02" "2018-01-03" "2018-01-04" "2018-01-05" "2018-01-08"
  [6] "2018-01-09" "2018-01-10" "2018-01-11" "2018-01-12" "2018-01-16"
 [11] "2018-01-17" "2018-01-18" "2018-01-19" "2018-01-22" "2018-01-23"

msft.ts(ts对象)的索引如下:

[1] 2018.003 2018.005 2018.008 2018.011 2018.014 2018.016 2018.019 2018.022
  [9] 2018.025 2018.027 2018.030 2018.033 2018.036 2018.038 2018.041 2018.044
 [17] 2018.047 2018.049 2018.052 2018.055 2018.058 2018.060 2018.063 2018.066

我对那些索引的含义感到困惑。是2018年之后的数字吗?这些似乎并不正确。我的猜测可能不是因为我将频率设置为365,但实际上周末没有数据。在这种情况下,我该怎么办?   我在Google上搜索,发现ts仅适用于间隔均匀的数据。但是,为了使用预测程序包,我需要提供ts对象,尽管看起来从xts转换为ts对象后我丢失了所有日期信息。   如果有人可以对此进行澄清,我真的很感激。正确的做法是什么?我真的很困惑我想使用预测包制作一个预测模型。非常感谢。

1 个答案:

答案 0 :(得分:0)

您要做的是保留MSFT时间序列中的日期并将其添加到其中。 您可以为此使用timetk软件包。或者,如果需要,还可以使用扫描程序包,直到可以使用整洁的预测程序包寓言。 timetk与tidyquant搭配使用效果很好。您可以使用tk_tbl将时间序列变成小标题。

library(quantmod)
library(forecast)

start <- as.Date('2018-01-01')
end <- as.Date('2018-08-14')
getSymbols('MSFT', src='yahoo', from=start, to=end)

# forecast    
my_aa <- auto.arima(Cl(MSFT))
my_forecast = forecast(my_aa, h = 10, level = 95)

library(timetk)
time_index <- tk_index(MSFT)
# future days need to be the same as used in the forecast, but because we don't want weekends we
# need to make sure we have enough records so 30 should cover it.
time_index_future <- tk_make_future_timeseries(time_index, n_future = 30, inspect_weekdays = T)

my_fc_future <- cbind(forecast = my_forecast$mean, forecast_low = my_forecast$lower, forecast_high = my_forecast$upper)
# select the needed number of records from the index
my_xts_future <- xts(my_fc_future , time_index_future[1:nrow(my_fc_future)])
my_xts_future

           forecast forecast_low forecast_high
2018-08-14 108.6679     105.8490      111.4868
2018-08-15 108.8136     105.3287      112.2985
2018-08-16 108.9593     104.9167      113.0019
2018-08-17 109.1050     104.5728      113.6372
2018-08-20 109.2507     104.2768      114.2246
2018-08-21 109.3964     104.0170      114.7758
2018-08-22 109.5421     103.7857      115.2985
2018-08-23 109.6878     103.5776      115.7980
2018-08-24 109.8335     103.3889      116.2781
2018-08-27 109.9792     103.2168      116.7417

# merge forecast data with stock data
MSFT2 <- merge(MSFT, my_xts_future)

tail(MSFT2, 12)

           MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted forecast forecast_low forecast_high
2018-08-10    109.42    109.69   108.38     109.00    18183700      108.5821       NA           NA            NA
2018-08-13    109.24    109.58   108.10     108.21    18472500      107.7952       NA           NA            NA
2018-08-14        NA        NA       NA         NA          NA            NA 108.6679     105.8490      111.4868
2018-08-15        NA        NA       NA         NA          NA            NA 108.8136     105.3287      112.2985
2018-08-16        NA        NA       NA         NA          NA            NA 108.9593     104.9167      113.0019
2018-08-17        NA        NA       NA         NA          NA            NA 109.1050     104.5728      113.6372
2018-08-20        NA        NA       NA         NA          NA            NA 109.2507     104.2768      114.2246
2018-08-21        NA        NA       NA         NA          NA            NA 109.3964     104.0170      114.7758
2018-08-22        NA        NA       NA         NA          NA            NA 109.5421     103.7857      115.2985
2018-08-23        NA        NA       NA         NA          NA            NA 109.6878     103.5776      115.7980
2018-08-24        NA        NA       NA         NA          NA            NA 109.8335     103.3889      116.2781
2018-08-27        NA        NA       NA         NA          NA            NA 109.9792     103.2168      116.7417