6

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

2 Answers 2

7

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.