R:xts 中的错误 - order.by [英] R: Error in xts - order.by
问题描述
我正在尝试(重新)构建标准普尔 500 指数的基本预测模型(数据来自雅虎财经)
I am trying to (re)build a basic prediction model of the S&P 500 INDEX (data orignates from Yahoo finance)
我在数据集的排序"方面遇到了一些困难.
在data.model的构建过程中出现以下错误
I ran into some difficulties with the "ordering" of my data set.
During the build of data.model the following error occurs
xts(new.x, x.index) 中的错误:NROW(x) 必须匹配 length(order.by)
经过一些研究,我意识到问题出在排序上,而且似乎缺少底层动物园包所需的排序.
After some research I realize that the problem is with the ordering, and it seems to lack ordering as is required for the underlying zoo package.
有没有优雅的方法来解决这个问题?!提前致谢
Is there an elegant way to solve this issue?! Thanks in advance
library(xts)
library(tseries)
library(quantmod)
GSPC <- as.xts(get.hist.quote("^GSPC",start="1970-01-02",
quote=c("Open", "High", "Low", "Close","Volume","AdjClose")))
head(GSPC)
T.ind <- function(quotes, tgt.margin = 0.025, n.days = 10) {
v <- apply(HLC(quotes), 1, mean)
r <- matrix(NA, ncol = n.days, nrow = NROW(quotes))
for (x in 1:n.days) r[, x] <- Next(Delt(v, k = x), x)
x <- apply(r, 1, function(x) sum(x[x > tgt.margin | x <
-tgt.margin]))
if (is.xts(quotes))
xts(x, time(quotes))
else x
}
myATR <- function(x) ATR(HLC(x))[, "atr"]
mySMI <- function(x) SMI(HLC(x))[, "SMI"]
myADX <- function(x) ADX(HLC(x))[, "ADX"]
myAroon <- function(x) aroon(x[, c("High", "Low")])$oscillator
myBB <- function(x) BBands(HLC(x))[, "pctB"]
myChaikinVol <- function(x) Delt(chaikinVolatility(x[, c("High", "Low")]))[, 1]
myCLV <- function(x) EMA(CLV(HLC(x)))[, 1]
myEMV <- function(x) EMV(x[, c("High", "Low")], x[, "Volume"])[, 2]
myMACD <- function(x) MACD(Cl(x))[, 2]
myMFI <- function(x) MFI(x[, c("High", "Low", "Close")], x[, "Volume"])
mySAR <- function(x) SAR(x[, c("High", "Close")])[, 1]
myVolat <- function(x) volatility(OHLC(x), calc = "garman")[, 1]
library(randomForest)
data.model <- specifyModel(T.ind(GSPC) ~ Delt(Cl(GSPC),k=1:10) +
myATR(GSPC) + mySMI(GSPC) + myADX(GSPC) + myAroon(GSPC) +
myBB(GSPC) + myChaikinVol(GSPC) + myCLV(GSPC) +
CMO(Cl(GSPC)) + EMA(Delt(Cl(GSPC))) + myEMV(GSPC) +
myVolat(GSPC) + myMACD(GSPC) + myMFI(GSPC) + RSI(Cl(GSPC)) +
mySAR(GSPC) + runMean(Cl(GSPC)) + runSD(Cl(GSPC)))
推荐答案
traceback()
显示错误发生在 Delt(Cl(GSPC),k=1:10)
调用:
traceback()
reveals the error occurs in the Delt(Cl(GSPC),k=1:10)
call:
> Delt(Cl(GSPC),k=1:10)
Error in xts(new.x, x.index) : NROW(x) must match length(order.by)
Delt
需要一个 (m x 1) 对象,但您传递的是一个 (m x 2) 对象.这是因为 GSPC
有两列与 Cl
匹配(Close"和AdjClose").这很可能也会引起其他领域的头痛......
Delt
expects a (m x 1) object but you're passing a (m x 2) object. This is because GSPC
has two columns that are matched by Cl
("Close" and "AdjClose"). This will probably cause headaches in other areas too...
Cl
期望像那些由 getSymbols
返回的对象,其中调整后的关闭列被命名为调整".如果您出于某种原因需要使用get.hist.quote
,只需在下载数据后重命名AdjClose"列即可.
Cl
expects objects like those returned by getSymbols
, where the adjusted close column is named "Adjusted". If you need to use get.hist.quote
for some reason, just rename the "AdjClose" column after you download the data.
colnames(GSPC) <- c("Open", "High", "Low", "Close","Volume","Adjusted")
Delt(Cl(GSPC),k=1:10) # works now
这篇关于R:xts 中的错误 - order.by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!