R/LearnerClustMclust.R

#' @title Gaussian Mixture Model Clustering Learner
#'
#' @name mlr_learners_clust.mclust
#' @include LearnerClust.R
#'
#' @description
#' Gaussian mixture model-based clustering.
#' Calls [mclust::Mclust()] from package \CRANpkg{mclust}.
#'
#' The predict method uses [mclust::predict.Mclust()] to compute the cluster memberships for new data.
#'
#' @section Initial parameter values:
#' - `verbose`:
#'   - Actual default: `interactive()`.
#'   - Adjusted default: `FALSE`.
#'   - Reason for change: Suppress progress output during training.
#' - `warn`:
#'   - Actual default: `mclust.options("warn")`, which is `FALSE` by default.
#'   - Adjusted default: `FALSE`.
#'   - Reason for change: Suppress warnings during training independently of the `mclust` global options.
#'
#' @templateVar id clust.mclust
#' @template learner
#'
#' @references
#' `r format_bib("scrucca2016mclust", "fraley2002model")`
#'
#' @export
#' @template seealso_learner
#' @template example
LearnerClustMclust = R6Class(
  "LearnerClustMclust",
  inherit = LearnerClust,
  public = list(
    #' @description
    #' Creates a new instance of this [R6][R6::R6Class] class.
    initialize = function() {
      param_set = ps(
        G = p_uty(default = 1:9, tags = "train", custom_check = check_numeric),
        modelNames = p_uty(
          default = NULL,
          tags = "train",
          custom_check = crate(function(x) check_character(x, null.ok = TRUE))
        ),
        prior = p_uty(default = NULL, tags = "train", custom_check = crate(function(x) check_list(x, null.ok = TRUE))),
        control = p_uty(tags = "train", custom_check = check_list),
        initialization = p_uty(
          default = NULL,
          tags = "train",
          custom_check = crate(function(x) check_list(x, null.ok = TRUE))
        ),
        warn = p_lgl(default = FALSE, tags = "train"),
        x = p_uty(
          default = NULL,
          tags = "train",
          custom_check = crate(function(x) check_class(x, "mclustBIC", null.ok = TRUE))
        ),
        verbose = p_lgl(default = FALSE, tags = "train")
      )

      param_set$set_values(warn = FALSE, verbose = FALSE)

      super$initialize(
        id = "clust.mclust",
        feature_types = c("logical", "integer", "numeric"),
        predict_types = c("partition", "prob"),
        param_set = param_set,
        properties = c("partitional", "fuzzy", "complete"),
        packages = "mclust",
        man = "mlr3cluster::mlr_learners_clust.mclust",
        label = "Gaussian Mixture Model"
      )
    }
  ),

  private = list(
    .train = function(task) {
      pv = self$param_set$get_values(tags = "train")
      with_package("mclust", {
        m = invoke(mclust::Mclust, data = task$data(), .args = pv, .opts = allow_partial_matching)
      })
      if (self$save_assignments) {
        self$assignments = as.integer(m$classification)
      }
      m
    },

    .predict = function(task) {
      predictions = invoke(
        predict,
        self$model,
        newdata = ordered_features(task, self),
        .opts = allow_partial_matching
      )
      partition = as.integer(predictions$classification)
      prob = NULL
      if (self$predict_type == "prob") {
        prob = predictions$z
      }
      list(partition = partition, prob = prob)
    }
  )
)

#' @include zzz.R
register_learner("clust.mclust", LearnerClustMclust)

Try the mlr3cluster package in your browser

Any scripts or data that you put into this service are public.

mlr3cluster documentation built on Sept. 17, 2026, 5:09 p.m.