R/validation_powers.R

Defines functions validate_fp_power_list normalize_fp_power_vector

#' Normalize One Candidate FP Power Vector
#'
#' Validates and normalizes one vector of candidate fractional-polynomial base
#' powers. The input represents candidate base powers, not a selected FP power
#' vector. Duplicate values are removed because repeated selected powers are
#' generated later by the FP candidate generator for degree-2 and higher FP
#' models.
#'
#' @param powers Numeric vector of candidate FP base powers.
#' @param context Character scalar used in error messages.
#'
#' @return Sorted unique numeric vector of finite candidate powers.
#'
#' @keywords internal
#' @noRd
normalize_fp_power_vector <- function(powers, context = "`powers`") {
  if (!is.numeric(powers)) {
    stop(
      paste0(context, " must be a numeric vector."),
      call. = FALSE
    )
  }
  
  if (length(powers) == 0L || anyNA(powers) || any(!is.finite(powers))) {
    stop(
      paste0(
        context,
        " must contain at least one finite, non-missing numeric value."
      ),
      call. = FALSE
    )
  }
  
  sort(unique(as.numeric(powers)))
}

#' Validate and Normalize a Named Candidate FP Power List
#'
#' Validates a named list of candidate fractional-polynomial base-power sets and
#' returns a complete named list, one element per predictor. The \code{powers}
#' argument defines candidate base powers, not selected FP power vectors.
#' Duplicate values are removed.
#'
#' The optional \code{df} argument is accepted for compatibility with older
#' internal callers, but no df-dependent power-set restriction is needed:
#' power \code{1} is a valid searched FP1 candidate at every requested degree.
#'
#' @param powers User-supplied powers argument. Must be \code{NULL} or a named
#'   list of numeric vectors.
#' @param vnames Character vector of valid predictor names.
#' @param default_powers Numeric vector of default candidate powers.
#' @param arg_name Character scalar used in error messages.
#' @param df Optional numeric scalar or vector aligned with \code{vnames}.
#'   Accepted and ignored for compatibility with older internal callers.
#'
#' @return Named list of normalized candidate-power vectors, one element per
#'   predictor in \code{vnames}.
#'
#' @keywords internal
#' @noRd
validate_fp_power_list <- function(powers,
                                   vnames,
                                   default_powers = c(-2, -1, -0.5, 0, 0.5, 1, 2, 3),
                                   arg_name = "powers",
                                   df = NULL) {
  if (!is.character(vnames) || length(vnames) == 0L ||
      anyNA(vnames) || any(!nzchar(vnames))) {
    stop(
      "`vnames` must be a non-empty character vector of predictor names.",
      call. = FALSE
    )
  }
  
  # No df-dependent candidate-power validation is required. In particular,
  # the singleton set p = 1 remains valid for a searched FP1 class.
  if (!is.null(df)) {
    invisible(df)
  }
  
  default_powers <- normalize_fp_power_vector(
    default_powers,
    context = "`default_powers`"
  )
  
  power_list <- stats::setNames(
    replicate(length(vnames), default_powers, simplify = FALSE),
    vnames
  )
  
  if (is.null(powers)) {
    return(power_list)
  }
  
  if (!is.list(powers)) {
    stop(
      paste0("! `", arg_name, "` must be a named list."),
      call. = FALSE
    )
  }
  
  pow_names <- names(powers)
  
  if (is.null(pow_names) || length(pow_names) != length(powers) ||
      anyNA(pow_names) || any(!nzchar(pow_names))) {
    stop(
      paste0("! Every element of `", arg_name, "` must have a non-empty name."),
      call. = FALSE
    )
  }
  
  if (anyDuplicated(pow_names)) {
    dup <- unique(pow_names[duplicated(pow_names)])
    stop(
      paste0(
        "! `", arg_name, "` must not contain duplicated names. ",
        "Duplicated name(s): ",
        paste(dup, collapse = ", "),
        "."
      ),
      call. = FALSE
    )
  }
  
  unknown_names <- setdiff(pow_names, vnames)
  
  if (length(unknown_names) > 0L) {
    stop(
      paste0(
        "! The following names in `", arg_name,
        "` do not match any predictor column: ",
        paste(unknown_names, collapse = ", "),
        "."
      ),
      call. = FALSE
    )
  }
  
  powers_norm <- stats::setNames(
    vector("list", length(powers)),
    pow_names
  )
  
  for (nm in pow_names) {
    powers_norm[[nm]] <- normalize_fp_power_vector(
      powers[[nm]],
      context = paste0("`", arg_name, "[[\"", nm, "\"]]`")
    )
  }
  
  power_list[names(powers_norm)] <- powers_norm
  
  
  power_list
}

Try the mfp2 package in your browser

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

mfp2 documentation built on Nov. 14, 2026, 1:06 a.m.