|
| 1 | +#' @title Classification Priority Lasso Learner |
| 2 | +#' @author HarutyunyanLiana |
| 3 | +#' @name mlr_learners_classif.priority_lasso |
| 4 | +#' |
| 5 | +#' @description |
| 6 | +#' Patient outcome prediction based on multi-omics data taking practitioners’ preferences into account. |
| 7 | +#' Calls [prioritylasso::prioritylasso()] from \CRANpkg{prioritylasso}. |
| 8 | +#' |
| 9 | +#' @templateVar id classif.priority_lasso |
| 10 | +#' @template learner |
| 11 | +#' |
| 12 | +#' @references |
| 13 | +#' `r format_bib("klau2018priolasso")` |
| 14 | +#' |
| 15 | +#' @template seealso_learner |
| 16 | +#' @template example |
| 17 | +#' @export |
| 18 | +LearnerClassifPriorityLasso = R6Class("LearnerClassifPriorityLasso", |
| 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 | + blocks = p_uty(default = NULL, tags = c("train", "required")), |
| 26 | + type.measure = p_fct(default = "class", levels = c("class", "auc"), tags = c("train", "required")), |
| 27 | + max.coef = p_uty(default = NULL, tags = "train"), |
| 28 | + block1.penalization = p_lgl(default = TRUE, tags = "train"), |
| 29 | + lambda.type = p_fct(default = "lambda.min", levels = c("lambda.min", "lambda.1se"), tags = c("train", "predict")), #nolint |
| 30 | + standardize = p_lgl(default = TRUE, tags = "train"), |
| 31 | + nfolds = p_int(default = 5L, lower = 1L, tags = "train"), |
| 32 | + foldid = p_uty(default = NULL, tags = "train"), |
| 33 | + cvoffset = p_lgl(default = FALSE, tags = "train"), |
| 34 | + cvoffsetnfolds = p_int(default = 10, lower = 1L, tags = "train"), |
| 35 | + |
| 36 | + # params from cv.glmnet |
| 37 | + alignment = p_fct(c("lambda", "fraction"), default = "lambda", tags = "train"), |
| 38 | + alpha = p_dbl(0, 1, default = 1, tags = "train"), |
| 39 | + big = p_dbl(default = 9.9e35, tags = "train"), |
| 40 | + devmax = p_dbl(0, 1, default = 0.999, tags = "train"), |
| 41 | + dfmax = p_int(0L, tags = "train"), |
| 42 | + eps = p_dbl(0, 1, default = 1.0e-6, tags = "train"), |
| 43 | + epsnr = p_dbl(0, 1, default = 1.0e-8, tags = "train"), |
| 44 | + exclude = p_uty(tags = "train"), |
| 45 | + exmx = p_dbl(default = 250.0, tags = "train"), |
| 46 | + fdev = p_dbl(0, 1, default = 1.0e-5, tags = "train"), |
| 47 | + gamma = p_uty(tags = "train"), |
| 48 | + grouped = p_lgl(default = TRUE, tags = "train"), |
| 49 | + intercept = p_lgl(default = TRUE, tags = "train"), |
| 50 | + keep = p_lgl(default = FALSE, tags = "train"), |
| 51 | + lambda = p_uty(tags = "train"), |
| 52 | + lambda.min.ratio = p_dbl(0, 1, tags = "train"), |
| 53 | + lower.limits = p_uty(default = -Inf, tags = "train"), |
| 54 | + maxit = p_int(1L, default = 100000L, tags = "train"), |
| 55 | + mnlam = p_int(1L, default = 5L, tags = "train"), |
| 56 | + mxit = p_int(1L, default = 100L, tags = "train"), |
| 57 | + mxitnr = p_int(1L, default = 25L, tags = "train"), |
| 58 | + nlambda = p_int(1L, default = 100L, tags = "train"), |
| 59 | + offset = p_uty(default = NULL, tags = "train"), |
| 60 | + parallel = p_lgl(default = FALSE, tags = "train"), |
| 61 | + penalty.factor = p_uty(tags = "train"), |
| 62 | + pmax = p_int(0L, tags = "train"), |
| 63 | + pmin = p_dbl(0, 1, default = 1.0e-9, tags = "train"), |
| 64 | + prec = p_dbl(default = 1e-10, tags = "train"), |
| 65 | + predict.gamma = p_dbl(default = "gamma.1se", special_vals = list("gamma.1se", "gamma.min"), tags = "predict"), #nolint |
| 66 | + relax = p_lgl(default = FALSE, tags = "train"), |
| 67 | + s = p_dbl(0, 1, special_vals = list("lambda.1se", "lambda.min"), default = "lambda.1se", tags = "predict"), #nolint |
| 68 | + standardize.response = p_lgl(default = FALSE, tags = "train"), |
| 69 | + thresh = p_dbl(0, default = 1e-07, tags = "train"), |
| 70 | + trace.it = p_int(0, 1, default = 0, tags = "train"), |
| 71 | + type.gaussian = p_fct(c("covariance", "naive"), tags = "train"), |
| 72 | + type.logistic = p_fct(c("Newton", "modified.Newton"), default = "Newton", tags = "train"), |
| 73 | + type.multinomial = p_fct(c("ungrouped", "grouped"), default = "ungrouped", tags = "train"), |
| 74 | + upper.limits = p_uty(default = Inf, tags = "train") |
| 75 | + ) |
| 76 | + |
| 77 | + super$initialize( |
| 78 | + id = "classif.priority_lasso", |
| 79 | + packages = "prioritylasso", |
| 80 | + feature_types = c("logical", "integer", "numeric"), |
| 81 | + predict_types = c("response", "prob"), |
| 82 | + param_set = param_set, |
| 83 | + properties = c("weights", "selected_features", "twoclass"), |
| 84 | + man = "mlr3extralearners::mlr_learners_classif.priority_lasso", |
| 85 | + label = "Priority Lasso" |
| 86 | + ) |
| 87 | + }, |
| 88 | + |
| 89 | + #' @description |
| 90 | + #' Selected features, i.e. those where the coefficient is positive. |
| 91 | + #' @return `character()`. |
| 92 | + selected_features = function() { |
| 93 | + if (is.null(self$model)) { |
| 94 | + stopf("No model stored") |
| 95 | + } |
| 96 | + coefs = self$model$coefficients |
| 97 | + coefs = coefs[coefs != 0] |
| 98 | + names(coefs) |
| 99 | + } |
| 100 | + ), |
| 101 | + private = list( |
| 102 | + .train = function(task) { |
| 103 | + # get parameters for training |
| 104 | + pars = self$param_set$get_values(tags = "train") |
| 105 | + pars$family = "binomial" |
| 106 | + |
| 107 | + if ("weights" %in% task$properties) { |
| 108 | + pars$weights = task$weights$weight |
| 109 | + } |
| 110 | + data = as_numeric_matrix(task$data(cols = task$feature_names)) |
| 111 | + target = task$truth() |
| 112 | + invoke(prioritylasso::prioritylasso, |
| 113 | + X = data, Y = target, |
| 114 | + .args = pars) |
| 115 | + }, |
| 116 | + .predict = function(task) { |
| 117 | + newdata = as_numeric_matrix(ordered_features(task, self)) |
| 118 | + pv = self$param_set$get_values(tags = "predict") |
| 119 | + pv = rename(pv, "predict.gamma", "gamma") |
| 120 | + |
| 121 | + p = invoke(predict, self$model, |
| 122 | + newdata = newdata, type = "response", |
| 123 | + .args = pv) |
| 124 | + p = drop(p) |
| 125 | + classnames = self$model$glmnet.fit[[1L]]$classnames |
| 126 | + if (self$predict_type == "response") { |
| 127 | + response = ifelse(p <= 0.5, classnames[1L], classnames[2L]) |
| 128 | + list(response = drop(response)) |
| 129 | + } else { |
| 130 | + prob = cbind(1 - p, p) |
| 131 | + colnames(prob) = classnames |
| 132 | + list(prob = prob) |
| 133 | + } |
| 134 | + } |
| 135 | + ) |
| 136 | +) |
| 137 | + |
| 138 | +.extralrns_dict$add("classif.priority_lasso", LearnerClassifPriorityLasso) |
0 commit comments