gpt4 book ai didi

r - 将每日股票数据转换为时间序列对象时的问题

转载 作者:行者123 更新时间:2023-12-05 07:31:21 24 4
gpt4 key购买 nike

我使用 quantmod 包下载了 MSFT 历史每日股票数据。我得到的是 xts/zoo 对象。我想将它转换为 ts 对象,以便我可以使用 forecast 包进行每日价格预测。

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 ,但实际上周末没有数据。遇到这种情况,我该怎么办? 我用谷歌搜索并发现 ts仅适用于均匀间隔的数据。但是为了使用预测包,我需要提供 ts对象,尽管看起来我在从 xts 转换后丢失了所有日期信息至 ts目的。 如果有人能就此澄清我,我真的很感激。正确的做法是什么?我真的很困惑。我想使用预测包制作预测模型。非常感谢。

最佳答案

您要做的是保留 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

关于r - 将每日股票数据转换为时间序列对象时的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51886160/

24 4 0
文章推荐: image - 为多个屏幕 react native 图像
文章推荐: apache-spark - spark-submit 依赖冲突
文章推荐: java - 带有 List 或 Object 的 Spring RequestBody。两者的名称相同 "data"