Oriol Prat
Oriol Prat

Reputation: 1047

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)

Upvotes: 0

Views: 191

Answers (1)

user_012314112
user_012314112

Reputation: 344

That is because you must specify as.character(param$prior) in the fit function.

Upvotes: 0

Related Questions