R/rand_forest-data.R

Defines functions make_rand_forest_orf make_rand_forest_ordinalForest

# These functions define the random forest models.
# They are executed when this package is loaded via `.onLoad()`
# and modify the parsnip package's model environment.

# These functions are tested indirectly when the models are used.
# Since they are added to the parsnip model database on startup execution,
# they can't be test-executed so are excluded from coverage stats.

# nocov start

# ------------------------------------------------------------------------------
# `ordinalForest::ordfor` components

make_rand_forest_ordinalForest <- function() {

  parsnip::set_model_engine("rand_forest", "classification", "ordinalForest")
  parsnip::set_dependency(
    "rand_forest",
    eng = "ordinalForest",
    pkg = "ordinalForest",
    mode = "classification"
  )
  parsnip::set_dependency(
    "rand_forest",
    eng = "ordinalForest",
    pkg = "ordered",
    mode = "classification"
  )

  # dials provided in {dials}
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "ordinalForest",
    parsnip = "mtry",
    original = "mtry",
    func = list(pkg = "dials", fun = "mtry"),
    has_submodel = FALSE
  )
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "ordinalForest",
    parsnip = "trees",
    original = "ntreefinal",
    func = list(pkg = "dials", fun = "trees"),
    has_submodel = FALSE
  )
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "ordinalForest",
    parsnip = "min_n",
    original = "min.node.size",
    func = list(pkg = "dials", fun = "min_n"),
    has_submodel = FALSE
  )

  # engine-specific arguments
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "ordinalForest",
    parsnip = "naive",
    original = "naive",
    func = list(pkg = "ordered", fun = "naive_scores"),
    has_submodel = FALSE
  )
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "ordinalForest",
    parsnip = "nsets",
    original = "nsets",
    func = list(pkg = "ordered", fun = "num_scores"),
    has_submodel = FALSE
  )
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "ordinalForest",
    parsnip = "npermtrial",
    original = "npermtrial",
    func = list(pkg = "ordered", fun = "num_score_perms"),
    has_submodel = FALSE
  )
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "ordinalForest",
    parsnip = "ntreeperdiv",
    original = "ntreeperdiv",
    func = list(pkg = "ordered", fun = "num_score_trees"),
    has_submodel = FALSE
  )
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "ordinalForest",
    parsnip = "nbest",
    original = "nbest",
    func = list(pkg = "ordered", fun = "num_scores_best"),
    has_submodel = FALSE
  )
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "ordinalForest",
    parsnip = "perffunction",
    original = "perffunction",
    func = list(pkg = "ordered", fun = "ord_metric"),
    has_submodel = FALSE
  )

  # Fit information
  parsnip::set_fit(
    model = "rand_forest",
    eng = "ordinalForest",
    mode = "classification",
    value = list(
      interface = "data.frame",
      data = c(x = "x", y = "y"),
      protect = c("x", "y"),
      func = c(pkg = "ordered", fun = "ordinalForest_wrapper"),
      defaults =
        list(
          num.threads = 1,
          perffunction = "probability"
        )
    )
  )

  parsnip::set_encoding(
    model = "rand_forest",
    eng = "ordinalForest",
    mode = "classification",
    options = list(
      predictor_indicators = "none",
      compute_intercept = FALSE,
      remove_intercept = FALSE,
      allow_sparse_x = TRUE
    )
  )

  parsnip::set_pred(
    model = "rand_forest",
    eng = "ordinalForest",
    mode = "classification",
    type = "class",
    value = list(
      pre = NULL,
      post = function(x, object) x$ypred,
      func = c(fun = "predict"),
      args =
        list(
          object = quote(object$fit),
          newdata = quote(new_data)
        )
    )
  )

  parsnip::set_pred(
    model = "rand_forest",
    eng = "ordinalForest",
    mode = "classification",
    type = "prob",
    value = list(
      pre = function(x, object) {
        if (object$fit$perffunction != "probability") {
          cli::cli_abort(
            c(
              "The model fit does not appear to support class probabilities.",
              "i" = "Specify {.code perffunction = 'probability'} for
                     probability predictions."
            ),
            call = rlang::call2("predict")
          )
        }
        x
      },
      post = function(x, object) {
        x <- x$classprobs
        colnames(x) <- object$lvl
        tibble::as_tibble(x)
      },
      func = c(fun = "predict"),
      args =
        list(
          object = quote(object$fit),
          newdata = quote(new_data)
        )
    )
  )
}

# ------------------------------------------------------------------------------
# `orf::orf` components

make_rand_forest_orf <- function() {

  parsnip::set_model_engine("rand_forest", "classification", "orf")
  parsnip::set_dependency(
    "rand_forest",
    eng = "orf",
    pkg = "orf",
    mode = "classification"
  )
  parsnip::set_dependency(
    "rand_forest",
    eng = "orf",
    pkg = "ordered",
    mode = "classification"
  )

  # dials provided in {dials}
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "orf",
    parsnip = "mtry",
    original = "mtry",
    func = list(pkg = "dials", fun = "mtry"),
    has_submodel = FALSE
  )
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "orf",
    parsnip = "trees",
    original = "num.trees",
    func = list(pkg = "dials", fun = "trees"),
    has_submodel = FALSE
  )
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "orf",
    parsnip = "min_n",
    original = "min.node.size",
    func = list(pkg = "dials", fun = "min_n"),
    has_submodel = FALSE
  )

  # engine-specific arguments
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "orf",
    parsnip = "sample.fraction",
    original = "sample.fraction",
    func = list(pkg = "ordered", fun = "sample_fraction"),
    has_submodel = FALSE
  )
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "orf",
    parsnip = "honesty",
    original = "honesty",
    func = list(pkg = "ordered", fun = "honesty"),
    has_submodel = FALSE
  )
  parsnip::set_model_arg(
    model = "rand_forest",
    eng = "orf",
    parsnip = "honesty.fraction",
    original = "honesty.fraction",
    func = list(pkg = "ordered", fun = "honesty_fraction"),
    has_submodel = FALSE
  )

  # Fit information
  parsnip::set_fit(
    model = "rand_forest",
    eng = "orf",
    mode = "classification",
    value = list(
      interface = "data.frame",
      data = c(x = "X", y = "Y"),
      protect = c("X", "Y"),
      func = c(pkg = "ordered", fun = "orf_wrapper"),
      defaults = list()
    )
  )

  parsnip::set_encoding(
    model = "rand_forest",
    eng = "orf",
    mode = "classification",
    options = list(
      predictor_indicators = "traditional",
      compute_intercept = FALSE,
      remove_intercept = TRUE,
      allow_sparse_x = FALSE
    )
  )

  parsnip::set_pred(
    model = "rand_forest",
    eng = "orf",
    mode = "classification",
    type = "class",
    value = list(
      pre = NULL,
      post = orf_class_post,
      func = c(fun = "predict"),
      args =
        list(
          object = quote(object$fit),
          newdata = quote(new_data),
          type = "class"
        )
    )
  )

  parsnip::set_pred(
    model = "rand_forest",
    eng = "orf",
    mode = "classification",
    type = "prob",
    value = list(
      pre = NULL,
      post = orf_prob_post,
      func = c(fun = "predict"),
      args =
        list(
          object = quote(object$fit),
          newdata = quote(new_data),
          type = "probs"
        )
    )
  )
}

# nocov end

Try the ordered package in your browser

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

ordered documentation built on Nov. 23, 2026, 9:08 a.m.