Retrieve models from resample function in mlr

349 Views Asked by At

I would like to retrieve the binary classification models (i.e. selected features and coefficients) generated by resample function in MLR. Below, you can find my code sample. It seems to be located within the attribute models of the resulting object (here r$models), but I don't find it.

# 1. Find a synthetic dataset for supervised learning (two classes)
###################################################################

library(mlbench)
data(BreastCancer)

# generate 1000 rows, 21 quantitative candidate predictors and 1 target variable 
p<-mlbench.waveform(1000) 

# convert list into dataframe
dataset<-as.data.frame(p)

# drop thrid class to get 2 classes
dataset2  = subset(dataset, classes != 3)
dataset2  <- droplevels(dataset2  ) 


# 2. Perform cross validation with embedded feature selection using logistic regression
##########################################################################################

library(BBmisc)
library(mlr)

set.seed(123, "L'Ecuyer")
set.seed(21)

# Choice of data 
mCT <- makeClassifTask(data =dataset2, target = "classes")

# Choice of algorithm 
mL <- makeLearner("classif.logreg", predict.type = "prob")

# Choice of cross-validations for folds 

outer = makeResampleDesc("CV", iters = 10,stratify = TRUE)

# Choice of feature selection method

ctrl = makeFeatSelControlSequential(method = "sbs", maxit = NA,beta = 0.001)

# Choice of sampling between training and test within the fold

inner = makeResampleDesc("Holdout",stratify = TRUE)

lrn = makeFeatSelWrapper(mL, resampling = inner, control = ctrl)
r = resample(lrn, mCT, outer, extract = getFeatSelResult,measures = list(mlr::auc,mlr::acc,mlr::brier),models=TRUE)
1

There are 1 best solutions below

2
On BEST ANSWER

You have to dig a bit deeper in the list. For the first model, for example:

r$models[[1]]$learner.model$opt.result
r$models[[1]]$learner.model$next.model$learner.model