根据我的经验,将您的财务数据保留为 xts 对象通常更有意义,以便将来使用其他技术指标等进行操作,除非您计划在 caret 中运行预测模型,在这种情况下转换data.frame 可能有意义。
考虑将数据符号保留为容器的元素,例如
update_sym_md <- function(sym, env = .GlobalEnv) {
x <- get(sym, env)
pd <- setNames((Hi(x) - Op(x)) / Op(x), "PD")
merge(x, pd)
}
# Adjust env for location of xts symbol data
l.syms <- lapply(Nasdaq100_Symbols, update_sym_md, env = .GlobalEnv)
lapply(l.syms, head)
# [[1]]
# AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted PD
# 2007-01-03 86.29 86.58 81.90 83.80 309579900 10.85709 0.003360760
# 2007-01-04 84.05 85.95 83.82 85.66 211815100 11.09807 0.022605556
# 2007-01-05 85.77 86.20 84.40 85.05 208685400 11.01904 0.005013373
# 2007-01-08 85.96 86.53 85.28 85.47 199276700 11.07345 0.006630991
# 2007-01-09 86.45 92.98 85.15 92.57 837324600 11.99333 0.075534942
# 2007-01-10 94.75 97.80 93.45 97.00 738220000 12.56728 0.032190006
#
# [[2]]
# AAL.Open AAL.High AAL.Low AAL.Close AAL.Volume AAL.Adjusted PD
# 2007-01-03 53.89 56.92 53.89 56.30 2955600 54.80361 0.0562256273
# 2007-01-04 56.30 59.15 53.65 58.84 2614500 57.27610 0.0506217238
# 2007-01-05 58.83 59.15 57.90 58.29 1656300 56.74072 0.0054394015
# 2007-01-08 57.30 60.48 57.04 57.93 2163200 56.39028 0.0554974006
# 2007-01-09 59.44 60.20 57.56 57.90 2098600 56.36108 0.0127860366
# 2007-01-10 60.03 60.04 57.34 58.93 3892200 57.36371 0.0001666167
此外,如果您确实想在一个 xts 对象中而不是在 data.frame 中比较符号之间的价格回报/原始价格,您可能会发现 qmao 包很有用。
例如:
install.packages("qmao", repos="http://R-Forge.R-project.org", type = "source")
library(qmao)
pf <- makePriceFrame(Nasdaq100_Symbols)
head(pf, 3)
# AAPL AAL
# 2007-01-03 10.85709 54.80361
# 2007-01-04 11.09807 57.27610
# 2007-01-05 11.01904 56.74072
rf <- makeReturnFrame(Nasdaq100_Symbols)
head(rf)
# AAPL AAL
# 2007-01-03 NA NA
# 2007-01-04 0.021952895 0.0441273684
# 2007-01-05 -0.007146715 -0.0093913155
# 2007-01-08 0.004926208 -0.0061951917
# 2007-01-09 0.079799692 -0.0005179716
# 2007-01-10 0.046745798 0.0176329011
根据 OP 的评论进行更新:
要将所有数据合并为一行,请尝试以下操作:
(顺便说一句:如果您要在此 data.frame 上使用非线性预测模型,请确保首先考虑在每一行的证券之间扩展您的数据点。)
x.cbind <- do.call(cbind, l.syms)
head(x.cbind)
# AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted PD AAL.Open AAL.High AAL.Low AAL.Close AAL.Volume AAL.Adjusted PD.1
# 2007-01-03 86.29 86.58 81.90 83.80 309579900 10.85709 0.003360760 53.89 56.92 53.89 56.30 2955600 54.80361 0.0562256273
# 2007-01-04 84.05 85.95 83.82 85.66 211815100 11.09807 0.022605556 56.30 59.15 53.65 58.84 2614500 57.27610 0.0506217238
# 2007-01-05 85.77 86.20 84.40 85.05 208685400 11.01904 0.005013373 58.83 59.15 57.90 58.29 1656300 56.74072 0.0054394015
# 2007-01-08 85.96 86.53 85.28 85.47 199276700 11.07345 0.006630991 57.30 60.48 57.04 57.93 2163200 56.39028 0.0554974006
# 2007-01-09 86.45 92.98 85.15 92.57 837324600 11.99333 0.075534942 59.44 60.20 57.56 57.90 2098600 56.36108 0.0127860366
# 2007-01-10 94.75 97.80 93.45 97.00 738220000 12.56728 0.032190006 60.03 60.04 57.34 58.93 3892200 57.36371 0.0001666167
df.cbind <- data.frame("time" = index(x.cbind), coredata(x.cbind))
head(df.cbind)
# time AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted PD AAL.Open AAL.High AAL.Low AAL.Close AAL.Volume AAL.Adjusted PD.1
# 1 2007-01-03 86.29 86.58 81.90 83.80 309579900 10.85709 0.003360760 53.89 56.92 53.89 56.30 2955600 54.80361 0.0562256273
# 2 2007-01-04 84.05 85.95 83.82 85.66 211815100 11.09807 0.022605556 56.30 59.15 53.65 58.84 2614500 57.27610 0.0506217238
# 3 2007-01-05 85.77 86.20 84.40 85.05 208685400 11.01904 0.005013373 58.83 59.15 57.90 58.29 1656300 56.74072 0.0054394015
# 4 2007-01-08 85.96 86.53 85.28 85.47 199276700 11.07345 0.006630991 57.30 60.48 57.04 57.93 2163200 56.39028 0.0554974006
# 5 2007-01-09 86.45 92.98 85.15 92.57 837324600 11.99333 0.075534942 59.44 60.20 57.56 57.90 2098600 56.36108 0.0127860366
# 6 2007-01-10 94.75 97.80 93.45 97.00 738220000 12.56728 0.032190006 60.03 60.04 57.34 58.93 3892200 57.36371 0.0001666167
为了更好地理解 qmao 函数的工作原理,为什么不查看文档中的示例并从那里开始呢?
?makeReturnFrame查看源代码以真正了解发生了什么(通过学习良好的编码风格同时成为更好的 R 程序员)