Search code examples
rcoefficientsmlr

mlr: Extract penalized logistic regression coefficients


When using mlr, the parameters of the fitted model are (according to the documentation https://mlr-org.github.io/mlr-tutorial/release/html/train/index.html) accessed with getLearnerModel(). However, with penalized logistic regression, this just tells me the number of coefficients, but not what they are. How do I get the coefficient values? Here's an example of where I fail to get the values with getLearnerModel().

library(mlr); library(titanic); suppressMessages(library(tidyverse))
#> Loading required package: ParamHelpers
data("titanic_train")
data <- titanic_train %>% 
  transmute(age = Age,
            class = as.factor(Pclass),
            survived = as.factor(Survived)) %>% 
  drop_na()
glimpse(data)
#> Observations: 714
#> Variables: 3
#> $ age      <dbl> 22, 38, 26, 35, 35, 54, 2, 27, 14, 4, 58, 20, 39, 14,...
#> $ class    <fct> 3, 1, 3, 1, 3, 1, 3, 3, 2, 3, 1, 3, 3, 3, 2, 3, 3, 2,...
#> $ survived <fct> 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0,...
task <- makeClassifTask(data = data, target = "survived")
learner <- makePreprocWrapperCaret("classif.penalized")
#> Loading required package: penalized
#> Loading required package: survival
#> Welcome to penalized. For extended examples, see vignette("penalized").
model <- train(learner, task)
getLearnerModel(model)
#> Model for learner.id=classif.penalized; learner.class=classif.penalized
#> Trained on: task.id = data; obs = 714; features = 2
#> Hyperparameters: trace=FALSE

Created on 2018-04-17 by the reprex package (v0.2.0).


Solution

  • You have created a wrapped learner. To retrieve the base learner of a (multiply) nested learner, use getLearnerModel(model, more.unwrap = TRUE). For your example

    coef(getLearnerModel(model, more.unwrap = TRUE))
    

    should work.