From 3a4835b962dbce7a229ad3ce832d5d2df5894873 Mon Sep 17 00:00:00 2001 From: john Date: Mon, 21 Oct 2024 18:29:43 +0200 Subject: [PATCH 01/18] refactor: add helper function to create a graph learner --- R/helpers.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/R/helpers.R b/R/helpers.R index 5b112bca7..03d712a57 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -50,3 +50,12 @@ ordered_features = function(task, learner) { cols = names(learner$state$data_prototype) %??% learner$state$feature_names task$data(cols = intersect(cols, task$feature_names)) } + +## create GraphLearner +create_grlrn = function(gr, graph_learner = FALSE) { + if (graph_learner) { + gr = as_learner(gr) + } + + gr +} From ca305c19b335304e61d1176955914f047f990489 Mon Sep 17 00:00:00 2001 From: john Date: Mon, 21 Oct 2024 18:31:47 +0200 Subject: [PATCH 02/18] add mlr3pipelines to Imports and set minimum latest version from CRAN --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1ad0b1e60..b03ba9bf0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -60,6 +60,7 @@ Imports: distr6 (>= 1.8.4), ggplot2, mlr3misc (>= 0.7.0), + mlr3pipelines (>= 0.7.0), mlr3viz, paradox (>= 1.0.0), R6, @@ -71,7 +72,6 @@ Suggests: knitr, lgr, lifecycle, - mlr3pipelines (>= 0.3.4), param6 (>= 0.2.4), pracma, rpart, From 4cd46b22ab9d72a472396b2dbd9e10f0456055fa Mon Sep 17 00:00:00 2001 From: john Date: Mon, 21 Oct 2024 18:32:58 +0200 Subject: [PATCH 03/18] change style --- R/PipeOpPredClassifSurvDiscTime.R | 3 +-- R/PipeOpPredClassifSurvIPCW.R | 3 +-- R/PipeOpTaskSurvClassifIPCW.R | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/R/PipeOpPredClassifSurvDiscTime.R b/R/PipeOpPredClassifSurvDiscTime.R index 5671041cd..9de1203e6 100644 --- a/R/PipeOpPredClassifSurvDiscTime.R +++ b/R/PipeOpPredClassifSurvDiscTime.R @@ -37,8 +37,7 @@ #' @family PipeOps #' @family Transformation PipeOps #' @export -PipeOpPredClassifSurvDiscTime = R6Class( - "PipeOpPredClassifSurvDiscTime", +PipeOpPredClassifSurvDiscTime = R6Class("PipeOpPredClassifSurvDiscTime", inherit = mlr3pipelines::PipeOp, public = list( diff --git a/R/PipeOpPredClassifSurvIPCW.R b/R/PipeOpPredClassifSurvIPCW.R index e5f3273ae..dffff1e70 100644 --- a/R/PipeOpPredClassifSurvIPCW.R +++ b/R/PipeOpPredClassifSurvIPCW.R @@ -39,8 +39,7 @@ #' @family PipeOps #' @family Transformation PipeOps #' @export -PipeOpPredClassifSurvIPCW = R6Class( - "PipeOpPredClassifSurvIPCW", +PipeOpPredClassifSurvIPCW = R6Class("PipeOpPredClassifSurvIPCW", inherit = mlr3pipelines::PipeOp, public = list( diff --git a/R/PipeOpTaskSurvClassifIPCW.R b/R/PipeOpTaskSurvClassifIPCW.R index 371a4a52e..4d54d1313 100644 --- a/R/PipeOpTaskSurvClassifIPCW.R +++ b/R/PipeOpTaskSurvClassifIPCW.R @@ -100,8 +100,7 @@ #' p$score(msr("classif.auc")) #' } #' @export -PipeOpTaskSurvClassifIPCW = R6Class( - "PipeOpTaskSurvClassifIPCW", +PipeOpTaskSurvClassifIPCW = R6Class("PipeOpTaskSurvClassifIPCW", inherit = mlr3pipelines::PipeOp, public = list( From 5133cf3a708fe7a5ed23d3de86001cc03e8ffae6 Mon Sep 17 00:00:00 2001 From: john Date: Mon, 21 Oct 2024 22:55:28 +0200 Subject: [PATCH 04/18] import mlr3pipelines functions and shorthand constructors --- NAMESPACE | 9 +++++++-- R/zzz.R | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 0887aa0e2..ada0238a7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -115,13 +115,18 @@ import(paradox) importFrom(R6,R6Class) importFrom(Rcpp,sourceCpp) importFrom(graphics,plot) +importFrom(mlr3pipelines,"%>>%") +importFrom(mlr3pipelines,Graph) +importFrom(mlr3pipelines,as_graph) +importFrom(mlr3pipelines,gunion) +importFrom(mlr3pipelines,pipeline_greplicate) +importFrom(mlr3pipelines,po) +importFrom(mlr3pipelines,ppl) importFrom(mlr3viz,fortify) -importFrom(stats,complete.cases) importFrom(stats,density) importFrom(stats,model.frame) importFrom(stats,model.matrix) importFrom(stats,predict) -importFrom(stats,reformulate) importFrom(stats,sd) importFrom(survival,Surv) importFrom(utils,data) diff --git a/R/zzz.R b/R/zzz.R index bad8118b7..904a59cf2 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -28,10 +28,11 @@ NULL #' @import paradox #' @importFrom R6 R6Class #' @importFrom utils data head tail -#' @importFrom stats reformulate model.matrix model.frame sd predict complete.cases density +#' @importFrom stats model.matrix model.frame sd predict density #' @importFrom survival Surv #' @importFrom mlr3viz fortify #' @importFrom utils getFromNamespace +#' @importFrom mlr3pipelines po as_graph %>>% pipeline_greplicate gunion Graph ppl "_PACKAGE" # nolint end From 678e0c163eb05d1bbe8afdb852b0868113a9335b Mon Sep 17 00:00:00 2001 From: john Date: Mon, 21 Oct 2024 22:56:42 +0200 Subject: [PATCH 05/18] refactor: use mlr3pipelines function without namespace :: --- R/pipelines.R | 230 ++++++++++++++++++++------------------------------ 1 file changed, 92 insertions(+), 138 deletions(-) diff --git a/R/pipelines.R b/R/pipelines.R index e37190e0b..693c2ee82 100644 --- a/R/pipelines.R +++ b/R/pipelines.R @@ -10,8 +10,8 @@ #' #' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' library("mlr3") -#' library("mlr3pipelines") +#' library(mlr3) +#' library(mlr3pipelines) #' #' task = tsk("rats") #' pipe = ppl( @@ -24,16 +24,11 @@ #' pipe$predict(task) #' } pipeline_survaverager = function(learners, param_vals = list(), graph_learner = FALSE) { - learners = mlr3pipelines::gunion(map(learners, mlr3pipelines::as_graph)) - po = mlr3pipelines::po("survavg", param_vals = param_vals) - - gr = mlr3pipelines::`%>>%`(learners, po) - - if (graph_learner) { - gr = mlr3pipelines::GraphLearner$new(gr) - } + learners = gunion(map(learners, as_graph)) + po_survavg = po("survavg", param_vals = param_vals) - gr + gr = learners %>>% po_survavg + create_grlrn(gr, graph_learner) } #' @template pipeline @@ -63,8 +58,8 @@ pipeline_survaverager = function(learners, param_vals = list(), graph_learner = #' #' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' library("mlr3") -#' library("mlr3pipelines") +#' library(mlr3) +#' library(mlr3pipelines) #' #' task = tsk("rats") #' pipe = ppl( @@ -82,23 +77,16 @@ pipeline_survbagging = function(learner, iterations = 10, frac = 0.7, avg = TRUE assert_count(iterations) assert_number(frac, lower = 0, upper = 1) - graph = mlr3pipelines::as_graph(learner) - subs = mlr3pipelines::`%>>%`(mlr3pipelines::po("subsample", param_vals = list(frac = frac)), - graph) - subs_repls = mlr3pipelines::pipeline_greplicate(subs, iterations) + graph = as_graph(learner) + subs = po("subsample", param_vals = list(frac = frac)) %>>% graph + subs_repls = pipeline_greplicate(subs, iterations) if (!avg) { return(subs_repls) } else { - po = mlr3pipelines::po("survavg", param_vals = list(weights = weights)) - - gr = mlr3pipelines::`%>>%`(subs_repls, po) - - if (graph_learner) { - gr = mlr3pipelines::GraphLearner$new(gr) - } - - gr + po_survavg = po("survavg", param_vals = list(weights = weights)) + gr = subs_repls %>>% po_survavg + create_grlrn(gr, graph_learner) } } @@ -118,8 +106,8 @@ pipeline_survbagging = function(learner, iterations = 10, frac = 0.7, avg = TRUE #' #' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' library("mlr3") -#' library("mlr3pipelines") +#' library(mlr3) +#' library(mlr3pipelines) #' #' task = tsk("lung") #' part = partition(task) @@ -142,18 +130,13 @@ pipeline_crankcompositor = function(learner, method = c("mort"), assert_logical(overwrite) assert_logical(graph_learner) - pred = mlr3pipelines::as_graph(learner) + pred = as_graph(learner) pv = list(method = method, overwrite = overwrite) - compositor = mlr3pipelines::po("crankcompose", param_vals = pv) - - gr = mlr3pipelines::`%>>%`(pred, compositor) - - if (graph_learner) { - gr = mlr3pipelines::GraphLearner$new(gr) - } + compositor = po("crankcompose", param_vals = pv) - gr + gr = pred %>>% compositor + create_grlrn(gr, graph_learner) } #' @template pipeline @@ -177,8 +160,8 @@ pipeline_crankcompositor = function(learner, method = c("mort"), #' #' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' library("mlr3") -#' library("mlr3pipelines") +#' library(mlr3) +#' library(mlr3pipelines) #' #' task = tsk("lung") #' part = partition(task) @@ -204,19 +187,14 @@ pipeline_responsecompositor = function(learner, method = "rmst", tau = NULL, assert_logical(overwrite) assert_logical(graph_learner) - pred = mlr3pipelines::as_graph(learner) + pred = as_graph(learner) pv = list(method = method, tau = tau, add_crank = add_crank, overwrite = overwrite) - compositor = mlr3pipelines::po("responsecompose", param_vals = pv) - - gr = mlr3pipelines::`%>>%`(pred, compositor) - - if (graph_learner) { - gr = mlr3pipelines::GraphLearner$new(gr) - } + compositor = po("responsecompose", param_vals = pv) - gr + gr = pred %>>% compositor + create_grlrn(gr, graph_learner) } #' @template pipeline @@ -244,7 +222,7 @@ pipeline_responsecompositor = function(learner, method = "rmst", tau = NULL, #' #' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' library("mlr3pipelines") +#' library(mlr3pipelines) #' #' # let's change the distribution prediction of Cox (Breslow-based) to an AFT form: #' task = tsk("rats") @@ -268,24 +246,19 @@ pipeline_distrcompositor = function(learner, estimator = "kaplan", form = "aft", # make the pipeline Graph object if (estimator == "breslow") { - gr = mlr3pipelines::as_graph( - mlr3pipelines::po("breslowcompose", learner = learner, breslow.overwrite = overwrite) - ) + gr = as_graph(po("breslowcompose", learner = learner, breslow.overwrite = overwrite)) } else { - pred = mlr3pipelines::as_graph(learner) - base = mlr3pipelines::po("learner", - lrn(paste0("surv.", estimator), id = paste0("distrcompositor.", estimator))) + pred = as_graph(learner) + learner_key = paste0("surv.", estimator) + learner_id = paste0("distrcompositor.", estimator) + base = po("learner", lrn(learner_key, id = learner_id)) - compositor = mlr3pipelines::po("distrcompose", param_vals = list(form = form, overwrite = overwrite)) + compositor = po("distrcompose", param_vals = list(form = form, overwrite = overwrite)) - gr = mlr3pipelines::`%>>%`(mlr3pipelines::gunion(list(base, pred)), compositor) + gr = gunion(list(base, pred)) %>>% compositor } - if (graph_learner) { - gr = mlr3pipelines::GraphLearner$new(gr) - } - - gr + create_grlrn(gr, graph_learner) } #' @template pipeline @@ -304,8 +277,8 @@ pipeline_distrcompositor = function(learner, estimator = "kaplan", form = "aft", #' Current possibilities are' `"Cauchy", "Gumbel", "Laplace", "Logistic", "Normal", "Uniform"`. Default is `"Uniform"`. #' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines", "rpart"), quietly = TRUE) #' \dontrun{ -#' library("mlr3") -#' library("mlr3pipelines") +#' library(mlr3) +#' library(mlr3pipelines) #' #' task = tsk("boston_housing") #' @@ -331,26 +304,23 @@ pipeline_distrcompositor = function(learner, estimator = "kaplan", form = "aft", pipeline_probregr = function(learner, learner_se = NULL, dist = "Uniform", graph_learner = FALSE) { - gr = mlr3pipelines::Graph$new()$add_pipeop(mlr3pipelines::po("compose_probregr", param_vals = list(dist = dist))) + gr = Graph$new()$ + add_pipeop(po("compose_probregr", param_vals = list(dist = dist))) if (is.null(learner_se)) { learner$predict_type = "se" - gr$add_pipeop(mlr3pipelines::po("learner", learner, id = "response_learner"))$ + gr$add_pipeop(po("learner", learner, id = "response_learner"))$ add_edge("response_learner", "compose_probregr", dst_channel = "input_response")$ add_edge("response_learner", "compose_probregr", dst_channel = "input_se") } else { learner_se$predict_type = "se" - gr$add_pipeop(mlr3pipelines::po("learner", learner, id = "response_learner"))$ - add_pipeop(mlr3pipelines::po("learner", learner_se, id = "se_learner"))$ + gr$add_pipeop(po("learner", learner, id = "response_learner"))$ + add_pipeop(po("learner", learner_se, id = "se_learner"))$ add_edge("response_learner", "compose_probregr", dst_channel = "input_response")$ add_edge("se_learner", "compose_probregr", dst_channel = "input_se") } - if (graph_learner) { - gr = mlr3pipelines::GraphLearner$new(gr) - } - - gr + create_grlrn(gr, graph_learner) } #' @name mlr_graphs_survtoregr @@ -442,8 +412,8 @@ pipeline_probregr = function(learner, learner_se = NULL, dist = "Uniform", #' #' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' library("mlr3") -#' library("mlr3pipelines") +#' library(mlr3) +#' library(mlr3pipelines) #' #' task = tsk("rats") #' @@ -491,35 +461,34 @@ pipeline_survtoregr = function(method = 1, regr_learner = lrn("regr.featureless" graph_learner = FALSE) { if (method == 1) { - gr = mlr3pipelines::Graph$new()$ - add_pipeop(mlr3pipelines::po("nop", id = "task_surv"))$ - add_pipeop(mlr3pipelines::po("trafotask_survregr", param_vals = survregr_params))$ - add_pipeop(mlr3pipelines::po("learner", regr_learner, id = "regr_learner"))$ - add_pipeop(mlr3pipelines::po("trafopred_regrsurv"))$ + gr = Graph$new()$ + add_pipeop(po("nop", id = "task_surv"))$ + add_pipeop(po("trafotask_survregr", param_vals = survregr_params))$ + add_pipeop(po("learner", regr_learner, id = "regr_learner"))$ + add_pipeop(po("trafopred_regrsurv"))$ add_edge("task_surv", "trafotask_survregr", dst_channel = "input")$ add_edge("trafotask_survregr", "regr_learner")$ add_edge("regr_learner", "trafopred_regrsurv", dst_channel = "pred")$ add_edge("task_surv", "trafopred_regrsurv", dst_channel = "task") } else if (method == 2) { - - gr = mlr3pipelines::Graph$new()$ - add_pipeop(mlr3pipelines::po("nop", id = "task_surv"))$ - add_pipeop(mlr3pipelines::po("trafotask_survregr", param_vals = survregr_params))$ - add_pipeop(mlr3pipelines::po("compose_probregr", param_vals = probregr_params))$ - add_pipeop(mlr3pipelines::po("trafopred_regrsurv"))$ + gr = Graph$new()$ + add_pipeop(po("nop", id = "task_surv"))$ + add_pipeop(po("trafotask_survregr", param_vals = survregr_params))$ + add_pipeop(po("compose_probregr", param_vals = probregr_params))$ + add_pipeop(po("trafopred_regrsurv"))$ add_edge("compose_probregr", "trafopred_regrsurv", dst_channel = "pred")$ add_edge("task_surv", "trafopred_regrsurv", dst_channel = "task") if (!is.null(regr_se_learner)) { regr_se_learner$predict_type = "se" - gr$add_pipeop(mlr3pipelines::po("learner", regr_learner, id = "regr_learner"))$ - add_pipeop(mlr3pipelines::po("learner", regr_se_learner, id = "regr_se_learner"))$ + gr$add_pipeop(po("learner", regr_learner, id = "regr_learner"))$ + add_pipeop(po("learner", regr_se_learner, id = "regr_se_learner"))$ add_edge("trafotask_survregr", "regr_se_learner")$ add_edge("regr_se_learner", "compose_probregr", dst_channel = "input_se") } else { regr_learner$predict_type = "se" - gr$add_pipeop(mlr3pipelines::po("learner", regr_learner, id = "regr_learner"))$ + gr$add_pipeop(po("learner", regr_learner, id = "regr_learner"))$ add_edge("regr_learner", "compose_probregr", dst_channel = "input_se") } @@ -529,14 +498,13 @@ pipeline_survtoregr = function(method = 1, regr_learner = lrn("regr.featureless" } else if (method == 3) { assert("lp" %in% surv_learner$predict_types) - gr = mlr3pipelines::Graph$new()$ - add_pipeop(mlr3pipelines::po("nop", id = "task_surv_train"))$ - add_pipeop(mlr3pipelines::po("nop", id = "task_surv_predict"))$ - add_pipeop(mlr3pipelines::po("learner_cv", surv_learner, id = "surv_learner", - param_vals = learnercv_params))$ - add_pipeop(mlr3pipelines::po("trafotask_survregr", method = "reorder", target = "surv_learner.lp"))$ - add_pipeop(mlr3pipelines::po("learner", regr_learner, id = "regr_learner"))$ - add_pipeop(mlr3pipelines::po("trafopred_regrsurv", target_type = "lp"))$ + gr = Graph$new()$ + add_pipeop(po("nop", id = "task_surv_train"))$ + add_pipeop(po("nop", id = "task_surv_predict"))$ + add_pipeop(po("learner_cv", surv_learner, id = "surv_learner", param_vals = learnercv_params))$ + add_pipeop(po("trafotask_survregr", method = "reorder", target = "surv_learner.lp"))$ + add_pipeop(po("learner", regr_learner, id = "regr_learner"))$ + add_pipeop(po("trafopred_regrsurv", target_type = "lp"))$ add_edge("surv_learner", "trafotask_survregr", dst_channel = "input")$ add_edge("task_surv_train", "trafotask_survregr", dst_channel = "input_features")$ add_edge("trafotask_survregr", "regr_learner")$ @@ -546,18 +514,14 @@ pipeline_survtoregr = function(method = 1, regr_learner = lrn("regr.featureless" if (distrcompose) { assert("distr" %in% distr_estimator$predict_types) - gr$add_pipeop(mlr3pipelines::po("learner", distr_estimator, id = "distr_estimator"))$ - add_pipeop(mlr3pipelines::po("distrcompose", param_vals = distrcompose_params))$ + gr$add_pipeop(po("learner", distr_estimator, id = "distr_estimator"))$ + add_pipeop(po("distrcompose", param_vals = distrcompose_params))$ add_edge("trafopred_regrsurv", dst_id = "distrcompose", dst_channel = "pred")$ add_edge("distr_estimator", dst_id = "distrcompose", dst_channel = "base") } } - if (graph_learner) { - gr = mlr3pipelines::GraphLearner$new(gr) - } - - gr + create_grlrn(gr, graph_learner) } #' @template pipeline @@ -615,29 +579,24 @@ pipeline_survtoclassif_disctime = function(learner, cut = NULL, max_time = NULL, assert_learner(learner, task_type = "classif") assert_true("prob" %in% learner$predict_types) - gr = mlr3pipelines::Graph$new() - gr$add_pipeop(mlr3pipelines::po("trafotask_survclassif_disctime", cut = cut, max_time = max_time)) - gr$add_pipeop(mlr3pipelines::po("learner", learner, predict_type = "prob")) - gr$add_pipeop(mlr3pipelines::po("nop")) - gr$add_pipeop(mlr3pipelines::po("trafopred_classifsurv_disctime")) - - gr$add_edge(src_id = "trafotask_survclassif_disctime", dst_id = learner$id, src_channel = "output", dst_channel = "input") - gr$add_edge(src_id = "trafotask_survclassif_disctime", dst_id = "nop", src_channel = "transformed_data", dst_channel = "input") - gr$add_edge(src_id = learner$id, dst_id = "trafopred_classifsurv_disctime", src_channel = "output", dst_channel = "input") - gr$add_edge(src_id = "nop", dst_id = "trafopred_classifsurv_disctime", src_channel = "output", dst_channel = "transformed_data") + gr = Graph$new()$ + add_pipeop(po("trafotask_survclassif_disctime", cut = cut, max_time = max_time))$ + add_pipeop(po("learner", learner, predict_type = "prob"))$ + add_pipeop(po("nop"))$ + add_pipeop(po("trafopred_classifsurv_disctime"))$ + add_edge(src_id = "trafotask_survclassif_disctime", dst_id = learner$id, src_channel = "output", dst_channel = "input")$ + add_edge(src_id = "trafotask_survclassif_disctime", dst_id = "nop", src_channel = "transformed_data", dst_channel = "input")$ + add_edge(src_id = learner$id, dst_id = "trafopred_classifsurv_disctime", src_channel = "output", dst_channel = "input")$ + add_edge(src_id = "nop", dst_id = "trafopred_classifsurv_disctime", src_channel = "output", dst_channel = "transformed_data") if (!is.null(rhs)) { gr$edges = gr$edges[-1, ] - gr$add_pipeop(mlr3pipelines::po("modelmatrix", formula = formulate(rhs = rhs, quote = "left"))) - gr$add_edge(src_id = "trafotask_survclassif_disctime", dst_id = "modelmatrix", src_channel = "output") - gr$add_edge(src_id = "modelmatrix", dst_id = learner$id, src_channel = "output", dst_channel = "input") - } - - if (graph_learner) { - gr = mlr3pipelines::GraphLearner$new(gr) + gr$add_pipeop(po("modelmatrix", formula = formulate(rhs = rhs, quote = "left")))$ + add_edge(src_id = "trafotask_survclassif_disctime", dst_id = "modelmatrix", src_channel = "output")$ + add_edge(src_id = "modelmatrix", dst_id = learner$id, src_channel = "output", dst_channel = "input") } - gr + create_grlrn(gr, graph_learner) } #' @template pipeline @@ -698,22 +657,17 @@ pipeline_survtoclassif_IPCW = function(learner, tau = NULL, eps = 1e-3, graph_le assert_learner(learner, task_type = "classif") assert_true("prob" %in% learner$predict_types) - gr = mlr3pipelines::Graph$new() - gr$add_pipeop(mlr3pipelines::po("trafotask_survclassif_IPCW", tau = tau, eps = eps)) - gr$add_pipeop(mlr3pipelines::po("learner", learner, predict_type = "prob")) - gr$add_pipeop(mlr3pipelines::po("trafopred_classifsurv_IPCW")) - gr$add_pipeop(mlr3pipelines::po("nop")) - - gr$add_edge(src_id = "trafotask_survclassif_IPCW", dst_id = learner$id, src_channel = "output", dst_channel = "input") - gr$add_edge(src_id = learner$id, dst_id = "trafopred_classifsurv_IPCW", src_channel = "output", dst_channel = "input") - gr$add_edge(src_id = "trafotask_survclassif_IPCW", dst_id = "nop", src_channel = "data", dst_channel = "input") - gr$add_edge(src_id = "nop", dst_id = "trafopred_classifsurv_IPCW", src_channel = "output", dst_channel = "data") - - if (graph_learner) { - gr = mlr3pipelines::GraphLearner$new(gr) - } - - gr + gr = Graph$new()$ + add_pipeop(po("trafotask_survclassif_IPCW", tau = tau, eps = eps))$ + add_pipeop(po("learner", learner, predict_type = "prob"))$ + add_pipeop(po("trafopred_classifsurv_IPCW"))$ + add_pipeop(po("nop"))$ + add_edge(src_id = "trafotask_survclassif_IPCW", dst_id = learner$id, src_channel = "output", dst_channel = "input")$ + add_edge(src_id = learner$id, dst_id = "trafopred_classifsurv_IPCW", src_channel = "output", dst_channel = "input")$ + add_edge(src_id = "trafotask_survclassif_IPCW", dst_id = "nop", src_channel = "data", dst_channel = "input")$ + add_edge(src_id = "nop", dst_id = "trafopred_classifsurv_IPCW", src_channel = "output", dst_channel = "data") + + create_grlrn(gr, graph_learner) } register_graph("survaverager", pipeline_survaverager) From 0da65225ebf58bf80088742367c0ea39621624a7 Mon Sep 17 00:00:00 2001 From: john Date: Mon, 21 Oct 2024 22:57:57 +0200 Subject: [PATCH 06/18] update pipeline examples --- man/mlr_graphs_crankcompositor.Rd | 4 ++-- man/mlr_graphs_distrcompositor.Rd | 2 +- man/mlr_graphs_probregr.Rd | 4 ++-- man/mlr_graphs_responsecompositor.Rd | 4 ++-- man/mlr_graphs_survaverager.Rd | 4 ++-- man/mlr_graphs_survbagging.Rd | 4 ++-- man/mlr_graphs_survtoregr.Rd | 4 ++-- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/man/mlr_graphs_crankcompositor.Rd b/man/mlr_graphs_crankcompositor.Rd index 4af2d53e9..5467ee1bc 100644 --- a/man/mlr_graphs_crankcompositor.Rd +++ b/man/mlr_graphs_crankcompositor.Rd @@ -50,8 +50,8 @@ ppl("crankcompositor") \examples{ \dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ - library("mlr3") - library("mlr3pipelines") + library(mlr3) + library(mlr3pipelines) task = tsk("lung") part = partition(task) diff --git a/man/mlr_graphs_distrcompositor.Rd b/man/mlr_graphs_distrcompositor.Rd index 1722cf7a4..1463d6535 100644 --- a/man/mlr_graphs_distrcompositor.Rd +++ b/man/mlr_graphs_distrcompositor.Rd @@ -61,7 +61,7 @@ ppl("distrcompositor") \examples{ \dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ - library("mlr3pipelines") + library(mlr3pipelines) # let's change the distribution prediction of Cox (Breslow-based) to an AFT form: task = tsk("rats") diff --git a/man/mlr_graphs_probregr.Rd b/man/mlr_graphs_probregr.Rd index 66668c89b..12876c92e 100644 --- a/man/mlr_graphs_probregr.Rd +++ b/man/mlr_graphs_probregr.Rd @@ -51,8 +51,8 @@ ppl("probregr") \examples{ \dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines", "rpart"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ - library("mlr3") - library("mlr3pipelines") + library(mlr3) + library(mlr3pipelines) task = tsk("boston_housing") diff --git a/man/mlr_graphs_responsecompositor.Rd b/man/mlr_graphs_responsecompositor.Rd index 5f34c67d0..a96a14fdc 100644 --- a/man/mlr_graphs_responsecompositor.Rd +++ b/man/mlr_graphs_responsecompositor.Rd @@ -59,8 +59,8 @@ ppl("responsecompositor") \examples{ \dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ - library("mlr3") - library("mlr3pipelines") + library(mlr3) + library(mlr3pipelines) task = tsk("lung") part = partition(task) diff --git a/man/mlr_graphs_survaverager.Rd b/man/mlr_graphs_survaverager.Rd index e706e5928..f02a424d6 100644 --- a/man/mlr_graphs_survaverager.Rd +++ b/man/mlr_graphs_survaverager.Rd @@ -37,8 +37,8 @@ ppl("survaverager") \examples{ \dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ - library("mlr3") - library("mlr3pipelines") + library(mlr3) + library(mlr3pipelines) task = tsk("rats") pipe = ppl( diff --git a/man/mlr_graphs_survbagging.Rd b/man/mlr_graphs_survbagging.Rd index 4d01ee60c..0fe191b36 100644 --- a/man/mlr_graphs_survbagging.Rd +++ b/man/mlr_graphs_survbagging.Rd @@ -66,8 +66,8 @@ ppl("survbagging") \examples{ \dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ - library("mlr3") - library("mlr3pipelines") + library(mlr3) + library(mlr3pipelines) task = tsk("rats") pipe = ppl( diff --git a/man/mlr_graphs_survtoregr.Rd b/man/mlr_graphs_survtoregr.Rd index 5202d21e2..30a23285f 100644 --- a/man/mlr_graphs_survtoregr.Rd +++ b/man/mlr_graphs_survtoregr.Rd @@ -120,8 +120,8 @@ predictors of the new data, which can ultimately be composed to a distribution. \examples{ \dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ - library("mlr3") - library("mlr3pipelines") + library(mlr3) + library(mlr3pipelines) task = tsk("rats") From cae5f507840abff00fb89914f0cb5ae8a9043feb Mon Sep 17 00:00:00 2001 From: john Date: Mon, 21 Oct 2024 23:10:38 +0200 Subject: [PATCH 07/18] add alias pipeline check --- tests/testthat/test_unload.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test_unload.R b/tests/testthat/test_unload.R index 90b3edbed..2ca6fdd47 100644 --- a/tests/testthat/test_unload.R +++ b/tests/testthat/test_unload.R @@ -34,7 +34,7 @@ test_that("unloading leaves no trace", { # compose prediction types "crankcompositor", "distrcompositor", "responsecompositor", # transform surv to other tasks - "survtoregr", "survtoclassif_disctime", "survtoclassif_IPCW" + "survtoregr", "survtoclassif_disctime", "survtoclassif_IPCW", "survtoclassif_vock" ) expect_in(proba_graphs, mlr_graphs$keys()) From 231fe12b217bec8a1cbbd63807bd2c2fa0e0da71 Mon Sep 17 00:00:00 2001 From: john Date: Mon, 21 Oct 2024 23:32:18 +0200 Subject: [PATCH 08/18] refactor: pipeline tests --- tests/testthat/test_breslow.R | 10 ++-- tests/testthat/test_crankcompose.R | 10 ++-- tests/testthat/test_discretetime.R | 2 +- tests/testthat/test_distrcompose.R | 22 ++++---- tests/testthat/test_ipcw.R | 2 +- tests/testthat/test_pipelines.R | 72 ++++++++++++--------------- tests/testthat/test_responsecompose.R | 23 ++++----- tests/testthat/test_survavg.R | 18 +++---- tests/testthat/test_survtoregr.R | 22 ++++---- 9 files changed, 83 insertions(+), 98 deletions(-) diff --git a/tests/testthat/test_breslow.R b/tests/testthat/test_breslow.R index 23578a0a5..369c0ff76 100644 --- a/tests/testthat/test_breslow.R +++ b/tests/testthat/test_breslow.R @@ -127,19 +127,19 @@ test_that("breslow() works", { test_that("breslowcompose PipeOp works", { # learner is needed - expect_error(mlr3pipelines::po("breslowcompose"), "is missing") + expect_error(po("breslowcompose"), "is missing") # learner needs to be of survival type - expect_error(mlr3pipelines::po("breslowcompose", learner = lrn("classif.featureless")), + expect_error(po("breslowcompose", learner = lrn("classif.featureless")), "must have task type") # learner needs to have lp predictions - expect_error(mlr3pipelines::po("breslowcompose", learner = lrn("surv.kaplan")), + expect_error(po("breslowcompose", learner = lrn("surv.kaplan")), "must provide lp") # learner with lp predictions learner = lrn("surv.coxph") - b1 = mlr3pipelines::po("breslowcompose", learner = learner, breslow.overwrite = TRUE) - b2 = mlr3pipelines::po("breslowcompose", learner = learner) + b1 = po("breslowcompose", learner = learner, breslow.overwrite = TRUE) + b2 = po("breslowcompose", learner = learner) expect_pipeop(b1) expect_pipeop(b2) diff --git a/tests/testthat/test_crankcompose.R b/tests/testthat/test_crankcompose.R index e65a8bd94..a5818743e 100644 --- a/tests/testthat/test_crankcompose.R +++ b/tests/testthat/test_crankcompose.R @@ -23,17 +23,17 @@ test_that("no params", { test_that("overwrite crank", { # no overwrite - poc = mlr3pipelines::po("crankcompose") + poc = po("crankcompose") p1 = poc$predict(list(pcox))[[1L]] expect_identical(p1$crank, pcox$crank) # overwrite crank - poc = mlr3pipelines::po("crankcompose", param_vals = list(overwrite = TRUE)) + poc = po("crankcompose", param_vals = list(overwrite = TRUE)) p2 = poc$predict(list(pcox))[[1L]] expect_false(all(p2$crank == pcox$crank)) # even if prediction doesn't have crank somehow, pipeop will add it even if no overwrite - por = mlr3pipelines::po("crankcompose", param_vals = list(overwrite = FALSE)) + por = po("crankcompose", param_vals = list(overwrite = FALSE)) p2$data$crank = NULL expect_true(all(is.na(p2$crank))) # NAs produce so that c-index can be calculated p3 = poc$predict(list(p2))[[1L]] @@ -41,10 +41,10 @@ test_that("overwrite crank", { }) test_that("pipeline works", { - pipe = mlr3pipelines::ppl("crankcompositor", learner = lrn("surv.kaplan")) + pipe = ppl("crankcompositor", learner = lrn("surv.kaplan")) expect_class(pipe, "Graph") - grlrn = mlr3pipelines::ppl("crankcompositor", learner = lrn("surv.kaplan"), graph_learner = TRUE) + grlrn = ppl("crankcompositor", learner = lrn("surv.kaplan"), graph_learner = TRUE) expect_class(grlrn, "GraphLearner") p = grlrn$train(task)$predict(task) expect_prediction_surv(p) diff --git a/tests/testthat/test_discretetime.R b/tests/testthat/test_discretetime.R index 9723bd53d..62795bf6f 100644 --- a/tests/testthat/test_discretetime.R +++ b/tests/testthat/test_discretetime.R @@ -9,7 +9,7 @@ test_that("PipeOpTaskSurvClassifDiscTime", { expect_equal(test_task$row_ids, test_ids) expect_equal(train_task$row_ids, train_ids) - po_disc = mlr3pipelines::po("trafotask_survclassif_disctime", cut = 4) + po_disc = po("trafotask_survclassif_disctime", cut = 4) expect_class(po_disc, c("PipeOp", "PipeOpTaskSurvClassifDiscTime")) res = po_disc$train(list(train_task)) diff --git a/tests/testthat/test_distrcompose.R b/tests/testthat/test_distrcompose.R index 64257aa15..a2aadc4c0 100644 --- a/tests/testthat/test_distrcompose.R +++ b/tests/testthat/test_distrcompose.R @@ -14,12 +14,12 @@ cox_pred = lrn("surv.coxph")$train(task)$predict(task) test_that("no params", { base = lrn("surv.kaplan")$train(task)$predict(task) pred = lrn("surv.kaplan")$train(task)$predict(task) - pod = mlr3pipelines::po("distrcompose", param_vals = list()) + pod = po("distrcompose", param_vals = list()) expect_silent(pod$predict(list(base = base, pred = pred))) }) test_that("overwrite = FALSE", { - gr = mlr3pipelines::ppl("distrcompositor", lrn("surv.kaplan"), overwrite = FALSE) + gr = ppl("distrcompositor", lrn("surv.kaplan"), overwrite = FALSE) expect_class(gr, "Graph") expect_silent(gr$train(task)) expect_identical( @@ -28,41 +28,41 @@ test_that("overwrite = FALSE", { ) # breslow - gr = mlr3pipelines::ppl("distrcompositor", lrn("surv.coxph"), + gr = ppl("distrcompositor", lrn("surv.coxph"), estimator = "breslow", overwrite = FALSE) expect_silent(gr$train(task)) expect_identical(gr$predict(task)[[1L]]$data$distr, cox_pred$data$distr) }) test_that("overwrite = TRUE", { - gr = mlr3pipelines::ppl("distrcompositor", lrn("surv.kaplan"), overwrite = TRUE, form = "ph") + gr = ppl("distrcompositor", lrn("surv.kaplan"), overwrite = TRUE, form = "ph") expect_class(gr, "Graph") expect_silent(gr$train(task)) p = gr$predict(task)[[1L]] expect_prediction_surv(p) expect_true("distr" %in% p$predict_types) - grlrn = mlr3pipelines::ppl("distrcompositor", learner = lrn("surv.coxph"), - overwrite = TRUE, form = "po", graph_learner = TRUE) + grlrn = ppl("distrcompositor", learner = lrn("surv.coxph"), + overwrite = TRUE, form = "po", graph_learner = TRUE) expect_class(grlrn, "GraphLearner") p = grlrn$train(task)$predict(task) expect_prediction_surv(p) expect_false(all(p$data$distr == cox_pred$data$distr)) # PO distr != Cox's Breslow default - grlrn = mlr3pipelines::ppl("distrcompositor", learner = lrn("surv.coxph"), - overwrite = TRUE, form = "aft", graph_learner = TRUE) + grlrn = ppl("distrcompositor", learner = lrn("surv.coxph"), + overwrite = TRUE, form = "aft", graph_learner = TRUE) p = grlrn$train(task)$predict(task) expect_false(all(p$data$distr == cox_pred$data$distr)) # AFT distr != Cox's Breslow default # our breslow seems different from Cox's breslow - gr = mlr3pipelines::ppl("distrcompositor", learner = lrn("surv.coxph"), - estimator = "breslow", overwrite = TRUE, graph_learner = TRUE) + gr = ppl("distrcompositor", learner = lrn("surv.coxph"), + estimator = "breslow", overwrite = TRUE, graph_learner = TRUE) p = grlrn$train(task)$predict(task) expect_false(all(p$data$distr == cox_pred$data$distr)) # distr predictions changed (a bit) }) test_that("composition from crank doesn't work", { - grlrn = mlr3pipelines::ppl("distrcompositor", learner = lrn("surv.rpart"), graph_learner = TRUE) + grlrn = ppl("distrcompositor", learner = lrn("surv.rpart"), graph_learner = TRUE) p = grlrn$train(task)$predict(task) # rpart has only crank prediction, so no distr composition can be made expect_false("distr" %in% p$predict_types) diff --git a/tests/testthat/test_ipcw.R b/tests/testthat/test_ipcw.R index 497c30714..87ef407ab 100644 --- a/tests/testthat/test_ipcw.R +++ b/tests/testthat/test_ipcw.R @@ -9,7 +9,7 @@ test_that("PipeOpTaskSurvClassifIPCW", { expect_equal(test_task$row_ids, test_ids) expect_equal(train_task$row_ids, train_ids) - po_ipcw = mlr3pipelines::po("trafotask_survclassif_IPCW") + po_ipcw = po("trafotask_survclassif_IPCW") expect_class(po_ipcw, c("PipeOp", "PipeOpTaskSurvClassifIPCW")) # don't allow NULL `tau` expect_error(po_ipcw$train(list(train_task)), "not 'NULL'") diff --git a/tests/testthat/test_pipelines.R b/tests/testthat/test_pipelines.R index 4dae18b89..d8b264db1 100644 --- a/tests/testthat/test_pipelines.R +++ b/tests/testthat/test_pipelines.R @@ -2,37 +2,32 @@ task = tsk("rats")$filter(sample(300, 50L)) task_regr = tgen("friedman1")$generate(20L) test_that("survaverager", { - pipe = mlr3pipelines::ppl("survaverager", learners = list(lrn("surv.kaplan"), - lrn("surv.kaplan", id = "k2"))) + learners = list(lrn("surv.kaplan"), lrn("surv.kaplan", id = "k2")) + pipe = ppl("survaverager", learners = learners) expect_class(pipe, "Graph") - pipe = mlr3pipelines::ppl("survaverager", learners = list(lrn("surv.kaplan"), - lrn("surv.kaplan", id = "k2")), - graph_learner = TRUE) - expect_class(pipe, "GraphLearner") + grlrn = ppl("survaverager", learners = learners, graph_learner = TRUE) + expect_class(grlrn, "GraphLearner") - expect_prediction_surv(pipe$train(task)$predict(task)) + expect_prediction_surv(grlrn$train(task)$predict(task)) }) test_that("survbagging", { - pipe = mlr3pipelines::ppl("survbagging", lrn("surv.kaplan"), iterations = 2) + pipe = ppl("survbagging", lrn("surv.kaplan"), iterations = 2) expect_class(pipe, "Graph") - pipe = mlr3pipelines::ppl("survbagging", lrn("surv.kaplan"), iterations = 2, graph_learner = TRUE) - expect_class(pipe, "GraphLearner") - pipe$train(task) - p = pipe$predict(task) + grlrn = ppl("survbagging", lrn("surv.kaplan"), iterations = 2, graph_learner = TRUE) + expect_class(grlrn, "GraphLearner") + p = grlrn$train(task)$predict(task) expect_prediction_surv(p) }) -skip_if_not_installed("mlr3learners") - test_that("survtoclassif_disctime", { - requireNamespace("mlr3learners") + skip_if_not_installed("mlr3learners") - pipe = mlr3pipelines::ppl("survtoclassif_disctime", learner = lrn("classif.log_reg")) + pipe = ppl("survtoclassif_disctime", learner = lrn("classif.log_reg")) expect_class(pipe, "Graph") - grlrn = mlr3pipelines::ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), - graph_learner = TRUE) + grlrn = ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), + graph_learner = TRUE) expect_class(grlrn, "GraphLearner") grlrn$train(task) p = grlrn$predict(task) @@ -48,8 +43,8 @@ test_that("survtoclassif_disctime", { expect_equal(p$score(), p2$score(), tolerance = 0.015) # Test with cut - grlrn = mlr3pipelines::ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), - cut = c(10, 30, 50), graph_learner = TRUE) + grlrn = ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), + cut = c(10, 30, 50), graph_learner = TRUE) expect_class(grlrn, "GraphLearner") suppressWarnings(grlrn$train(task)) p = grlrn$predict(task) @@ -58,24 +53,24 @@ test_that("survtoclassif_disctime", { # `max_time` needs to be larger than the minimum event time so we choose # the minimum event time in the data for testing max_time = task$data()[status == 1, min(time)] - grlrn = mlr3pipelines::ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), - max_time = max_time, graph_learner = TRUE) + grlrn = ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), + max_time = max_time, graph_learner = TRUE) expect_error(grlrn$train(task)) - grlrn = mlr3pipelines::ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), - max_time = max_time + 1, graph_learner = TRUE) + grlrn = ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), + max_time = max_time + 1, graph_learner = TRUE) suppressWarnings(grlrn$train(task)) p = grlrn$predict(task) expect_prediction_surv(p) # Test with rhs - grlrn = mlr3pipelines::ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), - rhs = "1", graph_learner = TRUE) + grlrn = ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), + rhs = "1", graph_learner = TRUE) grlrn$train(task) pred = suppressWarnings(grlrn$predict(task)) - grlrn2 = mlr3pipelines::ppl("survtoclassif_disctime", learner = lrn("classif.featureless"), - graph_learner = TRUE) + grlrn2 = ppl("survtoclassif_disctime", learner = lrn("classif.featureless"), + graph_learner = TRUE) grlrn2$train(task) pred2 = grlrn2$predict(task) @@ -84,13 +79,13 @@ test_that("survtoclassif_disctime", { expect_equal(unname(pred2$score()), 0.5) expect_equal(pred$data$distr, pred2$data$distr) - grlrn = mlr3pipelines::ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), - rhs = "rx + litter", graph_learner = TRUE) + grlrn = ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), + rhs = "rx + litter", graph_learner = TRUE) grlrn$train(task) pred = suppressWarnings(grlrn$predict(task)) - grlrn2 = mlr3pipelines::ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), - rhs = ".", graph_learner = TRUE) + grlrn2 = ppl("survtoclassif_disctime", learner = lrn("classif.log_reg"), + rhs = ".", graph_learner = TRUE) grlrn2$train(task) pred2 = suppressWarnings(grlrn2$predict(task)) @@ -98,18 +93,15 @@ test_that("survtoclassif_disctime", { expect_gt(pred2$score(), pred$score()) }) -skip_if_not_installed("mlr3extralearners") - test_that("survtoclassif_IPCW", { task = tsk("lung") part = partition(task) - pipe = mlr3pipelines::ppl("survtoclassif_IPCW", learner = lrn("classif.rpart"), - tau = 500) + pipe = ppl("survtoclassif_IPCW", learner = lrn("classif.rpart"), tau = 500) expect_class(pipe, "Graph") - grlrn = mlr3pipelines::ppl("survtoclassif_IPCW", learner = lrn("classif.rpart"), - tau = 500, graph_learner = TRUE) + grlrn = ppl("survtoclassif_IPCW", learner = lrn("classif.rpart"), tau = 500, + graph_learner = TRUE) expect_class(grlrn, "GraphLearner") grlrn$train(task, row_ids = part$train) # check that the weights were used for classif learner @@ -128,8 +120,8 @@ test_that("survtoclassif_IPCW", { expect_number(p$score(msr("surv.brier", times = 500, integrated = FALSE)), finite = TRUE) # Test with different tau - grlrn = mlr3pipelines::ppl("survtoclassif_IPCW", learner = lrn("classif.rpart"), - tau = 600, graph_learner = TRUE) + grlrn = ppl("survtoclassif_IPCW", learner = lrn("classif.rpart"), tau = 600, + graph_learner = TRUE) grlrn$train(task, part$train) p2 = grlrn$predict(task, part$test) diff --git a/tests/testthat/test_responsecompose.R b/tests/testthat/test_responsecompose.R index 33d8cf869..3c9b62aec 100644 --- a/tests/testthat/test_responsecompose.R +++ b/tests/testthat/test_responsecompose.R @@ -21,17 +21,17 @@ pcox$data$response = rexp(20) # hack: add survival time predictions to cox model test_that("overwrite", { # no overwrite - por = mlr3pipelines::po("responsecompose") + por = po("responsecompose") p1 = por$predict(list(pcox))[[1L]] expect_identical(p1$response, pcox$response) # overwrite response - por = mlr3pipelines::po("responsecompose", overwrite = TRUE) + por = po("responsecompose", overwrite = TRUE) p2 = por$predict(list(pcox))[[1L]] expect_false(all(p2$response == pcox$response)) # even if prediction doesn't have response, pipeop will add them even if no overwrite - por = mlr3pipelines::po("responsecompose") + por = po("responsecompose") pkm = lrn("surv.kaplan")$train(task)$predict(task) expect_null(pkm$response) p3 = por$predict(list(pkm))[[1L]] @@ -40,7 +40,7 @@ test_that("overwrite", { }) test_that("different methods, different responses", { - por = mlr3pipelines::po("responsecompose", overwrite = TRUE, method = "rmst") + por = po("responsecompose", overwrite = TRUE, method = "rmst") p1 = por$predict(list(pcox))[[1L]] por$param_set$set_values(method = "median") p2 = por$predict(list(pcox))[[1L]] @@ -48,13 +48,10 @@ test_that("different methods, different responses", { }) test_that("different cutoffs, different rmst", { - por1 = mlr3pipelines::po("responsecompose", overwrite = TRUE, method = "rmst") - por2 = mlr3pipelines::po("responsecompose", overwrite = TRUE, method = "rmst", - tau = 100) # t_max = 99 in the generated data - por3 = mlr3pipelines::po("responsecompose", overwrite = TRUE, method = "rmst", - tau = 65) - por4 = mlr3pipelines::po("responsecompose", overwrite = TRUE, method = "rmst", - tau = 25) + por1 = po("responsecompose", overwrite = TRUE, method = "rmst") + por2 = po("responsecompose", overwrite = TRUE, method = "rmst", tau = 100) # t_max = 99 in the generated data + por3 = po("responsecompose", overwrite = TRUE, method = "rmst", tau = 65) + por4 = po("responsecompose", overwrite = TRUE, method = "rmst", tau = 25) p1 = por1$predict(list(pcox))[[1L]] p2 = por2$predict(list(pcox))[[1L]] p3 = por3$predict(list(pcox))[[1L]] @@ -68,12 +65,12 @@ test_that("different cutoffs, different rmst", { }) test_that("crank is added", { - por = mlr3pipelines::po("responsecompose", overwrite = FALSE, add_crank = TRUE) + por = po("responsecompose", overwrite = FALSE, add_crank = TRUE) p1 = por$predict(list(pcox))[[1L]] # same crank or response expect_identical(p1$response, pcox$response) - por = mlr3pipelines::po("responsecompose", overwrite = TRUE, add_crank = TRUE) + por = po("responsecompose", overwrite = TRUE, add_crank = TRUE) p2 = por$predict(list(pcox))[[1L]] # response changed expect_false(all(pcox$response == p2$response)) diff --git a/tests/testthat/test_survavg.R b/tests/testthat/test_survavg.R index 765f1c0d6..78d7fbef8 100644 --- a/tests/testthat/test_survavg.R +++ b/tests/testthat/test_survavg.R @@ -8,7 +8,7 @@ p1 = lrn("surv.kaplan")$train(task)$predict(task) p2 = lrn("surv.coxph")$train(task)$predict(task) test_that("equal weights", { - poc = mlr3pipelines::po("survavg") + poc = po("survavg") expect_silent({ p = poc$predict(list(p1, p2))[[1L]] }) @@ -22,7 +22,7 @@ test_that("equal weights", { }) test_that("unequal weights", { - poc = mlr3pipelines::po("survavg", param_vals = list(weights = c(0.2, 0.8))) + poc = po("survavg", param_vals = list(weights = c(0.2, 0.8))) expect_silent({ p = poc$predict(list(p1, p2))$output }) @@ -34,7 +34,7 @@ test_that("unequal weights", { }) test_that("lp", { - poc = mlr3pipelines::po("survavg") + poc = po("survavg") expect_silent({ p = poc$predict(list(p2, p2))[[1L]] }) @@ -42,9 +42,8 @@ test_that("lp", { }) test_that("response", { - poc = mlr3pipelines::po("survavg") - por = mlr3pipelines::ppl("responsecompositor", learner = lrn("surv.coxph"), - graph_learner = TRUE) + poc = po("survavg") + por = ppl("responsecompositor", learner = lrn("surv.coxph"), graph_learner = TRUE) p3 = por$train(task)$predict(task) expect_silent({ p = poc$predict(list(p3, p3))[[1L]] @@ -53,12 +52,11 @@ test_that("response", { }) test_that("pipeline surv_averager", { - poc = mlr3pipelines::po("survavg", weights = c(0.2, 0.8)) + poc = po("survavg", weights = c(0.2, 0.8)) p = poc$predict(list(p1, p2))[[1L]] - p2 = mlr3pipelines::ppl("survaverager", - learners = list(lrn("surv.kaplan"), lrn("surv.coxph")), - list(weights = c(0.2, 0.8)), graph_learner = TRUE)$ + p2 = ppl("survaverager", learners = list(lrn("surv.kaplan"), lrn("surv.coxph")), + list(weights = c(0.2, 0.8)), graph_learner = TRUE)$ train(task)$predict(task) expect_equal(p$crank, p2$crank) diff --git a/tests/testthat/test_survtoregr.R b/tests/testthat/test_survtoregr.R index 74ad389c9..67443182f 100644 --- a/tests/testthat/test_survtoregr.R +++ b/tests/testthat/test_survtoregr.R @@ -1,14 +1,14 @@ skip("Due to bugs in survtoregr methods") test_that("resample survtoregr", { - grlrn = mlr3pipelines::ppl("survtoregr", method = 1, distrcompose = FALSE, graph_learner = TRUE) + grlrn = ppl("survtoregr", method = 1, distrcompose = FALSE, graph_learner = TRUE) rr = resample(task, grlrn, rsmp("cv", folds = 2L)) expect_numeric(rr$aggregate()) }) test_that("survtoregr 1", { - pipe = mlr3pipelines::ppl("survtoregr", method = 1) + pipe = ppl("survtoregr", method = 1) expect_class(pipe, "Graph") - grlrn = mlr3pipelines::ppl("survtoregr", method = 1, graph_learner = TRUE) + grlrn = ppl("survtoregr", method = 1, graph_learner = TRUE) expect_class(grlrn, "GraphLearner") p = grlrn$train(task)$predict(task) expect_prediction_surv(p) @@ -16,17 +16,17 @@ test_that("survtoregr 1", { }) test_that("survtoregr 2", { - pipe = mlr3pipelines::ppl("survtoregr", method = 2) + pipe = ppl("survtoregr", method = 2) expect_class(pipe, "Graph") - pipe = mlr3pipelines::ppl("survtoregr", method = 2, graph_learner = TRUE) + pipe = ppl("survtoregr", method = 2, graph_learner = TRUE) expect_class(pipe, "GraphLearner") pipe$train(task) p = pipe$predict(task) expect_prediction_surv(p) expect_true("distr" %in% p$predict_types) - pipe = mlr3pipelines::ppl("survtoregr", method = 2, regr_se_learner = lrn("regr.featureless"), - graph_learner = TRUE) + pipe = ppl("survtoregr", method = 2, regr_se_learner = lrn("regr.featureless"), + graph_learner = TRUE) expect_class(pipe, "GraphLearner") pipe$train(task) p = pipe$predict(task) @@ -35,17 +35,15 @@ test_that("survtoregr 2", { }) test_that("survtoregr 3", { - pipe = mlr3pipelines::ppl("survtoregr", method = 3, distrcompose = FALSE) + pipe = ppl("survtoregr", method = 3, distrcompose = FALSE) expect_class(pipe, "Graph") - pipe = mlr3pipelines::ppl("survtoregr", method = 3, distrcompose = FALSE, - graph_learner = TRUE) + pipe = ppl("survtoregr", method = 3, distrcompose = FALSE, graph_learner = TRUE) expect_class(pipe, "GraphLearner") suppressWarnings(pipe$train(task)) # suppress loglik warning p = pipe$predict(task) expect_prediction_surv(p) - pipe = mlr3pipelines::ppl("survtoregr", method = 3, distrcompose = TRUE, - graph_learner = TRUE) + pipe = ppl("survtoregr", method = 3, distrcompose = TRUE, graph_learner = TRUE) expect_class(pipe, "GraphLearner") suppressWarnings(pipe$train(task)) # suppress loglik warning p = pipe$predict(task) From e8c922ec243ee2a1eb690ebb47f9ac0aaf2b64ae Mon Sep 17 00:00:00 2001 From: john Date: Mon, 21 Oct 2024 23:57:13 +0200 Subject: [PATCH 09/18] refactor R6Class --- R/LearnerDens.R | 2 +- R/LearnerDensHistogram.R | 2 +- R/LearnerDensKDE.R | 2 +- R/MeasureDensLogloss.R | 2 +- R/MeasureRegrLogloss.R | 2 +- R/MeasureSurvGraf.R | 2 +- R/MeasureSurvIntLogloss.R | 2 +- R/MeasureSurvLogloss.R | 2 +- R/MeasureSurvMAE.R | 2 +- R/MeasureSurvMSE.R | 2 +- R/MeasureSurvRCLL.R | 2 +- R/MeasureSurvRMSE.R | 2 +- R/MeasureSurvSchmid.R | 2 +- R/TaskDens.R | 2 +- R/TaskGeneratorCoxed.R | 2 +- R/TaskGeneratorSimdens.R | 2 +- R/TaskSurv.R | 2 +- 17 files changed, 17 insertions(+), 17 deletions(-) diff --git a/R/LearnerDens.R b/R/LearnerDens.R index 6018e6896..fb470929a 100644 --- a/R/LearnerDens.R +++ b/R/LearnerDens.R @@ -30,7 +30,7 @@ #' # get a specific learner from mlr_learners: #' mlr_learners$get("dens.hist") #' lrn("dens.hist") -LearnerDens = R6::R6Class("LearnerDens", +LearnerDens = R6Class("LearnerDens", inherit = Learner, public = list( #' @description Creates a new instance of this [R6][R6::R6Class] class. diff --git a/R/LearnerDensHistogram.R b/R/LearnerDensHistogram.R index 5ed1bd514..21a116579 100644 --- a/R/LearnerDensHistogram.R +++ b/R/LearnerDensHistogram.R @@ -4,7 +4,7 @@ #' @templateVar caller [graphics::hist()] #' #' @export -LearnerDensHistogram = R6::R6Class("LearnerDensHistogram", +LearnerDensHistogram = R6Class("LearnerDensHistogram", inherit = LearnerDens, public = list( #' @description diff --git a/R/LearnerDensKDE.R b/R/LearnerDensKDE.R index 0094a4cf8..1bc962869 100644 --- a/R/LearnerDensKDE.R +++ b/R/LearnerDensKDE.R @@ -11,7 +11,7 @@ #' `r format_bib("silverman_1986")` #' #' @export -LearnerDensKDE = R6::R6Class("LearnerDensKDE", +LearnerDensKDE = R6Class("LearnerDensKDE", inherit = LearnerDens, public = list( #' @description diff --git a/R/MeasureDensLogloss.R b/R/MeasureDensLogloss.R index 0c552843b..173d43849 100644 --- a/R/MeasureDensLogloss.R +++ b/R/MeasureDensLogloss.R @@ -15,7 +15,7 @@ #' #' @family Density estimation measures #' @export -MeasureDensLogloss = R6::R6Class("MeasureDensLogloss", +MeasureDensLogloss = R6Class("MeasureDensLogloss", inherit = MeasureDens, public = list( #' @description diff --git a/R/MeasureRegrLogloss.R b/R/MeasureRegrLogloss.R index c7bfbe280..e485cb26a 100644 --- a/R/MeasureRegrLogloss.R +++ b/R/MeasureRegrLogloss.R @@ -14,7 +14,7 @@ #' \deqn{L(f, y) = -\log(f(y))}{L(f, y) = -log(f(y))} #' #' @export -MeasureRegrLogloss = R6::R6Class("MeasureRegrLogloss", +MeasureRegrLogloss = R6Class("MeasureRegrLogloss", inherit = MeasureRegr, public = list( #' @description diff --git a/R/MeasureSurvGraf.R b/R/MeasureSurvGraf.R index 2ef60545b..c062b495e 100644 --- a/R/MeasureSurvGraf.R +++ b/R/MeasureSurvGraf.R @@ -53,7 +53,7 @@ #' @family Probabilistic survival measures #' @family distr survival measures #' @export -MeasureSurvGraf = R6::R6Class("MeasureSurvGraf", +MeasureSurvGraf = R6Class("MeasureSurvGraf", inherit = MeasureSurv, public = list( #' @description diff --git a/R/MeasureSurvIntLogloss.R b/R/MeasureSurvIntLogloss.R index 0b5ce26f2..3768f2a9c 100644 --- a/R/MeasureSurvIntLogloss.R +++ b/R/MeasureSurvIntLogloss.R @@ -51,7 +51,7 @@ #' @family Probabilistic survival measures #' @family distr survival measures #' @export -MeasureSurvIntLogloss = R6::R6Class("MeasureSurvIntLogloss", +MeasureSurvIntLogloss = R6Class("MeasureSurvIntLogloss", inherit = MeasureSurv, public = list( #' @description diff --git a/R/MeasureSurvLogloss.R b/R/MeasureSurvLogloss.R index 2e2d11ac8..ceb57dc68 100644 --- a/R/MeasureSurvLogloss.R +++ b/R/MeasureSurvLogloss.R @@ -36,7 +36,7 @@ #' @family Probabilistic survival measures #' @family distr survival measures #' @export -MeasureSurvLogloss = R6::R6Class("MeasureSurvLogloss", +MeasureSurvLogloss = R6Class("MeasureSurvLogloss", inherit = MeasureSurv, public = list( #' @description diff --git a/R/MeasureSurvMAE.R b/R/MeasureSurvMAE.R index 116d36caf..1f0d76238 100644 --- a/R/MeasureSurvMAE.R +++ b/R/MeasureSurvMAE.R @@ -15,7 +15,7 @@ #' #' @family response survival measures #' @export -MeasureSurvMAE = R6::R6Class("MeasureSurvMAE", +MeasureSurvMAE = R6Class("MeasureSurvMAE", inherit = MeasureSurv, public = list( #' @description Creates a new instance of this [R6][R6::R6Class] class. diff --git a/R/MeasureSurvMSE.R b/R/MeasureSurvMSE.R index 6c6a1e8e2..63819b7d3 100644 --- a/R/MeasureSurvMSE.R +++ b/R/MeasureSurvMSE.R @@ -15,7 +15,7 @@ #' #' @family response survival measures #' @export -MeasureSurvMSE = R6::R6Class("MeasureSurvMSE", +MeasureSurvMSE = R6Class("MeasureSurvMSE", inherit = MeasureSurv, public = list( #' @description Creates a new instance of this [R6][R6::R6Class] class. diff --git a/R/MeasureSurvRCLL.R b/R/MeasureSurvRCLL.R index cb961acdb..d7329094c 100644 --- a/R/MeasureSurvRCLL.R +++ b/R/MeasureSurvRCLL.R @@ -30,7 +30,7 @@ #' @family Probabilistic survival measures #' @family distr survival measures #' @export -MeasureSurvRCLL = R6::R6Class("MeasureSurvRCLL", +MeasureSurvRCLL = R6Class("MeasureSurvRCLL", inherit = MeasureSurv, public = list( #' @description diff --git a/R/MeasureSurvRMSE.R b/R/MeasureSurvRMSE.R index 427ca5b5d..2a46c3ab4 100644 --- a/R/MeasureSurvRMSE.R +++ b/R/MeasureSurvRMSE.R @@ -15,7 +15,7 @@ #' #' @family response survival measures #' @export -MeasureSurvRMSE = R6::R6Class("MeasureSurvRMSE", +MeasureSurvRMSE = R6Class("MeasureSurvRMSE", inherit = MeasureSurv, public = list( #' @description Creates a new instance of this [R6][R6::R6Class] class. diff --git a/R/MeasureSurvSchmid.R b/R/MeasureSurvSchmid.R index 1773dfd2f..22c574a91 100644 --- a/R/MeasureSurvSchmid.R +++ b/R/MeasureSurvSchmid.R @@ -57,7 +57,7 @@ #' @family Probabilistic survival measures #' @family distr survival measures #' @export -MeasureSurvSchmid = R6::R6Class("MeasureSurvSchmid", +MeasureSurvSchmid = R6Class("MeasureSurvSchmid", inherit = MeasureSurv, public = list( #' @description diff --git a/R/TaskDens.R b/R/TaskDens.R index 7452b53a5..a775d2cc1 100644 --- a/R/TaskDens.R +++ b/R/TaskDens.R @@ -17,7 +17,7 @@ #' @examples #' task = TaskDens$new("precip", backend = precip) #' task$task_type -TaskDens = R6::R6Class("TaskDens", +TaskDens = R6Class("TaskDens", inherit = TaskUnsupervised, public = list( #' @description diff --git a/R/TaskGeneratorCoxed.R b/R/TaskGeneratorCoxed.R index e25eb150b..1bdfdc672 100644 --- a/R/TaskGeneratorCoxed.R +++ b/R/TaskGeneratorCoxed.R @@ -32,7 +32,7 @@ #' gen$param_set$set_values(type = "tvc") #' gen$generate(50) #' } -TaskGeneratorCoxed = R6::R6Class("TaskGeneratorCoxed", +TaskGeneratorCoxed = R6Class("TaskGeneratorCoxed", inherit = TaskGenerator, public = list( #' @description Creates a new instance of this [R6][R6::R6Class] class. diff --git a/R/TaskGeneratorSimdens.R b/R/TaskGeneratorSimdens.R index d0b59d127..923a1051a 100644 --- a/R/TaskGeneratorSimdens.R +++ b/R/TaskGeneratorSimdens.R @@ -24,7 +24,7 @@ #' dens_gen = tgen("simdens", distribution = "Bernoulli", pars = list(prob = 0.8)) #' task = dens_gen$generate(50) #' task$data()[["x"]] -TaskGeneratorSimdens = R6::R6Class("TaskGeneratorSimdens", +TaskGeneratorSimdens = R6Class("TaskGeneratorSimdens", inherit = TaskGenerator, public = list( #' @description Creates a new instance of this [R6][R6::R6Class] class. diff --git a/R/TaskSurv.R b/R/TaskSurv.R index b595b9c3f..7fbcc034e 100644 --- a/R/TaskSurv.R +++ b/R/TaskSurv.R @@ -47,7 +47,7 @@ #' task$prop_haz() #' # veteran data is definitely non-PH (p << 0.05) #' tsk("veteran")$prop_haz() -TaskSurv = R6::R6Class("TaskSurv", +TaskSurv = R6Class("TaskSurv", inherit = TaskSupervised, public = list( #' @description From 27a83cd5f30dccd9bad978eca0bbdbd6bde2e50b Mon Sep 17 00:00:00 2001 From: john Date: Tue, 22 Oct 2024 00:32:32 +0200 Subject: [PATCH 10/18] refactor: use examplesIf --- R/PipeOpBreslow.R | 4 +--- R/PipeOpCrankCompositor.R | 4 +--- R/PipeOpDistrCompositor.R | 4 +--- R/PipeOpPredRegrSurv.R | 4 +--- R/PipeOpPredSurvRegr.R | 5 +---- R/PipeOpProbregrCompositor.R | 5 +---- R/PipeOpResponseCompositor.R | 4 +--- R/PipeOpSurvAvg.R | 4 +--- R/PipeOpTaskRegrSurv.R | 4 +--- R/PipeOpTaskSurvClassifDiscTime.R | 5 +---- R/PipeOpTaskSurvRegr.R | 4 +--- R/TaskGeneratorCoxed.R | 6 ++---- R/TaskGeneratorSimsurv.R | 6 ++---- R/TaskSurv.R | 2 +- R/as_prediction_dens.R | 2 -- R/autoplot.R | 12 ++++++------ R/pecs.R | 4 +--- man/autoplot.PredictionSurv.Rd | 2 ++ man/autoplot.TaskDens.Rd | 2 ++ man/autoplot.TaskSurv.Rd | 2 ++ man/mlr_pipeops_compose_breslow_distr.Rd | 4 ++-- man/mlr_pipeops_compose_probregr.Rd | 5 ++--- man/mlr_pipeops_crankcompose.Rd | 4 ++-- man/mlr_pipeops_distrcompose.Rd | 4 ++-- man/mlr_pipeops_responsecompose.Rd | 4 ++-- man/mlr_pipeops_survavg.Rd | 4 ++-- man/mlr_pipeops_trafopred_regrsurv.Rd | 4 ++-- man/mlr_pipeops_trafopred_survregr.Rd | 4 ++-- man/mlr_pipeops_trafotask_regrsurv.Rd | 4 ++-- man/mlr_pipeops_trafotask_survclassif_disctime.Rd | 6 ++---- man/mlr_pipeops_trafotask_survregr.Rd | 4 ++-- man/mlr_task_generators_coxed.Rd | 4 ++-- man/mlr_task_generators_simsurv.Rd | 4 ++-- man/pecs.Rd | 5 ++--- 34 files changed, 57 insertions(+), 88 deletions(-) diff --git a/R/PipeOpBreslow.R b/R/PipeOpBreslow.R index 63ec7a871..7a0ae6455 100644 --- a/R/PipeOpBreslow.R +++ b/R/PipeOpBreslow.R @@ -41,9 +41,8 @@ #' @seealso [pipeline_distrcompositor] #' @export #' @family survival compositors -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' if (requireNamespace("mlr3pipelines", quietly = TRUE)) { #' library(mlr3) #' library(mlr3pipelines) #' task = tsk("rats") @@ -57,7 +56,6 @@ #' b$train(list(train_task)) #' p = b$predict(list(test_task))[[1L]] #' } -#' } PipeOpBreslow = R6Class("PipeOpBreslow", inherit = mlr3pipelines::PipeOp, public = list( diff --git a/R/PipeOpCrankCompositor.R b/R/PipeOpCrankCompositor.R index 574318aab..ddf2eade3 100644 --- a/R/PipeOpCrankCompositor.R +++ b/R/PipeOpCrankCompositor.R @@ -36,9 +36,8 @@ #' #' @seealso [pipeline_crankcompositor] #' @family survival compositors -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' if (requireNamespace("mlr3pipelines", quietly = TRUE)) { #' library(mlr3pipelines) #' task = tsk("rats") #' @@ -47,7 +46,6 @@ #' poc = po("crankcompose", param_vals = list(overwrite = TRUE)) #' poc$predict(list(pred))[[1L]] #' } -#' } #' @export PipeOpCrankCompositor = R6Class("PipeOpCrankCompositor", inherit = mlr3pipelines::PipeOp, diff --git a/R/PipeOpDistrCompositor.R b/R/PipeOpDistrCompositor.R index 63a7da44b..3c1d1983e 100644 --- a/R/PipeOpDistrCompositor.R +++ b/R/PipeOpDistrCompositor.R @@ -58,9 +58,8 @@ #' @seealso [pipeline_distrcompositor] #' @export #' @family survival compositors -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' if (requireNamespace("mlr3pipelines", quietly = TRUE)) { #' library(mlr3) #' library(mlr3pipelines) #' task = tsk("rats") @@ -71,7 +70,6 @@ #' pod = po("distrcompose", param_vals = list(form = "aft", overwrite = TRUE)) #' pod$predict(list(base = base, pred = pred))[[1]] #' } -#' } PipeOpDistrCompositor = R6Class("PipeOpDistrCompositor", inherit = mlr3pipelines::PipeOp, public = list( diff --git a/R/PipeOpPredRegrSurv.R b/R/PipeOpPredRegrSurv.R index d8caaf351..a0d3798d9 100644 --- a/R/PipeOpPredRegrSurv.R +++ b/R/PipeOpPredRegrSurv.R @@ -21,9 +21,8 @@ #' If `NULL` then assumed no censoring in the dataset. Otherwise should be a vector of `0/1`s #' of same length as the prediction object, where `1` is dead and `0` censored. #' -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' if (requireNamespace("mlr3pipelines", quietly = TRUE)) { #' library(mlr3) #' library(mlr3pipelines) #' @@ -45,7 +44,6 @@ #' new_pred = po$predict(list(pred = pred, task = task_surv))[[1]] #' all.equal(new_pred$truth, task_surv$truth()) #' } -#' } #' @family PipeOps #' @family Transformation PipeOps #' @include PipeOpPredTransformer.R diff --git a/R/PipeOpPredSurvRegr.R b/R/PipeOpPredSurvRegr.R index ce01070fc..98b23447c 100644 --- a/R/PipeOpPredSurvRegr.R +++ b/R/PipeOpPredSurvRegr.R @@ -14,10 +14,8 @@ #' @section State: #' The `$state` is a named `list` with the `$state` elements inherited from [PipeOpPredTransformer]. #' -#' -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' if (requireNamespace("mlr3pipelines", quietly = TRUE)) { #' library(mlr3) #' library(mlr3pipelines) #' library(survival) @@ -29,7 +27,6 @@ #' new_pred = po$predict(list(pred = pred))[[1]] #' print(new_pred) #' } -#' } #' @family PipeOps #' @family Transformation PipeOps #' @include PipeOpPredTransformer.R diff --git a/R/PipeOpProbregrCompositor.R b/R/PipeOpProbregrCompositor.R index 5386432ac..eb1306108 100644 --- a/R/PipeOpProbregrCompositor.R +++ b/R/PipeOpProbregrCompositor.R @@ -38,10 +38,8 @@ #' distribution location and scale parameters respectively. #' #' @export -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines", "rpart"), quietly = TRUE) #' \dontrun{ -#' if (requireNamespace("mlr3pipelines", quietly = TRUE) && -#' requireNamespace("rpart", quietly = TRUE)) { #' library(mlr3) #' library(mlr3pipelines) #' set.seed(1) @@ -61,7 +59,6 @@ #' poc = po("compose_probregr") #' poc$predict(list(pred_response, pred_se))[[1]] #' } -#' } PipeOpProbregr = R6Class("PipeOpProbregr", inherit = mlr3pipelines::PipeOp, public = list( diff --git a/R/PipeOpResponseCompositor.R b/R/PipeOpResponseCompositor.R index f92a4e989..de278c2a4 100644 --- a/R/PipeOpResponseCompositor.R +++ b/R/PipeOpResponseCompositor.R @@ -58,9 +58,8 @@ #' #' @seealso [pipeline_crankcompositor] #' @family survival compositors -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' if (requireNamespace("mlr3pipelines", quietly = TRUE)) { #' library(mlr3pipelines) #' task = tsk("rats") #' @@ -76,7 +75,6 @@ #' por = po("responsecompose", param_vals = list(overwrite = TRUE, add_crank = TRUE)) #' por$predict(list(pred))[[1L]] #' } -#' } #' @export PipeOpResponseCompositor = R6Class("PipeOpResponseCompositor", inherit = mlr3pipelines::PipeOp, diff --git a/R/PipeOpSurvAvg.R b/R/PipeOpSurvAvg.R index 16718a17a..d9ac42263 100644 --- a/R/PipeOpSurvAvg.R +++ b/R/PipeOpSurvAvg.R @@ -33,9 +33,8 @@ #' @family PipeOps #' @family Ensembles #' @export -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' if (requireNamespace("mlr3pipelines", quietly = TRUE)) { #' library(mlr3) #' library(mlr3pipelines) #' @@ -45,7 +44,6 @@ #' poc = po("survavg", param_vals = list(weights = c(0.2, 0.8))) #' poc$predict(list(p1, p2)) #' } -#' } PipeOpSurvAvg = R6Class("PipeOpSurvAvg", inherit = mlr3pipelines::PipeOpEnsemble, diff --git a/R/PipeOpTaskRegrSurv.R b/R/PipeOpTaskRegrSurv.R index 647c21ed6..e258ae2b5 100644 --- a/R/PipeOpTaskRegrSurv.R +++ b/R/PipeOpTaskRegrSurv.R @@ -20,9 +20,8 @@ #' If `NULL` then assumed no censoring in the dataset. Otherwise should be a vector of `0/1`s #' of same length as the prediction object, where `1` is dead and `0` censored. #' -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' if (requireNamespace("mlr3pipelines", quietly = TRUE)) { #' library(mlr3) #' library(mlr3pipelines) #' @@ -41,7 +40,6 @@ #' new_task$truth() #' task_surv$truth() #' } -#' } #' @family PipeOps #' @family Transformation PipeOps #' @include PipeOpPredTransformer.R diff --git a/R/PipeOpTaskSurvClassifDiscTime.R b/R/PipeOpTaskSurvClassifDiscTime.R index 89f606516..ec8617797 100644 --- a/R/PipeOpTaskSurvClassifDiscTime.R +++ b/R/PipeOpTaskSurvClassifDiscTime.R @@ -57,10 +57,8 @@ #' All event times after `max_time` will be administratively censored at `max_time.` #' Needs to be greater than the minimum event time in the given task. #' -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines", "mlr3learners"), quietly = TRUE) #' \dontrun{ -#' if (requireNamespace("mlr3pipelines", quietly = TRUE) && -#' requireNamespace("mlr3learners", quietly = TRUE)) { #' library(mlr3) #' library(mlr3learners) #' library(mlr3pipelines) @@ -79,7 +77,6 @@ #' learner = lrn("classif.log_reg", predict_type = "prob") #' learner$train(task_classif) #' } -#' } #' #' @references #' `r format_bib("tutz_2016")` diff --git a/R/PipeOpTaskSurvRegr.R b/R/PipeOpTaskSurvRegr.R index 47cdf2173..19fe0ec0d 100644 --- a/R/PipeOpTaskSurvRegr.R +++ b/R/PipeOpTaskSurvRegr.R @@ -53,9 +53,8 @@ #' @references #' `r format_bib("buckley_1979", "klein_2003", "vock_2016")` #' -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ -#' if (requireNamespace("mlr3pipelines", quietly = TRUE)) { #' library(mlr3) #' library(mlr3pipelines) #' @@ -105,7 +104,6 @@ #' new_task = po$train(list(task, task))[[1]] #' print(new_task) #' } -#' } #' @family PipeOps #' @family Transformation PipeOps #' @include PipeOpPredTransformer.R diff --git a/R/TaskGeneratorCoxed.R b/R/TaskGeneratorCoxed.R index 1bdfdc672..f4e5e32b5 100644 --- a/R/TaskGeneratorCoxed.R +++ b/R/TaskGeneratorCoxed.R @@ -16,9 +16,7 @@ #' @template task_generator #' #' @template seealso_task_generator -#' @export -#' @examples -#' if (requireNamespace("coxed", quietly = TRUE)) { +#' @examplesIf mlr3misc::require_namespaces(c("coxed"), quietly = TRUE) #' library(mlr3) #' #' # time horizon = 365 days, censoring proportion = 60%, 6 covariates normally @@ -31,7 +29,7 @@ #' # same as above, but with time-varying coefficients (counting process format) #' gen$param_set$set_values(type = "tvc") #' gen$generate(50) -#' } +#' @export TaskGeneratorCoxed = R6Class("TaskGeneratorCoxed", inherit = TaskGenerator, public = list( diff --git a/R/TaskGeneratorSimsurv.R b/R/TaskGeneratorSimsurv.R index 453ce295e..0396b4a59 100644 --- a/R/TaskGeneratorSimsurv.R +++ b/R/TaskGeneratorSimsurv.R @@ -20,9 +20,7 @@ #' @template task_generator #' #' @template seealso_task_generator -#' @export -#' @examples -#' if (requireNamespace("simsurv", quietly = TRUE)) { +#' @examplesIf mlr3misc::require_namespaces(c("simsurv"), quietly = TRUE) #' # generate 20 samples with Weibull survival distribution #' gen = tgen("simsurv") #' task = gen$generate(20) @@ -32,7 +30,7 @@ #' gen = tgen("simsurv", dist = "exponential", gammas = NULL, maxt = 40) #' task = gen$generate(100) #' head(task) -#' } +#' @export TaskGeneratorSimsurv = R6Class("TaskGeneratorSimsurv", inherit = TaskGenerator, public = list( diff --git a/R/TaskSurv.R b/R/TaskSurv.R index 7fbcc034e..17e8267db 100644 --- a/R/TaskSurv.R +++ b/R/TaskSurv.R @@ -16,7 +16,6 @@ #' `r format_bib("grambsch_1994")` #' #' @family Task -#' @export #' @examples #' library(mlr3) #' task = tsk("lung") @@ -47,6 +46,7 @@ #' task$prop_haz() #' # veteran data is definitely non-PH (p << 0.05) #' tsk("veteran")$prop_haz() +#' @export TaskSurv = R6Class("TaskSurv", inherit = TaskSupervised, public = list( diff --git a/R/as_prediction_dens.R b/R/as_prediction_dens.R index d762df1cf..101b372a0 100644 --- a/R/as_prediction_dens.R +++ b/R/as_prediction_dens.R @@ -23,14 +23,12 @@ as_prediction_dens = function(x, ...) { UseMethod("as_prediction_dens") } - #' @rdname as_prediction_dens #' @export as_prediction_dens.PredictionDens = function(x, ...) { # nolint x } - #' @rdname as_prediction_dens #' @export as_prediction_dens.data.frame = function(x, ...) { # nolint diff --git a/R/autoplot.R b/R/autoplot.R index fe94e5cc0..e4e5cd0c4 100644 --- a/R/autoplot.R +++ b/R/autoplot.R @@ -27,8 +27,7 @@ #' #' @template section_theme #' -#' @export -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3viz", "ggplot2"), quietly = TRUE) #' library(mlr3) #' library(mlr3viz) #' library(mlr3proba) @@ -41,6 +40,7 @@ #' autoplot(task) # KM of the censoring distribution #' autoplot(task, rhs = "sex") #' autoplot(task, type = "duo") +#' @export autoplot.TaskSurv = function(object, type = "target", theme = theme_minimal(), reverse = FALSE, ...) { # nolint assert_string(type) require_namespaces(c("survival", "GGally")) @@ -99,8 +99,7 @@ plot.TaskSurv = function(x, ...) { #' #' @template section_theme #' -#' @export -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3viz", "ggplot2"), quietly = TRUE) #' library(mlr3) #' library(mlr3proba) #' library(mlr3viz) @@ -112,6 +111,7 @@ plot.TaskSurv = function(x, ...) { #' autoplot(task, type = "freq", bins = 15) #' autoplot(task, type = "overlay", bins = 15) #' autoplot(task, type = "freqpoly", bins = 15) +#' @export autoplot.TaskDens = function(object, type = "dens", theme = theme_minimal(), ...) { # nolint assert_choice(type, c("dens", "freq", "overlay", "freqpoly")) @@ -183,8 +183,7 @@ plot.TaskDens = function(x, ...) { #' @references #' `r format_bib("dcalib")` #' -#' @export -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("mlr3viz", "ggplot2"), quietly = TRUE) #' library(mlr3) #' library(mlr3proba) #' library(mlr3viz) @@ -201,6 +200,7 @@ plot.TaskDens = function(x, ...) { #' #' # Predictions #' autoplot(p, type = "preds") +#' @export autoplot.PredictionSurv = function(object, type = "calib", task = NULL, row_ids = NULL, times = NULL, xyline = TRUE, cuts = 11L, theme = theme_minimal(), extend_quantile = FALSE, ...) { diff --git a/R/pecs.R b/R/pecs.R index 2563030ee..449fd6dc7 100644 --- a/R/pecs.R +++ b/R/pecs.R @@ -32,9 +32,8 @@ #' from the [PredictionSurv] or [TaskSurv] object. If a range is provided for `times` without `n`, #' then all time-points between the range are returned. #' -#' @examples +#' @examplesIf mlr3misc::require_namespaces(c("ggplot2"), quietly = TRUE) #' \dontrun{ -#' if (requireNamespace("ggplot2", quietly = TRUE)) { #' #' library(mlr3) #' task = tsk("rats") #' @@ -55,7 +54,6 @@ #' lapply(learns, function(x) x$train(task)) #' pecs(learns, task = task, measure = "logloss", times = c(20, 90), n = 10) #' } -#' } #' #' @export pecs = function(x, measure = c("graf", "logloss"), times, n, eps = NULL, ...) { diff --git a/man/autoplot.PredictionSurv.Rd b/man/autoplot.PredictionSurv.Rd index 9db104459..771bb2428 100644 --- a/man/autoplot.PredictionSurv.Rd +++ b/man/autoplot.PredictionSurv.Rd @@ -63,6 +63,7 @@ predicted median survival time. A model is D-calibrated if the resulting plot li } } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3viz", "ggplot2"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} library(mlr3) library(mlr3proba) library(mlr3viz) @@ -79,6 +80,7 @@ autoplot(p, type = "dcalib") # Predictions autoplot(p, type = "preds") +\dontshow{\}) # examplesIf} } \references{ Haider H, Hoehn B, Davis S, Greiner R (2020). diff --git a/man/autoplot.TaskDens.Rd b/man/autoplot.TaskDens.Rd index 7f379142a..0df103bd3 100644 --- a/man/autoplot.TaskDens.Rd +++ b/man/autoplot.TaskDens.Rd @@ -32,6 +32,7 @@ Additional arguments, possibly passed down to the underlying plot functions.} Generates plots for \link{TaskDens}. } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3viz", "ggplot2"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} library(mlr3) library(mlr3proba) library(mlr3viz) @@ -43,4 +44,5 @@ autoplot(task, bins = 15) autoplot(task, type = "freq", bins = 15) autoplot(task, type = "overlay", bins = 15) autoplot(task, type = "freqpoly", bins = 15) +\dontshow{\}) # examplesIf} } diff --git a/man/autoplot.TaskSurv.Rd b/man/autoplot.TaskSurv.Rd index af4a16a84..cacb6acc9 100644 --- a/man/autoplot.TaskSurv.Rd +++ b/man/autoplot.TaskSurv.Rd @@ -47,6 +47,7 @@ Color is set to target column. } } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3viz", "ggplot2"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} library(mlr3) library(mlr3viz) library(mlr3proba) @@ -59,4 +60,5 @@ autoplot(task) # KM autoplot(task) # KM of the censoring distribution autoplot(task, rhs = "sex") autoplot(task, type = "duo") +\dontshow{\}) # examplesIf} } diff --git a/man/mlr_pipeops_compose_breslow_distr.Rd b/man/mlr_pipeops_compose_breslow_distr.Rd index c8f3138fa..95e016ca5 100644 --- a/man/mlr_pipeops_compose_breslow_distr.Rd +++ b/man/mlr_pipeops_compose_breslow_distr.Rd @@ -53,8 +53,8 @@ another. } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ -if (requireNamespace("mlr3pipelines", quietly = TRUE)) { library(mlr3) library(mlr3pipelines) task = tsk("rats") @@ -68,7 +68,7 @@ if (requireNamespace("mlr3pipelines", quietly = TRUE)) { b$train(list(train_task)) p = b$predict(list(test_task))[[1L]] } -} +\dontshow{\}) # examplesIf} } \seealso{ \link{pipeline_distrcompositor} diff --git a/man/mlr_pipeops_compose_probregr.Rd b/man/mlr_pipeops_compose_probregr.Rd index 968ae2561..1af2fbe7d 100644 --- a/man/mlr_pipeops_compose_probregr.Rd +++ b/man/mlr_pipeops_compose_probregr.Rd @@ -52,9 +52,8 @@ distribution location and scale parameters respectively. } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines", "rpart"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ -if (requireNamespace("mlr3pipelines", quietly = TRUE) && - requireNamespace("rpart", quietly = TRUE)) { library(mlr3) library(mlr3pipelines) set.seed(1) @@ -74,7 +73,7 @@ if (requireNamespace("mlr3pipelines", quietly = TRUE) && poc = po("compose_probregr") poc$predict(list(pred_response, pred_se))[[1]] } -} +\dontshow{\}) # examplesIf} } \section{Super class}{ \code{\link[mlr3pipelines:PipeOp]{mlr3pipelines::PipeOp}} -> \code{PipeOpProbregr} diff --git a/man/mlr_pipeops_crankcompose.Rd b/man/mlr_pipeops_crankcompose.Rd index adedaf7f7..3e088b9e9 100644 --- a/man/mlr_pipeops_crankcompose.Rd +++ b/man/mlr_pipeops_crankcompose.Rd @@ -47,8 +47,8 @@ If \code{TRUE}, then the \code{crank} will be overwritten. } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ -if (requireNamespace("mlr3pipelines", quietly = TRUE)) { library(mlr3pipelines) task = tsk("rats") @@ -57,7 +57,7 @@ if (requireNamespace("mlr3pipelines", quietly = TRUE)) { poc = po("crankcompose", param_vals = list(overwrite = TRUE)) poc$predict(list(pred))[[1L]] } -} +\dontshow{\}) # examplesIf} } \seealso{ \link{pipeline_crankcompositor} diff --git a/man/mlr_pipeops_distrcompose.Rd b/man/mlr_pipeops_distrcompose.Rd index 5d43c3504..d727697b5 100644 --- a/man/mlr_pipeops_distrcompose.Rd +++ b/man/mlr_pipeops_distrcompose.Rd @@ -72,8 +72,8 @@ predicted linear predictor. } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ -if (requireNamespace("mlr3pipelines", quietly = TRUE)) { library(mlr3) library(mlr3pipelines) task = tsk("rats") @@ -84,7 +84,7 @@ if (requireNamespace("mlr3pipelines", quietly = TRUE)) { pod = po("distrcompose", param_vals = list(form = "aft", overwrite = TRUE)) pod$predict(list(base = base, pred = pred))[[1]] } -} +\dontshow{\}) # examplesIf} } \seealso{ \link{pipeline_distrcompositor} diff --git a/man/mlr_pipeops_responsecompose.Rd b/man/mlr_pipeops_responsecompose.Rd index 6a0dc8642..3d8eb6433 100644 --- a/man/mlr_pipeops_responsecompose.Rd +++ b/man/mlr_pipeops_responsecompose.Rd @@ -70,8 +70,8 @@ This is \strong{not a good estimate to use in general}, only a reasonable substi } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ -if (requireNamespace("mlr3pipelines", quietly = TRUE)) { library(mlr3pipelines) task = tsk("rats") @@ -87,7 +87,7 @@ if (requireNamespace("mlr3pipelines", quietly = TRUE)) { por = po("responsecompose", param_vals = list(overwrite = TRUE, add_crank = TRUE)) por$predict(list(pred))[[1L]] } -} +\dontshow{\}) # examplesIf} } \references{ Zhao, Lihui, Claggett, Brian, Tian, Lu, Uno, Hajime, Pfeffer, A. M, Solomon, D. S, Trippa, Lorenzo, Wei, J. L (2016). diff --git a/man/mlr_pipeops_survavg.Rd b/man/mlr_pipeops_survavg.Rd index 48e02f1cf..1fee0908a 100644 --- a/man/mlr_pipeops_survavg.Rd +++ b/man/mlr_pipeops_survavg.Rd @@ -43,8 +43,8 @@ Inherits from \link[mlr3pipelines:PipeOpEnsemble]{PipeOpEnsemble} by implementin } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ -if (requireNamespace("mlr3pipelines", quietly = TRUE)) { library(mlr3) library(mlr3pipelines) @@ -54,7 +54,7 @@ if (requireNamespace("mlr3pipelines", quietly = TRUE)) { poc = po("survavg", param_vals = list(weights = c(0.2, 0.8))) poc$predict(list(p1, p2)) } -} +\dontshow{\}) # examplesIf} } \seealso{ Other PipeOps: diff --git a/man/mlr_pipeops_trafopred_regrsurv.Rd b/man/mlr_pipeops_trafopred_regrsurv.Rd index f723fc18c..8805ce1d7 100644 --- a/man/mlr_pipeops_trafopred_regrsurv.Rd +++ b/man/mlr_pipeops_trafopred_regrsurv.Rd @@ -31,8 +31,8 @@ of same length as the prediction object, where \code{1} is dead and \code{0} cen } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ -if (requireNamespace("mlr3pipelines", quietly = TRUE)) { library(mlr3) library(mlr3pipelines) @@ -54,7 +54,7 @@ if (requireNamespace("mlr3pipelines", quietly = TRUE)) { new_pred = po$predict(list(pred = pred, task = task_surv))[[1]] all.equal(new_pred$truth, task_surv$truth()) } -} +\dontshow{\}) # examplesIf} } \seealso{ Other PipeOps: diff --git a/man/mlr_pipeops_trafopred_survregr.Rd b/man/mlr_pipeops_trafopred_survregr.Rd index 405f02f9a..edc4218ea 100644 --- a/man/mlr_pipeops_trafopred_survregr.Rd +++ b/man/mlr_pipeops_trafopred_survregr.Rd @@ -21,8 +21,8 @@ The \verb{$state} is a named \code{list} with the \verb{$state} elements inherit } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ -if (requireNamespace("mlr3pipelines", quietly = TRUE)) { library(mlr3) library(mlr3pipelines) library(survival) @@ -34,7 +34,7 @@ if (requireNamespace("mlr3pipelines", quietly = TRUE)) { new_pred = po$predict(list(pred = pred))[[1]] print(new_pred) } -} +\dontshow{\}) # examplesIf} } \seealso{ Other PipeOps: diff --git a/man/mlr_pipeops_trafotask_regrsurv.Rd b/man/mlr_pipeops_trafotask_regrsurv.Rd index bf79af239..b4f0910e5 100644 --- a/man/mlr_pipeops_trafotask_regrsurv.Rd +++ b/man/mlr_pipeops_trafotask_regrsurv.Rd @@ -30,8 +30,8 @@ of same length as the prediction object, where \code{1} is dead and \code{0} cen } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ -if (requireNamespace("mlr3pipelines", quietly = TRUE)) { library(mlr3) library(mlr3pipelines) @@ -50,7 +50,7 @@ if (requireNamespace("mlr3pipelines", quietly = TRUE)) { new_task$truth() task_surv$truth() } -} +\dontshow{\}) # examplesIf} } \seealso{ Other PipeOps: diff --git a/man/mlr_pipeops_trafotask_survclassif_disctime.Rd b/man/mlr_pipeops_trafotask_survclassif_disctime.Rd index 6019e8325..fc6186024 100644 --- a/man/mlr_pipeops_trafotask_survclassif_disctime.Rd +++ b/man/mlr_pipeops_trafotask_survclassif_disctime.Rd @@ -69,9 +69,8 @@ Needs to be greater than the minimum event time in the given task. } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines", "mlr3learners"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ -if (requireNamespace("mlr3pipelines", quietly = TRUE) && - requireNamespace("mlr3learners", quietly = TRUE)) { library(mlr3) library(mlr3learners) library(mlr3pipelines) @@ -90,8 +89,7 @@ if (requireNamespace("mlr3pipelines", quietly = TRUE) && learner = lrn("classif.log_reg", predict_type = "prob") learner$train(task_classif) } -} - +\dontshow{\}) # examplesIf} } \references{ Tutz, Gerhard, Schmid, Matthias (2016). diff --git a/man/mlr_pipeops_trafotask_survregr.Rd b/man/mlr_pipeops_trafotask_survregr.Rd index cebdf7bc8..60cc7ceff 100644 --- a/man/mlr_pipeops_trafotask_survregr.Rd +++ b/man/mlr_pipeops_trafotask_survregr.Rd @@ -61,8 +61,8 @@ Passed to \link[bujar:bujar]{bujar::bujar}. } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ -if (requireNamespace("mlr3pipelines", quietly = TRUE)) { library(mlr3) library(mlr3pipelines) @@ -112,7 +112,7 @@ if (requireNamespace("mlr3pipelines", quietly = TRUE)) { new_task = po$train(list(task, task))[[1]] print(new_task) } -} +\dontshow{\}) # examplesIf} } \references{ Buckley, Jonathan, James, Ian (1979). diff --git a/man/mlr_task_generators_coxed.Rd b/man/mlr_task_generators_coxed.Rd index 6d6ef2739..e22a25017 100644 --- a/man/mlr_task_generators_coxed.Rd +++ b/man/mlr_task_generators_coxed.Rd @@ -39,7 +39,7 @@ tgen("coxed") } \examples{ -if (requireNamespace("coxed", quietly = TRUE)) { +\dontshow{if (mlr3misc::require_namespaces(c("coxed"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} library(mlr3) # time horizon = 365 days, censoring proportion = 60\%, 6 covariates normally @@ -52,7 +52,7 @@ if (requireNamespace("coxed", quietly = TRUE)) { # same as above, but with time-varying coefficients (counting process format) gen$param_set$set_values(type = "tvc") gen$generate(50) -} +\dontshow{\}) # examplesIf} } \seealso{ \itemize{ diff --git a/man/mlr_task_generators_simsurv.Rd b/man/mlr_task_generators_simsurv.Rd index 49f692830..81ccac464 100644 --- a/man/mlr_task_generators_simsurv.Rd +++ b/man/mlr_task_generators_simsurv.Rd @@ -39,7 +39,7 @@ tgen("simsurv") } \examples{ -if (requireNamespace("simsurv", quietly = TRUE)) { +\dontshow{if (mlr3misc::require_namespaces(c("simsurv"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} # generate 20 samples with Weibull survival distribution gen = tgen("simsurv") task = gen$generate(20) @@ -49,7 +49,7 @@ if (requireNamespace("simsurv", quietly = TRUE)) { gen = tgen("simsurv", dist = "exponential", gammas = NULL, maxt = 40) task = gen$generate(100) head(task) -} +\dontshow{\}) # examplesIf} } \seealso{ \itemize{ diff --git a/man/pecs.Rd b/man/pecs.Rd index 71d5b1a9c..a00d02d50 100644 --- a/man/pecs.Rd +++ b/man/pecs.Rd @@ -84,8 +84,8 @@ from the \link{PredictionSurv} or \link{TaskSurv} object. If a range is provided then all time-points between the range are returned. } \examples{ +\dontshow{if (mlr3misc::require_namespaces(c("ggplot2"), quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} \dontrun{ -if (requireNamespace("ggplot2", quietly = TRUE)) { #' library(mlr3) task = tsk("rats") @@ -106,6 +106,5 @@ if (requireNamespace("ggplot2", quietly = TRUE)) { lapply(learns, function(x) x$train(task)) pecs(learns, task = task, measure = "logloss", times = c(20, 90), n = 10) } -} - +\dontshow{\}) # examplesIf} } From 78447ea3703f1e8970f1b421e2383eb81aa7d290 Mon Sep 17 00:00:00 2001 From: john Date: Tue, 22 Oct 2024 10:22:14 +0200 Subject: [PATCH 11/18] add experimental badge --- R/PipeOpProbregrCompositor.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/PipeOpProbregrCompositor.R b/R/PipeOpProbregrCompositor.R index eb1306108..5cc6629df 100644 --- a/R/PipeOpProbregrCompositor.R +++ b/R/PipeOpProbregrCompositor.R @@ -3,7 +3,9 @@ #' @template param_pipelines #' #' @description -#' Combines a predicted `reponse` and `se` from [PredictionRegr] with a specified probability +#' `r lifecycle::badge("experimental")` +#' +#' Combines a predicted `response` and `se` from [PredictionRegr] with a specified probability #' distribution to estimate (or 'compose') a `distr` prediction. #' #' @section Dictionary: From 916ddb912b5397994164048cee99093a6ccb0df4 Mon Sep 17 00:00:00 2001 From: john Date: Tue, 22 Oct 2024 10:22:25 +0200 Subject: [PATCH 12/18] add refs also in the crankcompose pipeop --- R/PipeOpCrankCompositor.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/PipeOpCrankCompositor.R b/R/PipeOpCrankCompositor.R index ddf2eade3..bd54c74ab 100644 --- a/R/PipeOpCrankCompositor.R +++ b/R/PipeOpCrankCompositor.R @@ -35,6 +35,8 @@ #' If `TRUE`, then the `crank` will be overwritten. #' #' @seealso [pipeline_crankcompositor] +#' @references +#' `r format_bib("sonabend_2022", "ishwaran_2008")` #' @family survival compositors #' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ From 0a8577cf3389faed81acf2ce7ea4ffef4adde002 Mon Sep 17 00:00:00 2001 From: john Date: Tue, 22 Oct 2024 10:22:52 +0200 Subject: [PATCH 13/18] add paper --- R/bibentries.R | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/R/bibentries.R b/R/bibentries.R index 11c5ccb87..c4ec04dd9 100644 --- a/R/bibentries.R +++ b/R/bibentries.R @@ -709,5 +709,13 @@ bibentries = c( # nolint start url = "https://onlinelibrary.wiley.com/doi/full/10.1111/biom.12384", volume = "72", year = "2016" + ), + norman_2024 = bibentry("article", + author = "Norman, Patrick A and Li, Wanlu and Jiang, Wenyu and Chen, Bingshu E", + title = "deepAFT: A nonlinear accelerated failure time model with artificial neural network", + journal = "Statistics in Medicine", + year = "2024", + publisher = "Wiley Online Library", + doi = "10.1002/sim.10152" ) -) # nolint end +) From 25c747a2e48ef26f56279c3811bb326a455b9ec4 Mon Sep 17 00:00:00 2001 From: john Date: Tue, 22 Oct 2024 10:23:51 +0200 Subject: [PATCH 14/18] add experimental badge + scale_lp heuristic argument --- R/PipeOpDistrCompositor.R | 38 ++++++++++++++++++++++++++++++++------ R/pipelines.R | 11 +++++++++-- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/R/PipeOpDistrCompositor.R b/R/PipeOpDistrCompositor.R index 3c1d1983e..d6b5f5991 100644 --- a/R/PipeOpDistrCompositor.R +++ b/R/PipeOpDistrCompositor.R @@ -3,6 +3,8 @@ #' @template param_pipelines #' #' @description +#' `r lifecycle::badge("experimental")` +#' #' Estimates (or 'composes') a survival distribution from a predicted baseline #' survival distribution (`distr`) and a linear predictor (`lp`) from two [PredictionSurv]s. #' @@ -46,16 +48,33 @@ #' nothing and returns the given [PredictionSurv]. If `TRUE`, then the `distr` is overwritten #' with the `distr` composed from `lp` - this is useful for changing the prediction #' `distr` from one model form to another. +#' * `scale_lp` :: `logical(1)` \cr +#' This option is only applicable to `form` equal to `"aft"`. If `TRUE`, it +#' min-max scales the linear prediction scores to be in the interval \eqn{[0,1]}, +#' avoiding extrapolation of the baseline \eqn{S_0(t)} on the transformed time +#' points \eqn{\frac{t}{\exp(lp)}}, as these will be \eqn{\in [\frac{t}{e}, t]}, +#' and so always smaller than the maximum time point for which we have estimated +#' \eqn{S_0(t)}. +#' Note that this is just a **heuristic** to get reasonable results in the +#' case you observe survival predictions to be e.g. constant after the AFT +#' composition and it definitely provides no guarantee for creating calibrated +#' distribution predictions (as none of these methods do). Therefore, it is +#' set to `FALSE` by default. #' #' @section Internals: #' The respective `form`s above have respective survival distributions: -#' \deqn{aft: S(t) = S_0(\frac{t}{exp(lp)})}{aft: S(t) = S0(t/exp(lp))} -#' \deqn{ph: S(t) = S_0(t)^{exp(lp)}}{ph: S(t) = S0(t)^exp(lp)} -#' \deqn{po: S(t) = \frac{S_0(t)}{exp(-lp) + (1-exp(-lp)) S_0(t)}}{po: S(t) = S0(t) / [exp(-lp) + S0(t) (1-exp(-lp))]} # nolint -#' where \eqn{S_0}{S0} is the estimated baseline survival distribution, and \eqn{lp} is the +#' \deqn{aft: S(t) = S_0(\frac{t}{\exp(lp)})} +#' \deqn{ph: S(t) = S_0(t)^{\exp(lp)}} +#' \deqn{po: S(t) = \frac{S_0(t)}{\exp(-lp) + (1-\exp(-lp)) S_0(t)}} +#' where \eqn{S_0} is the estimated baseline survival distribution, and \eqn{lp} is the #' predicted linear predictor. #' +#' For an example use of the `"aft"` composition using Kaplan-Meier as a baseline +#' distribution, see Norman et al. (2024). +#' #' @seealso [pipeline_distrcompositor] +#' @references +#' `r format_bib("norman_2024")` #' @export #' @family survival compositors #' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) @@ -78,9 +97,10 @@ PipeOpDistrCompositor = R6Class("PipeOpDistrCompositor", initialize = function(id = "distrcompose", param_vals = list()) { param_set = ps( form = p_fct(default = "aft", levels = c("aft", "ph", "po"), tags = "predict"), - overwrite = p_lgl(default = FALSE, tags = "predict") + overwrite = p_lgl(default = FALSE, tags = "predict"), + scale_lp = p_lgl(default = FALSE, tags = "predict") ) - param_set$set_values(form = "aft", overwrite = FALSE) + param_set$set_values(form = "aft", overwrite = FALSE, scale_lp = FALSE) super$initialize( id = id, @@ -143,6 +163,12 @@ PipeOpDistrCompositor = R6Class("PipeOpDistrCompositor", if (form == "ph") { cdf = 1 - (survmat ^ exp(lpmat)) } else if (form == "aft") { + # add heuristic to keep the transformed t/exp(lp) time points within + # the domain of S_0(t) + if (self$param_set$values$scale_lp) { + lpmat = (lpmat - min(lpmat)) / (max(lpmat) - min(lpmat)) + } + # calculate cdf = 1 - S_0(t) on the time points t/exp(lp) mtc = findInterval(timesmat / exp(lpmat), times) mtc[mtc == 0] = NA cdf = 1 - matrix(survmat[1L, mtc], nr, nc, FALSE) diff --git a/R/pipelines.R b/R/pipelines.R index 693c2ee82..ba916da1e 100644 --- a/R/pipelines.R +++ b/R/pipelines.R @@ -219,6 +219,10 @@ pipeline_responsecompositor = function(learner, method = "rmst", tau = NULL, #' If `TRUE` then the `distr` is overwritten by the compositor if #' already present, which may be required for changing the prediction `distr` from one model form #' to another. +#' @param scale_lp (`logical(1)`)\cr +#' If `TRUE` and `form` is `"aft"`, the linear predictor scores are scaled before +#' the composition. Experimental option, see more details on [PipeOpDistrCompositor]. +#' Default is `FALSE`. #' #' @examplesIf mlr3misc::require_namespaces(c("mlr3pipelines"), quietly = TRUE) #' \dontrun{ @@ -238,11 +242,12 @@ pipeline_responsecompositor = function(learner, method = "rmst", tau = NULL, #' grlrn$predict(task) #' } pipeline_distrcompositor = function(learner, estimator = "kaplan", form = "aft", - overwrite = FALSE, graph_learner = FALSE) { + overwrite = FALSE, scale_lp = FALSE, graph_learner = FALSE) { # some checks assert_choice(estimator, choices = c("kaplan", "nelson", "breslow"), null.ok = FALSE) assert_choice(form, choices = c("aft", "ph", "po"), null.ok = FALSE) assert_learner(learner, task_type = "surv") + assert_logical(scale_lp, len = 1) # make the pipeline Graph object if (estimator == "breslow") { @@ -253,7 +258,9 @@ pipeline_distrcompositor = function(learner, estimator = "kaplan", form = "aft", learner_id = paste0("distrcompositor.", estimator) base = po("learner", lrn(learner_key, id = learner_id)) - compositor = po("distrcompose", param_vals = list(form = form, overwrite = overwrite)) + compositor = po("distrcompose", param_vals = list( + form = form, overwrite = overwrite, scale_lp = scale_lp + )) gr = gunion(list(base, pred)) %>>% compositor } From 5084162eaeee4b09be8d0244ee190fbe806155b3 Mon Sep 17 00:00:00 2001 From: john Date: Tue, 22 Oct 2024 10:25:13 +0200 Subject: [PATCH 15/18] add tests for scale_lp option --- tests/testthat/test_distrcompose.R | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/testthat/test_distrcompose.R b/tests/testthat/test_distrcompose.R index a2aadc4c0..b587597b3 100644 --- a/tests/testthat/test_distrcompose.R +++ b/tests/testthat/test_distrcompose.R @@ -18,6 +18,22 @@ test_that("no params", { expect_silent(pod$predict(list(base = base, pred = pred))) }) +test_that("scale_lp = TRUE", { + base = lrn("surv.kaplan")$train(task)$predict(task) + pod1 = po("distrcompose", param_vals = list(overwrite = TRUE, scale_lp = FALSE)) + pod2 = po("distrcompose", param_vals = list(overwrite = TRUE, scale_lp = TRUE)) + + p1 = pod1$predict(list(base = base, pred = cox_pred))[[1L]] + p2 = pod2$predict(list(base = base, pred = cox_pred))[[1L]] + + # same lp scores + expect_equal(p1$lp, p2$lp) + # same time points + expect_equal(colnames(p1$data$distr), colnames(p2$data$distr)) + # different survival distributions + expect_false(all(p1$data$distr == p2$data$distr)) +}) + test_that("overwrite = FALSE", { gr = ppl("distrcompositor", lrn("surv.kaplan"), overwrite = FALSE) expect_class(gr, "Graph") From 6a09a13e61099a22bdf12872504a93e3da73f7cb Mon Sep 17 00:00:00 2001 From: john Date: Tue, 22 Oct 2024 10:25:28 +0200 Subject: [PATCH 16/18] update docs --- man/mlr_graphs_distrcompositor.Rd | 6 ++++++ man/mlr_pipeops_compose_probregr.Rd | 4 +++- man/mlr_pipeops_crankcompose.Rd | 10 ++++++++++ man/mlr_pipeops_distrcompose.Rd | 31 +++++++++++++++++++++++++---- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/man/mlr_graphs_distrcompositor.Rd b/man/mlr_graphs_distrcompositor.Rd index 1463d6535..e144c9d66 100644 --- a/man/mlr_graphs_distrcompositor.Rd +++ b/man/mlr_graphs_distrcompositor.Rd @@ -10,6 +10,7 @@ pipeline_distrcompositor( estimator = "kaplan", form = "aft", overwrite = FALSE, + scale_lp = FALSE, graph_learner = FALSE ) } @@ -36,6 +37,11 @@ If \code{TRUE} then the \code{distr} is overwritten by the compositor if already present, which may be required for changing the prediction \code{distr} from one model form to another.} +\item{scale_lp}{(\code{logical(1)})\cr +If \code{TRUE} and \code{form} is \code{"aft"}, the linear predictor scores are scaled before +the composition. Experimental option, see more details on \link{PipeOpDistrCompositor}. +Default is \code{FALSE}.} + \item{graph_learner}{(\code{logical(1)})\cr If \code{TRUE} returns wraps the \link[mlr3pipelines:Graph]{Graph} as a \link[mlr3pipelines:mlr_learners_graph]{GraphLearner} otherwise (default) returns as a \code{Graph}.} diff --git a/man/mlr_pipeops_compose_probregr.Rd b/man/mlr_pipeops_compose_probregr.Rd index 1af2fbe7d..e81177f19 100644 --- a/man/mlr_pipeops_compose_probregr.Rd +++ b/man/mlr_pipeops_compose_probregr.Rd @@ -5,7 +5,9 @@ \alias{PipeOpProbregr} \title{PipeOpProbregr} \description{ -Combines a predicted \code{reponse} and \code{se} from \link{PredictionRegr} with a specified probability +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} + +Combines a predicted \code{response} and \code{se} from \link{PredictionRegr} with a specified probability distribution to estimate (or 'compose') a \code{distr} prediction. } \section{Dictionary}{ diff --git a/man/mlr_pipeops_crankcompose.Rd b/man/mlr_pipeops_crankcompose.Rd index 3e088b9e9..8fc7284ff 100644 --- a/man/mlr_pipeops_crankcompose.Rd +++ b/man/mlr_pipeops_crankcompose.Rd @@ -59,6 +59,16 @@ If \code{TRUE}, then the \code{crank} will be overwritten. } \dontshow{\}) # examplesIf} } +\references{ +Sonabend, Raphael, Bender, Andreas, Vollmer, Sebastian (2022). +\dQuote{Avoiding C-hacking when evaluating survival distribution predictions with discrimination measures.} +\emph{Bioinformatics}. +ISSN 1367-4803, \doi{10.1093/BIOINFORMATICS/BTAC451}, \url{https://academic.oup.com/bioinformatics/advance-article/doi/10.1093/bioinformatics/btac451/6640155}. + +Ishwaran, Hemant, Kogalur, B U, Blackstone, H E, Lauer, S M, others (2008). +\dQuote{Random survival forests.} +\emph{The Annals of applied statistics}, \bold{2}(3), 841--860. +} \seealso{ \link{pipeline_crankcompositor} diff --git a/man/mlr_pipeops_distrcompose.Rd b/man/mlr_pipeops_distrcompose.Rd index d727697b5..3c2447373 100644 --- a/man/mlr_pipeops_distrcompose.Rd +++ b/man/mlr_pipeops_distrcompose.Rd @@ -5,6 +5,8 @@ \alias{PipeOpDistrCompositor} \title{PipeOpDistrCompositor} \description{ +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} + Estimates (or 'composes') a survival distribution from a predicted baseline survival distribution (\code{distr}) and a linear predictor (\code{lp}) from two \link{PredictionSurv}s. @@ -58,17 +60,32 @@ If \code{FALSE} (default) then if the "pred" input already has a \code{distr}, t nothing and returns the given \link{PredictionSurv}. If \code{TRUE}, then the \code{distr} is overwritten with the \code{distr} composed from \code{lp} - this is useful for changing the prediction \code{distr} from one model form to another. +\item \code{scale_lp} :: \code{logical(1)} \cr +This option is only applicable to \code{form} equal to \code{"aft"}. If \code{TRUE}, it +min-max scales the linear prediction scores to be in the interval \eqn{[0,1]}, +avoiding extrapolation of the baseline \eqn{S_0(t)} on the transformed time +points \eqn{\frac{t}{\exp(lp)}}, as these will be \eqn{\in [\frac{t}{e}, t]}, +and so always smaller than the maximum time point for which we have estimated +\eqn{S_0(t)}. +Note that this is just a \strong{heuristic} to get reasonable results in the +case you observe survival predictions to be e.g. constant after the AFT +composition and it definitely provides no guarantee for creating calibrated +distribution predictions (as none of these methods do). Therefore, it is +set to \code{FALSE} by default. } } \section{Internals}{ The respective \code{form}s above have respective survival distributions: -\deqn{aft: S(t) = S_0(\frac{t}{exp(lp)})}{aft: S(t) = S0(t/exp(lp))} -\deqn{ph: S(t) = S_0(t)^{exp(lp)}}{ph: S(t) = S0(t)^exp(lp)} -\deqn{po: S(t) = \frac{S_0(t)}{exp(-lp) + (1-exp(-lp)) S_0(t)}}{po: S(t) = S0(t) / [exp(-lp) + S0(t) (1-exp(-lp))]} # nolint -where \eqn{S_0}{S0} is the estimated baseline survival distribution, and \eqn{lp} is the +\deqn{aft: S(t) = S_0(\frac{t}{\exp(lp)})} +\deqn{ph: S(t) = S_0(t)^{\exp(lp)}} +\deqn{po: S(t) = \frac{S_0(t)}{\exp(-lp) + (1-\exp(-lp)) S_0(t)}} +where \eqn{S_0} is the estimated baseline survival distribution, and \eqn{lp} is the predicted linear predictor. + +For an example use of the \code{"aft"} composition using Kaplan-Meier as a baseline +distribution, see Norman et al. (2024). } \examples{ @@ -86,6 +103,12 @@ predicted linear predictor. } \dontshow{\}) # examplesIf} } +\references{ +Norman, A P, Li, Wanlu, Jiang, Wenyu, Chen, E B (2024). +\dQuote{deepAFT: A nonlinear accelerated failure time model with artificial neural network.} +\emph{Statistics in Medicine}. +\doi{10.1002/sim.10152}. +} \seealso{ \link{pipeline_distrcompositor} From 2bf7430a063674ad21d7081a892fffaa355256f5 Mon Sep 17 00:00:00 2001 From: john Date: Tue, 22 Oct 2024 10:35:43 +0200 Subject: [PATCH 17/18] update version + news --- DESCRIPTION | 2 +- NEWS.md | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index b03ba9bf0..d2a4e7840 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: mlr3proba Title: Probabilistic Supervised Learning for 'mlr3' -Version: 0.6.9 +Version: 0.7.0 Authors@R: c(person(given = "Raphael", family = "Sonabend", diff --git a/NEWS.md b/NEWS.md index c20e7f20c..f70d31d10 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# mlr3proba 0.7.0 + +* Add `mlr3pipelines` to `Imports` and set minimum latest version from CRAN (`0.7.0`) +* Refactor code to minimize namespace calling and imports such as `mlr3pipelines::` or `R6::` +* Add experimental badge in the documentation of a few more PipeOps +* Add argument `scale_lp` for AFT `distrcompose` pipeop + respective pipeline + # mlr3proba 0.6.9 * New `PipeOp`s: `PipeOpTaskSurvClassifIPCW`, `PipeOpPredClassifSurvIPCW` From b736c8f4e6f473b78fa4cc82bcd1b836c6c6264f Mon Sep 17 00:00:00 2001 From: john Date: Tue, 22 Oct 2024 10:48:23 +0200 Subject: [PATCH 18/18] remove obsolete C++ function --- NAMESPACE | 1 - R/RcppExports.R | 4 ---- R/zzz.R | 5 ----- man/cpp.Rd | 1 - src/RcppExports.cpp | 13 ------------- src/survival_scores.cpp | 41 ----------------------------------------- 6 files changed, 65 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index ada0238a7..07043bb42 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -30,7 +30,6 @@ S3method(pecs,list) S3method(plot,LearnerSurv) S3method(plot,TaskDens) S3method(plot,TaskSurv) -export(.c_get_unique_times) export(.c_weight_survival_score) export(.surv_return) export(LearnerDens) diff --git a/R/RcppExports.R b/R/RcppExports.R index 34d44a152..099c82495 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -5,10 +5,6 @@ c_assert_surv <- function(mat) { .Call(`_mlr3proba_c_assert_surv`, mat) } -.c_get_unique_times <- function(true_times, req_times) { - .Call(`_mlr3proba_c_get_unique_times`, true_times, req_times) -} - c_score_intslogloss <- function(truth, unique_times, cdf, eps) { .Call(`_mlr3proba_c_score_intslogloss`, truth, unique_times, cdf, eps) } diff --git a/R/zzz.R b/R/zzz.R index 904a59cf2..356457010 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -13,11 +13,6 @@ NULL #' @export NULL -#' @name .c_get_unique_times -#' @rdname cpp -#' @export -NULL - # nolint start #' @import checkmate #' @import data.table diff --git a/man/cpp.Rd b/man/cpp.Rd index a078c50bc..95132e3b3 100644 --- a/man/cpp.Rd +++ b/man/cpp.Rd @@ -3,7 +3,6 @@ \name{cpp} \alias{cpp} \alias{.c_weight_survival_score} -\alias{.c_get_unique_times} \title{Cpp functions} \description{ Exported internal cpp functions for developers diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index ef1d8c4b1..6ad49c1c4 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -21,18 +21,6 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } -// c_get_unique_times -NumericVector c_get_unique_times(NumericVector true_times, NumericVector req_times); -RcppExport SEXP _mlr3proba_c_get_unique_times(SEXP true_timesSEXP, SEXP req_timesSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< NumericVector >::type true_times(true_timesSEXP); - Rcpp::traits::input_parameter< NumericVector >::type req_times(req_timesSEXP); - rcpp_result_gen = Rcpp::wrap(c_get_unique_times(true_times, req_times)); - return rcpp_result_gen; -END_RCPP -} // c_score_intslogloss NumericMatrix c_score_intslogloss(const NumericVector& truth, const NumericVector& unique_times, const NumericMatrix& cdf, double eps); RcppExport SEXP _mlr3proba_c_score_intslogloss(SEXP truthSEXP, SEXP unique_timesSEXP, SEXP cdfSEXP, SEXP epsSEXP) { @@ -110,7 +98,6 @@ END_RCPP static const R_CallMethodDef CallEntries[] = { {"_mlr3proba_c_assert_surv", (DL_FUNC) &_mlr3proba_c_assert_surv, 1}, - {"_mlr3proba_c_get_unique_times", (DL_FUNC) &_mlr3proba_c_get_unique_times, 2}, {"_mlr3proba_c_score_intslogloss", (DL_FUNC) &_mlr3proba_c_score_intslogloss, 4}, {"_mlr3proba_c_score_graf_schmid", (DL_FUNC) &_mlr3proba_c_score_graf_schmid, 4}, {"_mlr3proba_c_weight_survival_score", (DL_FUNC) &_mlr3proba_c_weight_survival_score, 6}, diff --git a/src/survival_scores.cpp b/src/survival_scores.cpp index 1590ceca2..52c2f396b 100644 --- a/src/survival_scores.cpp +++ b/src/survival_scores.cpp @@ -3,47 +3,6 @@ using namespace Rcpp; using namespace std; -// This function essentially finds and returns the subset of `true_times` that -// align with the requested times (`req_times`), after cleaning up the invalid -// `req_times` (outside of the `true_times` range or duplicate consecutive elements) -// [[Rcpp::export(.c_get_unique_times)]] -NumericVector c_get_unique_times(NumericVector true_times, NumericVector req_times) { - if (req_times.length() == 0) { - return sort_unique(true_times); - } - - std::sort(true_times.begin(), true_times.end()); - std::sort(req_times.begin(), req_times.end()); - - double mintime = true_times(0); - double maxtime = true_times(true_times.length() - 1); - - for (int i = 0; i < req_times.length(); i++) { - if (req_times[i] < mintime || req_times[i] > maxtime || ((i > 1) && req_times[i] == req_times[i - 1])) { - req_times.erase(i); - i--; - } - } - - if (req_times.length() == 0) { - Rcpp::stop("Requested times are all outside the observed range."); - } - for (int i = 0; i < true_times.length(); i++) { - for (int j = 0; j < req_times.length(); j++) { - if (true_times[i] <= req_times[j] && - (i == true_times.length() - 1 || true_times[i + 1] > req_times[j])) { - break; - } else if (j == req_times.length() - 1) { - true_times.erase(i); - i--; - break; - } - } - } - - return true_times; -} - // [[Rcpp::export]] NumericMatrix c_score_intslogloss(const NumericVector& truth, const NumericVector& unique_times,