R:xts 中的错误 - order.by

发布于 2024-11-18 20:35:27 字数 1892 浏览 0 评论 0原文

我正在尝试(重新)构建标准普尔 500 指数的基本预测模型(数据来自雅虎财经),

我在数据集的“排序”方面遇到了一些困难。
在构建 data.model 期间,出现以下错误

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

经过一番研究,我意识到问题在于排序,它似乎缺乏底层动物园包所需的排序。

有没有优雅的方法来解决这个问题?提前致谢

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

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

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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倚栏听风 2024-11-25 20:35:27

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 需要 (mx 1) 对象,但您传递的是 (mx 2) 对象。这是因为 GSPC 有两列与 Cl 匹配(“Close”和“AdjClose”)。这可能也会在其他领域引起头痛...

Cl 期望像 getSymbols 返回的对象一样的对象,其中调整后的关闭列名为“Adjusted”。如果您出于某种原因需要使用 get.hist.quote,只需在下载数据后重命名“AdjClose”列即可。

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

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 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 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
冷情妓 2024-11-25 20:35:27
## Error in xts(x, order.by = order.by, frequency = frequency, ...
##     NROW(x) must match length(order.by)

我浪费了几个小时遇到这个错误。无论我是否遇到完全相同的问题,我都会展示我如何解决此错误消息,以防它减轻您的痛苦。

我通过几个导入函数导入了 Excel 或 CSV 文件(都尝试过),然后尝试将我的数据(作为 data.frame 或 .zoo 对象)转换为 xts 对象,但不断收到错误,包括这个错误。

我尝试单独创建一个日期向量作为 order.by 参数传入。我尝试确保 data.frame 的行的日期向量相同。有时有效,有时无效,原因我无法解释。即使它确实有效,R 也将我所有的数字数据“强制”为字符数据。 (后来给我带来了无穷无尽的问题。我学会了注意强制转换。)

这些错误不断发生,直到:

对于 xts 转换,我使用导入的 Excel 工作表中的日期列作为 order.by 参数和 as.Date( ) 修饰符,并且我*在转换为 xts 期间删除了日期列。*

这是工作代码:

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

请注意,我的日期列是第一列,因此删除了 xl_sheet[-1]第一个 柱子。

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文