# R/pipeline_bagging.R In mlr3pipelines: Preprocessing Operators and Pipelines for 'mlr3'

#### Documented in pipeline_bagging

```#' @include mlr_graphs.R

#' @title Create a bagging learner
#' @name mlr_graphs_bagging
#' @description
#' Creates a [`Graph`] that performs bagging for a supplied graph.
#' This is done as follows:
#' * `Subsample` the data in each step using [`PipeOpSubsample`], afterwards apply `graph`
#' * Replicate this step `iterations` times (in parallel via [multiplicities][Multiplicity])
#' * Average outputs of replicated `graph`s predictions using the `averager`
#'   (note that setting `collect_multipliciy = TRUE` is required)
#'
#' All input arguments are cloned and have no references in common with the returned [`Graph`].
#'
#' @param graph [`PipeOp`] | [`Graph`] \cr
#'   A [`PipeOpLearner`] or [`Graph`] to create a robustifying pipeline for.
#'   Outputs from the replicated `graph`s are connected with the `averager`.
#' @param iterations `integer(1)` \cr
#'   Number of bagging iterations. Defaults to 10.
#' @param frac `numeric(1)` \cr
#'   Percentage of rows to keep during subsampling. See [`PipeOpSubsample`] for
#' @param averager [`PipeOp`] | [`Graph`] \cr
#'   A [`PipeOp`] or [`Graph`] that averages the predictions from the
#'   replicated and subsampled graph's.
#'   In the simplest case, `po("classifavg")` and `po("regravg")` can be used
#'   in order to perform simple averaging of classification and regression
#'   predictions respectively.
#'   If `NULL` (default), no averager is added to the end of the graph.
#'   Note that setting `collect_multipliciy = TRUE` during construction of the averager is required.
#' @return [`Graph`]
#' @export
#' @examples
#' \donttest{
#' library(mlr3)
#' lrn_po = po("learner", lrn("regr.rpart"))
#' gr = pipeline_bagging(lrn_po, 3, averager = po("regravg", collect_multiplicity = TRUE))
#' resample(task, GraphLearner\$new(gr), rsmp("holdout"))
#' }
pipeline_bagging = function(graph, iterations = 10, frac = 0.7, averager = NULL) {
g = as_graph(graph)
assert_count(iterations)
assert_number(frac, lower = 0, upper = 1)
if (!is.null(averager)) {
if (NROW(averager\$input) != 1L || !(grepl("\\[*\\]", x = averager\$input\$train) && grepl("\\[*\\]", x = averager\$input\$predict))) {
stop("'averager' must collect multiplicities.")
}
averager = as_graph(averager, clone = TRUE)
}

po("replicate", param_vals = list(reps = iterations)) %>>!%
po("subsample", param_vals = list(frac = frac)) %>>!%
g %>>!%
averager
}
{"ft_lang_label":"__label__en","ft_lang_prob":0.6706792,"math_prob":0.9639537,"size":2656,"snap":"2022-40-2023-06","text_gpt3_token_len":729,"char_repetition_ratio":0.12971343,"word_repetition_ratio":0.016172506,"special_character_ratio":0.30421686,"punctuation_ratio":0.11709602,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9919854,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-09-27T20:31:38Z\",\"WARC-Record-ID\":\"<urn:uuid:3299f32a-e10d-439d-9063-320bea7e6603>\",\"Content-Length\":\"49302\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:dff0e4e8-5fe6-407a-80af-548bf33a1fd3>\",\"WARC-Concurrent-To\":\"<urn:uuid:d9d1253b-55d5-4a22-88e6-9db97d8f8a32>\",\"WARC-IP-Address\":\"51.81.83.12\",\"WARC-Target-URI\":\"https://rdrr.io/cran/mlr3pipelines/src/R/pipeline_bagging.R\",\"WARC-Payload-Digest\":\"sha1:DQAVHSJMXOO4REV3MGRNFDZY7UTS6B5Z\",\"WARC-Block-Digest\":\"sha1:U4B2M3KNFTE4VFPNDRF5EW742PBXO4YP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030335058.80_warc_CC-MAIN-20220927194248-20220927224248-00294.warc.gz\"}"}