在xts - order.by中出现错误。

时间:2020-12-24 12:38:20

I am trying to (re)build a basic prediction model of the S&P 500 INDEX (data orignates from Yahoo finance)

我试图(重新)构建一个标准普尔500指数的基本预测模型(来自雅虎金融的数据来源)

I ran into some difficulties with the "ordering" of my data set.
During the build of data.model the following error occurs

我在数据集的“排序”过程中遇到了一些困难。模型出现以下错误。

Error in xts(new.x, x.index) : NROW(x) must match length(order.by)

xt(新错误。(x, x.index): NROW(x)必须匹配长度(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)))

2 个解决方案

#1


7  

traceback() reveals the error occurs in the Delt(Cl(GSPC),k=1:10) call:

traceback()显示错误发生在Delt(Cl(GSPC),k=1:10)调用:

> Delt(Cl(GSPC),k=1:10)
Error in xts(new.x, x.index) : NROW(x) must match length(order.by)

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...

Delt期望一个(mx1)对象,但是您正在传递一个(mx2)对象。这是因为GSPC有两个与Cl匹配的列(“Close”和“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.

Cl希望像getSymbols返回的对象一样,调整后的关闭列命名为“调整”。如果你需要使用get.hist。引用某种原因,只需在下载数据后重命名“AdjClose”列。

colnames(GSPC) <- c("Open", "High", "Low", "Close","Volume","Adjusted")
Delt(Cl(GSPC),k=1:10)  # works now

#2


2  

## Error in xts(x, order.by = order.by, frequency = frequency, ...
##     NROW(x) must match length(order.by)

I wasted hours running into this error. Regardless of whether or not I had the exact same problem, I'll show how I solved for this error message in case it saves you the pain I had.

我浪费了几个小时来处理这个错误。不管我是否有完全相同的问题,我将展示我是如何解决这个错误信息的,以防它能帮你减轻我的痛苦。

I imported an Excel or CSV file (tried both) through several importing functions, then tried to convert my data (as either a data.frame or .zoo object) into an xts object and kept getting errors, this one included.

我通过几个导入函数导入了一个Excel或CSV文件(同时尝试了这两个文件),然后试图将我的数据(作为一个数据集或.zoo对象)转换成一个xts对象,并不断地出错,包括这个。

I tried creating a vector of dates seperately to pass in as the order.by parameter. I tried making sure the date vector the rows of the data.frame were the same. Sometimes it worked and sometimes it didn't, for reasons I can't explain. Even when it did work, R had "coerced" all my numeric data into character data. (Causing me endless problems, later. Watch for coercion, I learned.)

我试着按顺序创建一个日期的矢量。通过参数。我试着确保数据的行向量是相同的。有时它奏效了,有时却没有,原因我无法解释。即使是在工作中,R也将我所有的数字数据“强制”到字符数据中。(后来给我带来了无穷无尽的问题。我了解到,要注意强制。

These errors kept happening until:

这些错误一直持续到:

For xts conversion I used the date column from the imported Excel sheet as the order.by parameter with an as.Date() modifier, AND I *dropped the date column during the conversion to xts.*

对于xts转换,我使用导入的Excel表中的日期列作为订单。通过参数使用as.Date()修饰符,在转换到xts期间,我*删除了日期列。

Here's the working code:

这里的工作代码:

xl_sheet <- read_excel("../path/to/my_excel_file.xlsx")
sheet_xts <- xts(xl_sheet[-1], order.by = as.Date(xl_sheet$date))

Note my date column was the first column, so the xl_sheet[-1] removed the first column.

注意,我的日期列是第一列,所以xl_sheet[-1]删除了第一列。

#1


7  

traceback() reveals the error occurs in the Delt(Cl(GSPC),k=1:10) call:

traceback()显示错误发生在Delt(Cl(GSPC),k=1:10)调用:

> Delt(Cl(GSPC),k=1:10)
Error in xts(new.x, x.index) : NROW(x) must match length(order.by)

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...

Delt期望一个(mx1)对象,但是您正在传递一个(mx2)对象。这是因为GSPC有两个与Cl匹配的列(“Close”和“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.

Cl希望像getSymbols返回的对象一样,调整后的关闭列命名为“调整”。如果你需要使用get.hist。引用某种原因,只需在下载数据后重命名“AdjClose”列。

colnames(GSPC) <- c("Open", "High", "Low", "Close","Volume","Adjusted")
Delt(Cl(GSPC),k=1:10)  # works now

#2


2  

## Error in xts(x, order.by = order.by, frequency = frequency, ...
##     NROW(x) must match length(order.by)

I wasted hours running into this error. Regardless of whether or not I had the exact same problem, I'll show how I solved for this error message in case it saves you the pain I had.

我浪费了几个小时来处理这个错误。不管我是否有完全相同的问题,我将展示我是如何解决这个错误信息的,以防它能帮你减轻我的痛苦。

I imported an Excel or CSV file (tried both) through several importing functions, then tried to convert my data (as either a data.frame or .zoo object) into an xts object and kept getting errors, this one included.

我通过几个导入函数导入了一个Excel或CSV文件(同时尝试了这两个文件),然后试图将我的数据(作为一个数据集或.zoo对象)转换成一个xts对象,并不断地出错,包括这个。

I tried creating a vector of dates seperately to pass in as the order.by parameter. I tried making sure the date vector the rows of the data.frame were the same. Sometimes it worked and sometimes it didn't, for reasons I can't explain. Even when it did work, R had "coerced" all my numeric data into character data. (Causing me endless problems, later. Watch for coercion, I learned.)

我试着按顺序创建一个日期的矢量。通过参数。我试着确保数据的行向量是相同的。有时它奏效了,有时却没有,原因我无法解释。即使是在工作中,R也将我所有的数字数据“强制”到字符数据中。(后来给我带来了无穷无尽的问题。我了解到,要注意强制。

These errors kept happening until:

这些错误一直持续到:

For xts conversion I used the date column from the imported Excel sheet as the order.by parameter with an as.Date() modifier, AND I *dropped the date column during the conversion to xts.*

对于xts转换,我使用导入的Excel表中的日期列作为订单。通过参数使用as.Date()修饰符,在转换到xts期间,我*删除了日期列。

Here's the working code:

这里的工作代码:

xl_sheet <- read_excel("../path/to/my_excel_file.xlsx")
sheet_xts <- xts(xl_sheet[-1], order.by = as.Date(xl_sheet$date))

Note my date column was the first column, so the xl_sheet[-1] removed the first column.

注意,我的日期列是第一列,所以xl_sheet[-1]删除了第一列。