Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using your own model in train (caret package)?

Tags:

r

r-caret

I am trying to use train from Caret with a package which is not included, and I get an error that I don't manage to figure out, any idea ? I used the following link to get started

bmsMeth<-list(type="Regression",library="BMS",loop=NULL,prob=NULL) 
prm<-data.frame(parameter="mprior.size",class="numeric",label="mprior.size")
bmsMeth$parameters<-prm
bmsGrid<-function(x,y,len=NULL){
out<-expand.grid(mprior.size=seq(2,3,by=len))
out
}
bmsMeth$grid<-bmsGrid
bmsFit<-function(x,y,param, lev=NULL) {bms(cbind(y,x),burn=5000,iter=100000,nmodel=1000,mcmc="bd",g="UIP",mprior.size=param$mprior.size)}
bmsMeth$fit<-bmsFit
bmsPred<-function(modelFit,newdata,preProcess=NULL,submodels=NULL){predict(modelFit,newdata)}
bmsMeth$predict<-bmsPred

library(caret)
data.train<-data.frame(runif(100),runif(100),runif(100),runif(100),runif(100))#synthetic data for testing
bms(cbind(data.train[,1],data.train[,-1]),burn=5000,iter=100000,nmodel=1000,mcmc="bd",g="UIP",mprior.size=2)#function out of caret is working

preProcess=c('center','scale')
myTimeControl <- trainControl(method = "timeslice",initialWindow = 0.99*nrow(data.train), horizon = 1, fixedWindow = FALSE)
tune <- train(data.train[,-1],data.train[,1],preProcess=preProcess,method = bmsMeth,tuneLength=2,metric= "RMSE",trControl =myTimeControl,type="Regression")

Error I get :

Error in train.default(data.train[, -1], data.train[, 1], preProcess = preProcess, : Stopping In addition: Warning messages: 1: In eval(expr, envir, enclos) : model fit failed for Training1: mprior.size=2 Error in method$fit(x = x, y = y, wts = wts, param = tuneValue, lev = obsLevels, : unused arguments (wts = wts, last = last, classProbs = classProbs, type = "Regression")

2: In nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo, : There were missing values in resampled performance measures.

like image 328
Stéphanie C Avatar asked Oct 19 '22 00:10

Stéphanie C


1 Answers

Apparantly, I just had to put the arguments in the function even if I never use them :

bmsFit<-function(x,y,param, lev=NULL, last, weights, classProbs, ...) {bms(data.frame(y,x),burn=5000,iter=100000,nmodel=1000,mcmc="bd",g="UIP",mprior.size=param$mprior.size)}
like image 166
Stéphanie C Avatar answered Nov 15 '22 09:11

Stéphanie C