1

I'm facing this problem and can't solve it : Here is the code :

library(quantmod)
library(TTR)
library(randomForest)


getSymbols('^STOXX50E', src='yahoo')
equity.index<-STOXX50E

myReturnsSign = function(x) sign(Delt(Cl(x),type="log"))[-1]
mySMA =function(x,n) SMA(Cl(x),n)[-1]
myEMA = function(x,n,ratio) EMA(Cl(x),n,ratio)[-1]



model1<-specifyModel(myReturnsSign(equity.index) ~ myEMA(equity.index,20,0.8) + mySMA    (equity.index,5))

Here is the error message :

Error in xts(model.frame(model@model.spec, data = env, na.action = NULL),  : 
NROW(x) must match length(order.by)

However :

> dim(myEMA(equity.index,20,0.8))
[1] 2632    1

> dim(mySMA(equity.index,5))
[1] 2632    1

> dim(myReturnsSign(equity.index))
[1] 2632    1
GSee
  • 48,880
  • 13
  • 125
  • 145
marino89
  • 899
  • 1
  • 10
  • 16

1 Answers1

2

FWIW, the specifyModel code hasn't been worked on in over 4 years, and I don't think that is because it is stable.

Without looking too closely, it looks like your problem can be solved by removing those [-1] subsets from your functions

myReturnsSign = function(x) sign(Delt(Cl(x),type="log"))
mySMA =function(x,n) SMA(Cl(x),n)
myEMA = function(x,n,ratio) EMA(Cl(x),n,ratio)
model1<-specifyModel(myReturnsSign(equity.index) ~ myEMA(equity.index,20,0.8) + mySMA(equity.index,5))
GSee
  • 48,880
  • 13
  • 125
  • 145
  • Infact, it works. I don't understand why the `[-1]`in my functions is problematic. I put it in order to get rid of the first value of the returns whcih is `Na`. – marino89 Aug 27 '12 at 13:18
  • I'm not really sure either. But, as I mentioned, I think this code was sort of abandoned. – GSee Aug 27 '12 at 13:26
  • Ok thanks for your answers GSee. I was using `specifyModel` with the intention of building a Random Forest. Do you advice me so to use directly the `randomForest`function? – marino89 Aug 27 '12 at 13:33
  • I don't see what one has to do with the other. If you want to use `randomForest`, don't bother with `specifyModel` – GSee Aug 27 '12 at 14:04