Search code examples
rtime-seriesxtsquantmod

Error with specifyModel


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

Solution

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