-1

嗨,当我使用 caret ensemble 包时,我一直遇到这个错误,即 createTimeslices 交叉验证方法不能用于 caretEnsemble 包。

有没有人建议如何克服这个

4

1 回答 1

2

我已经解决了这个问题,我给出了一个可重现的例子

library(quantmod)

startDate = as.Date("2010-01-01")

endDate = as.Date("2014-12-31") 

getSymbols("^GDAXI", src = "yahoo", from = startDate, to = endDate)

RSI3<-RSI(Op(GDAXI), n= 3) 
#Calculate a 3-period relative strength index (RSI) off the open price


EMA5<-EMA(Op(GDAXI),n=5) 
#Calculate a 5-period exponential moving average (EMA)
EMAcross<- Op(GDAXI)-EMA5 
#Let’s explore the difference between the open price and our 5-period EMA



MACD<-MACD(Op(GDAXI),fast = 12, slow = 26, signal = 9) 
#Calculate a MACD with standard parameters
MACDsignal<-MACD[,2] 
#Grab just the signal line to use as our indicator.


SMI<-SMI(Op(GDAXI),n=13,slow=25,fast=2,signal=9) 
#Stochastic Oscillator with standard parameters
SMI<-SMI[,1] 
#Grab just the oscillator to use as our indicator


PriceChange<- Cl(GDAXI) - Op(GDAXI) 
#Calculate the difference between the close price and open price
Class<-ifelse(PriceChange>0,"UP","DOWN") 
#Create a binary classification variable, the variable we are trying to   predict.


DataSet<-data.frame(RSI3,EMAcross,MACDsignal,SMI,Class) 

Create our data set
colnames(DataSet)<-c("RSI3","EMAcross","MACDsignal","Stochastic","Class") 
#Name the columns 
DataSet<-DataSet[-c(1:33),] 
#Get rid of the data where the indicators are being calculated

Alldata<-cbind(DataSet,CombDF[34:1279,2])

   colnames(Alldata)<-c("RSI3","EMAcross","MACDsignal","Stochastic","Class","ArabSpring")

TrainingSet<-Alldata[1:1000,] 

TestSet<-Alldata[1001:1246,]


time_slices <- createTimeSlices(1:nrow(TrainingSet),initialWindow =800,horizon =200, fixedWindow = TRUE)
str(time_slices)
myTimeControl <- trainControl(method = "cv", number = 2, repeats = 1, savePrediction = TRUE,classProbs = TRUE,returnResamp = "final",returnData = TRUE,index= time_slices$train, indexOut=time_slices$test )

model_list_big <- caretList(
  Class~., data=TrainingSet,
  trControl=myTimeControl,
  metric='Accuracy',
  methodList=c('rf', 'gbm','treebag', 'nodeHarvest'),
  tuneList=list(
    rf=caretModelSpec(method='rf',tunelength = 10, ntrees = 2000, importance = TRUE),
    gbm=caretModelSpec(method='gbm',tuneGrid= expand.grid(.interaction.depth = seq(1, 7, by = 2), .n.trees = seq(100,1000,   by = 50), .shrinkage = c(0.01, 0.1) ) ),
    tbag=caretModelSpec(method='treebag'),
    Nharvest=caretModelSpec(method='nodeHarvest',nodes = 100)
  )


)

    greedy_ensemble <- caretEnsemble(model_list_big)
    summary(greedy_ensemble)

    ens_preds <- predict(greedy_ensemble, newdata=TestSet)
于 2015-04-22T15:04:21.183 回答