R/utils.R

Defines functions .tidy_model_call .with_defaults .prob_of_observed .truncate_weights .build_model_formula .get_col_name .check_trunc .polr_method .check_link

# ==========================================================================
# Internal helpers shared by ipwpoint() and ipwtm()
# ==========================================================================

.valid_links <- list(
  binomial = c("logit", "probit", "cauchit", "log", "cloglog"),
  ordinal  = c("logit", "probit", "cauchit", "cloglog")
)

.check_link <- function(family, link) {
  valid <- .valid_links[[family]]
  if (is.null(valid)) return(invisible(TRUE))
  if (is.null(link) || length(link) != 1 || !(link %in% valid)) {
    stop("Invalid 'link' specified for family = \"", family, "\". Must be one of: ",
         paste(sprintf('"%s"', valid), collapse = ", "), call. = FALSE)
  }
  invisible(TRUE)
}

.polr_method <- function(link) {
  switch(link, logit = "logistic", probit = "probit", cloglog = "cloglog", cauchit = "cauchit")
}

.check_trunc <- function(trunc) {
  if (!is.null(trunc) &&
      (!is.numeric(trunc) || length(trunc) != 1 || is.na(trunc) || trunc < 0 || trunc > 0.5)) {
    stop("'trunc' must be a single numeric percentile between 0 and 0.5.", call. = FALSE)
  }
  invisible(TRUE)
}

# Column name from an unquoted symbol or a string
.get_col_name <- function(expr) {
  if (is.symbol(expr) || is.character(expr)) as.character(expr) else deparse(expr, width.cutoff = 500)
}

# Build "lhs ~ rhs" from a one-sided formula, keeping its environment
.build_model_formula <- function(lhs_str, rhs_formula) {
  tt <- stats::terms(rhs_formula)
  rhs_terms <- attr(tt, "term.labels")
  intercept <- attr(tt, "intercept") == 1
  rhs_str <- if (length(rhs_terms) == 0) {
    if (intercept) "1" else "0"
  } else {
    paste(rhs_terms, collapse = " + ")
  }
  if (length(rhs_terms) > 0 && !intercept) rhs_str <- paste(rhs_str, "- 1")
  stats::as.formula(paste(lhs_str, "~", rhs_str), env = environment(rhs_formula))
}

# Truncate weights at the trunc and 1 - trunc quantiles
.truncate_weights <- function(w, trunc) {
  q <- stats::quantile(w, probs = c(trunc, 1 - trunc), na.rm = TRUE, names = FALSE)
  pmin(pmax(w, q[1]), q[2])
}

# Probability of the observed category from predict(<multinom/polr>, type = "probs").
# Columns are matched by level name, never by position. Handles the vector that
# multinom returns for a two-level exposure (P of the second level).
.prob_of_observed <- function(probs, obs, levels_all) {
  if (is.null(dim(probs))) {
    if (length(levels_all) == 2L && length(probs) == length(obs)) {
      probs <- cbind(1 - probs, probs)
    } else {
      probs <- matrix(probs, nrow = length(obs), byrow = TRUE)
    }
    colnames(probs) <- levels_all
  }
  probs <- as.matrix(probs)
  idx <- match(as.character(obs), colnames(probs))
  if (anyNA(idx)) {
    stop("Observed exposure levels do not match fitted probability levels: ",
         paste(unique(as.character(obs)[is.na(idx)]), collapse = ", "), call. = FALSE)
  }
  probs[cbind(seq_len(nrow(probs)), idx)]
}

# Add default arguments unless the user supplied them through `...`
.with_defaults <- function(args, dots, defaults) {
  defaults <- defaults[setdiff(names(defaults), names(dots))]
  c(args, defaults, dots)
}

# Replace data copies that do.call() inlines into a model's call with the
# user's own expressions, so print()/summary() of the model stay readable.
.tidy_model_call <- function(mod, replacements) {
  if (is.null(mod) || is.null(mod$call)) return(mod)
  for (nm in names(replacements)) {
    if (!is.null(mod$call[[nm]]) && !is.null(replacements[[nm]])) {
      mod$call[[nm]] <- replacements[[nm]]
    }
  }
  mod
}

Try the ipw package in your browser

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

ipw documentation built on Nov. 2, 2026, 1:10 a.m.