R/01-constant.R

Defines functions makeLeaf predictConstant learnConstant

Documented in learnConstant makeLeaf predictConstant

# Copyright (C) Kevin R. Coombes, 2025.

## The "Modeler" assumption is that rows are features and columns are samples.
learnConstant <- function(data, status, params, predfun) {
  if (is.null(params$value)) {
    if (mode(data) == "numeric") {
      params$value <- mean(data, na.rm = TRUE)
    } else {
      params$value <- "X"
    }
  }
  FittedModel(predictConstant, data, status,
              details=list(value = params$value))
}

## Repeat: samples are columns.
predictConstant <- function(newdata, details, status, ...) {
  rep(details$value, ncol(newdata))
}

## Should really check that the 'value' is a legitmate symbol
## in the status factor.
makeLeaf <- function(value, status = factor(c("L", "R"))) {
  if (!value %in% levels(status)) {
    warning("Unrecognized level '", value, "' in status factor.\n")
  }
  learn(Modeler(learnConstant, predictConstant, value = value),
        matrix(), status, keepAll)
}

Try the Condens8R package in your browser

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

Condens8R documentation built on May 28, 2025, 3 a.m.