Skip to content

Commit

Permalink
Merge branch 'main' of github.com:mlr-org/mlr3viz
Browse files Browse the repository at this point in the history
  • Loading branch information
be-marc committed Jul 19, 2024
2 parents 98d1651 + 572d3de commit 776f524
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 8 deletions.
13 changes: 11 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Suggests:
mlr3cluster,
mlr3filters,
mlr3fselect (>= 1.0.0),
mlr3inference,
mlr3learners,
mlr3tuning (>= 1.0.0),
paradox,
Expand All @@ -60,13 +61,20 @@ Suggests:
stats,
testthat (>= 3.0.0),
vdiffr (>= 1.0.2),
xgboost
xgboost,
survminer,
mlr3proba (>= 0.6.3)
Remotes:
mlr-org/mlr3proba,
mlr-org/mlr3inference
Additional_repositories:
https://mlr-org.r-universe.dev
Config/testthat/edition: 3
Config/testthat/parallel: true
Encoding: UTF-8
NeedsCompilation: no
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Collate:
'BenchmarkResult.R'
'Filter.R'
Expand All @@ -79,6 +87,7 @@ Collate:
'LearnerRegrCVGlmnet.R'
'LearnerRegrGlmnet.R'
'LearnerRegrRpart.R'
'LearnerSurvCoxPH.R'
'OptimInstanceBatchSingleCrit.R'
'Prediction.R'
'PredictionClassif.R'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ S3method(autoplot,LearnerRegr)
S3method(autoplot,LearnerRegrCVGlmnet)
S3method(autoplot,LearnerRegrGlmnet)
S3method(autoplot,LearnerRegrRpart)
S3method(autoplot,LearnerSurvCoxPH)
S3method(autoplot,OptimInstanceBatchSingleCrit)
S3method(autoplot,PredictionClassif)
S3method(autoplot,PredictionClust)
Expand All @@ -41,6 +42,7 @@ S3method(plot,LearnerClassifRpart)
S3method(plot,LearnerRegrCVGlmnet)
S3method(plot,LearnerRegrGlmnet)
S3method(plot,LearnerRegrRpart)
S3method(plot,LearnerSurvCoxPH)
S3method(plot,PredictionClassif)
S3method(plot,PredictionRegr)
S3method(plot,ResampleResult)
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# mlr3viz (development version)

- Add plot for `LearnerSurvCoxPH`.
- Add plot for confidence intervals (`mlr3inference`)

# mlr3viz 0.9.0

- Work with new bbotk 0.9.0 and mlr3tuning 0.21.0

- Add plots for `EnsembleFSResult` object.

# mlr3viz 0.8.0
Expand Down
38 changes: 38 additions & 0 deletions R/BenchmarkResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#' Requires package \CRANpkg{precrec}.
#' * `"prc"`: Precision recall curve.
#' See `"roc"`.
#' * `"ci"`: Plot confidence intervals. Pass a `msr("ci", ...)` from the `mlr3inference` package as argument `measure`.
#'
#' @param object ([mlr3::BenchmarkResult]).
#' @template param_type
Expand Down Expand Up @@ -45,6 +46,43 @@ autoplot.BenchmarkResult = function(object, type = "boxplot", measure = NULL, th

task = object$tasks$task[[1L]]
measure = mlr3::assert_measure(mlr3::as_measure(measure, task_type = task$task_type), task = task)

if (identical(type, "ci")) {
mlr3misc::require_namespaces("mlr3inference")

assert_class(measure, "MeasureAbstractCi")
mid = measure$id

tbl = object$aggregate(measure)

tmp = map(object$resamplings$resampling, function(x) {
list(class(x), x$param_set$values)
})

if (length(unique(tmp)) != 1) {
stopf("Plot of type 'ci' requires exactly one resampling method")
}

# static checker
.data = NULL
task_id = NULL
p = ggplot(tbl, aes(x = .data[["learner_id"]], y = .data[[mid]])) +
geom_point() +
geom_errorbar(aes(ymin = .data[[paste0(mid, ".lower")]], ymax = .data[[paste0(mid, ".upper")]]), width = 0.2) +
facet_wrap(vars(task_id), scales = "free_y") +
labs(
title = sprintf("Confidence Intervals for alpha = %s", measure$param_set$values$alpha),
x = "Learner",
y = paste0(measure$measure$id)
) +
theme +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
axis.title.x = element_blank()
)
return(p)
}

measure_id = measure$id
tab = fortify(object, measure = measure)
tab$nr = sprintf("%09d", tab$nr)
Expand Down
49 changes: 49 additions & 0 deletions R/LearnerSurvCoxPH.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#' @title Plots for Cox Proportional Hazards Learner
#'
#' @description
#' Visualizations for [mlr3proba::LearnerSurvCoxPH].
#'
#' The argument `type` controls what kind of plot is drawn.
#' The only possible choice right now is `"ggforest"` (default) which is a
#' Forest Plot, using [ggforest][survminer::ggforest()].
#' This plot displays the estimated hazard ratios (HRs) and their confidence
#' intervals (CIs) for different variables included in the (trained) model.
#'
#' @param object ([mlr3proba::LearnerSurvCoxPH]).
#'
#' @template param_type
#' @param ... Additional parameters passed down to `ggforest`.
#'
#' @return [ggplot2::ggplot()].
#'
#' @export
#' @examples
#' \donttest{
#' if (requireNamespace("mlr3proba")) {
#' library(mlr3proba)
#' library(mlr3viz)
#'
#' task = tsk("lung")
#' learner = lrn("surv.coxph")
#' learner$train(task)
#' autoplot(learner)
#' }
#' }
autoplot.LearnerSurvCoxPH = function(object, type = "ggforest", ...) {
assert_class(object, classes = "LearnerSurvCoxPH", null.ok = FALSE)
assert_has_model(object)

switch(type,
"ggforest" = {
require_namespaces("survminer")
suppressWarnings(survminer::ggforest(object$model, ...))
},

stopf("Unknown plot type '%s'", type)
)
}

#' @export
plot.LearnerSurvCoxPH = function(x, ...) {
print(autoplot(x, ...))
}
1 change: 1 addition & 0 deletions man/autoplot.BenchmarkResult.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/autoplot.EnsembleFSResult.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/autoplot.LearnerClustHierarchical.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions man/autoplot.LearnerSurvCoxPH.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/mlr3viz-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/testthat/test_BenchmarkResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,19 @@ test_that("holdout roc plot (#54)", {

expect_doppelganger("bmr_holdout_roc", p)
})

skip_if_not_installed("mlr3inference")
skip_if_not_installed("rpart")

test_that("CI plot", {
bmr = benchmark(benchmark_grid(tsks(c("mtcars", "boston_housing")),
lrns(c("regr.featureless", "regr.rpart")), rsmp("holdout")))

p = autoplot(bmr, "ci", msr("ci", "regr.mse"))
expect_true(is.ggplot(p))
expect_doppelganger("bmr_holdout_ci", p)

bmr = benchmark(benchmark_grid(tsk("iris"), lrn("classif.rpart"),
rsmps(c("holdout", "cv"))))
expect_error(autoplot(bmr, "ci", msr("ci", "classif.acc")), "one resampling method")
})

0 comments on commit 776f524

Please sign in to comment.