|
| 1 | +#' @title Classification Logistic Regression Learner |
| 2 | +#' @author annanzrv |
| 3 | +#' @name mlr_learners_classif.stepPlr |
| 4 | +#' |
| 5 | +#' @description |
| 6 | +#' Logistic regression with a quadratic penalization on the coefficient. |
| 7 | +#' Calls [stepPlr::plr()] from \CRANpkg{stepPlr}. |
| 8 | +#' |
| 9 | +#' @templateVar id classif.stepPlr |
| 10 | +#' @template learner |
| 11 | +#' |
| 12 | +#' @references |
| 13 | +#' `r format_bib("park2008plr")` |
| 14 | +#' |
| 15 | +#' @template seealso_learner |
| 16 | +#' @template example |
| 17 | +#' @export |
| 18 | +LearnerClassifStepPlr = R6Class("LearnerClassifStepPlr", |
| 19 | + inherit = LearnerClassif, |
| 20 | + public = list( |
| 21 | + #' @description |
| 22 | + #' Creates a new instance of this [R6][R6::R6Class] class. |
| 23 | + initialize = function() { |
| 24 | + param_set = ps( |
| 25 | + cp = p_fct(default = "aic", levels = c("aic", "bic"), tags = "train"), |
| 26 | + lambda = p_dbl(default = 1e-4, lower = 0, tags = "train"), |
| 27 | + offset.coefficients = p_uty(tags = "train"), |
| 28 | + offset.subset = p_uty(tags = "train") |
| 29 | + ) |
| 30 | + |
| 31 | + super$initialize( |
| 32 | + id = "classif.stepPlr", |
| 33 | + packages = "stepPlr", |
| 34 | + feature_types = c("logical", "integer", "numeric"), |
| 35 | + predict_types = c("response", "prob"), |
| 36 | + param_set = param_set, |
| 37 | + properties = c("twoclass", "weights"), |
| 38 | + man = "mlr3extralearners::mlr_learners_classif.stepPlr", |
| 39 | + label = "Logistic Regression with a L2 Penalty" |
| 40 | + ) |
| 41 | + } |
| 42 | + ), |
| 43 | + private = list( |
| 44 | + .train = function(task) { |
| 45 | + pars = self$param_set$get_values(tags = "train") |
| 46 | + data = as.matrix(task$data(cols = task$feature_names)) |
| 47 | + y = as.numeric(task$data()[[task$target_names]]) - 1 |
| 48 | + pars$weights = private$.get_weights(task) |
| 49 | + invoke( |
| 50 | + stepPlr::plr, |
| 51 | + x = data, |
| 52 | + y = y, |
| 53 | + .args = pars |
| 54 | + ) |
| 55 | + }, |
| 56 | + .predict = function(task) { |
| 57 | + pars = self$param_set$get_values(tags = "predict") |
| 58 | + newdata = ordered_features(task, self) |
| 59 | + # Remove target column if present in newdata |
| 60 | + if ( |
| 61 | + length(task$target_names) > 0 && task$target_names %in% colnames(newdata) |
| 62 | + ) { |
| 63 | + newx = as.matrix(newdata[, !task$target_names, with = FALSE]) |
| 64 | + } else { |
| 65 | + newx = as.matrix(newdata) |
| 66 | + } |
| 67 | + |
| 68 | + type = if (self$predict_type == "prob") "response" else "class" |
| 69 | + pred = invoke(predict, self$model, newx = newx, type = type, .args = pars) |
| 70 | + |
| 71 | + if (type == "class") { |
| 72 | + levels = task$class_names |
| 73 | + response = factor(pred, levels = seq_along(levels) - 1, labels = levels) |
| 74 | + list(response = response) |
| 75 | + } else { |
| 76 | + prob = pprob_to_matrix(1 - unname(pred), task) |
| 77 | + list(prob = prob) |
| 78 | + } |
| 79 | + } |
| 80 | + ) |
| 81 | +) |
| 82 | + |
| 83 | +.extralrns_dict$add("classif.stepPlr", LearnerClassifStepPlr) |
0 commit comments