From 7c3c74b614c4c943f61e2e03992ee26b1e177605 Mon Sep 17 00:00:00 2001 From: Marc Becker <33069354+be-marc@users.noreply.github.com> Date: Wed, 13 Nov 2024 16:59:36 +0100 Subject: [PATCH] BREAKING CHANGE: remove $loglik method from all learners (#1207) --- NEWS.md | 1 + R/Learner.R | 3 --- R/MeasureAIC.R | 12 +++++++----- R/MeasureBIC.R | 10 ++++++---- R/mlr_reflections.R | 2 +- man/Learner.Rd | 2 -- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/NEWS.md b/NEWS.md index 7b3909adb..8c9054232 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ * fix: Quantiles must not ascend with probabilities. * refactor: Replace `tsk("boston_housing")` with `tsk("california_housing")`. +* BREAKING CHANGE: Remove ``$loglik()`` method from all learners. # mlr3 0.21.1 diff --git a/R/Learner.R b/R/Learner.R index 213c7855a..72014c8f5 100644 --- a/R/Learner.R +++ b/R/Learner.R @@ -59,9 +59,6 @@ #' * `oob_error(...)`: Returns the out-of-bag error of the model as `numeric(1)`. #' The learner must be tagged with property `"oob_error"`. #' -#' * `loglik(...)`: Extracts the log-likelihood (c.f. [stats::logLik()]). -#' This can be used in measures like [mlr_measures_aic] or [mlr_measures_bic]. -#' #' * `internal_valid_scores`: Returns the internal validation score(s) of the model as a named `list()`. #' Only available for [`Learner`]s with the `"validation"` property. #' If the learner is not trained yet, this returns `NULL`. diff --git a/R/MeasureAIC.R b/R/MeasureAIC.R index 5a0203d1d..e0a988ebc 100644 --- a/R/MeasureAIC.R +++ b/R/MeasureAIC.R @@ -40,12 +40,14 @@ MeasureAIC = R6Class("MeasureAIC", private = list( .score = function(prediction, learner, ...) { learner = learner$base_learner() - if ("loglik" %nin% learner$properties) { - return(NA_real_) - } - k = self$param_set$values$k %??% 2 - return(stats::AIC(learner$loglik(), k = k)) + + tryCatch({ + return(stats::AIC(stats::logLik(learner$model), k = k)) + }, error = function(e) { + warningf("Learner '%s' does not support AIC calculation", learner$id) + return(NA_real_) + }) } ) ) diff --git a/R/MeasureBIC.R b/R/MeasureBIC.R index 94d67d515..b3d48f205 100644 --- a/R/MeasureBIC.R +++ b/R/MeasureBIC.R @@ -38,11 +38,13 @@ MeasureBIC = R6Class("MeasureBIC", private = list( .score = function(prediction, learner, ...) { learner = learner$base_learner() - if ("loglik" %nin% learner$properties) { - return(NA_real_) - } - return(stats::BIC(learner$loglik())) + tryCatch({ + return(stats::BIC(stats::logLik(learner$model))) + }, error = function(e) { + warningf("Learner '%s' does not support BIC calculation", learner$id) + return(NA_real_) + }) } ) ) diff --git a/R/mlr_reflections.R b/R/mlr_reflections.R index e1111dc16..283562658 100644 --- a/R/mlr_reflections.R +++ b/R/mlr_reflections.R @@ -118,7 +118,7 @@ local({ ) ### Learner - tmp = c("featureless", "missings", "weights", "importance", "selected_features", "oob_error", "loglik", "hotstart_forward", "hotstart_backward", "validation", "internal_tuning", "marshal") + tmp = c("featureless", "missings", "weights", "importance", "selected_features", "oob_error", "hotstart_forward", "hotstart_backward", "validation", "internal_tuning", "marshal") mlr_reflections$learner_properties = list( classif = c(tmp, "twoclass", "multiclass"), regr = tmp diff --git a/man/Learner.Rd b/man/Learner.Rd index 8f3aacaeb..fee67c15a 100644 --- a/man/Learner.Rd +++ b/man/Learner.Rd @@ -46,8 +46,6 @@ To filter variables using the importance scores, see package \CRANpkg{mlr3filter The learner must be tagged with property \code{"selected_features"}. \item \code{oob_error(...)}: Returns the out-of-bag error of the model as \code{numeric(1)}. The learner must be tagged with property \code{"oob_error"}. -\item \code{loglik(...)}: Extracts the log-likelihood (c.f. \code{\link[stats:logLik]{stats::logLik()}}). -This can be used in measures like \link{mlr_measures_aic} or \link{mlr_measures_bic}. \item \code{internal_valid_scores}: Returns the internal validation score(s) of the model as a named \code{list()}. Only available for \code{\link{Learner}}s with the \code{"validation"} property. If the learner is not trained yet, this returns \code{NULL}.