简体   繁体   中英

error tuning custom algorithm with caret r

I want to tune two parameters of my custom algorithm with caret. Un parameter (lambda) is numeric and the other parameter (prior) is character. This parameter can take two values "known" or "unknown". I've tuned the algorithm with just the lambda parameter. It's okay. But when I add the character parameter (prior) gives me the following error:

1: In eval(expr, envir, enclos) : model fit failed for Resample01: lambda=1, prior=unknown Error in mdp(Class = y, data = x, lambda = param$lambda, prior = param$prior, : object 'assignment' not found

the error must be related with the way to specify the character parameter (prior). Here is my code:

my_mod$parameters <- data.frame(
  parameter = c("lambda","prior"),
  class = c("numeric", "character"),
  label = c("sample_length", "prior_type"))

## The grid Element

my_mod$grid <- function(x, y, len = NULL){expand.grid(lambda=1:2,prior=c("unknown", "known"))}

mygrid<-expand.grid(lambda=1:2,prior=c('unknown','known'))


## The fit Element

my_mod$fit <- function(x, y, wts, param, lev, last, classProbs, ...){ 
  mdp(Class=y,data=x,lambda=param$lambda,prior=param$prior,info.pred ="yes")
}

## The predict Element

mdpPred <- function(modelFit, newdata, preProc = NULL, submodels = NULL)
  predict.mdp(modelFit, newdata)
my_mod$predict <- mdpPred

fitControl <- trainControl(method = "cv",number = 5,repeats = 5)

train(x=data, y = factor(Class),method = my_mod,trControl = fitControl, tuneGrid = mygrid)

那是因为您必须在fit函数中指定as.character(param$prior)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM