R/gcefg.R

Defines functions .gcefg_H .gcefg_fit gcefg.default gcefg.formula gcefg .crr_H

Documented in gcefg gcefg.default gcefg.formula

# Internal: subdistribution cumulative hazard from a crr prediction matrix.
# pc is the matrix returned by predict.crr (column 1 = time, remaining columns
# = CIF for each covariate row). Returns a length(times) x nsubject matrix of
# H(t) = -log(1 - F(t)), read directly at the requested times (no extrapolation).
.crr_H <- function(pc, times) {
  tt  <- pc[, 1]
  cif <- pc[, -1, drop = FALSE]
  idx <- findInterval(times, tt)
  idx[idx < 1L] <- 1L
  cift <- cif[idx, , drop = FALSE]
  cift <- pmin(pmax(cift, 0), 1 - 1e-12)
  -log(1 - cift)
}

#' Generalized competing event model via Fine-Gray regression
#'
#' @description
#' Fits a generalized competing event (GCE) model on the subdistribution hazard
#' scale and estimates covariate effects on \eqn{\tilde\omega^{+}} using a
#' Lunn-McNeil-style stacked Fine-Gray model (see \code{\link{lunnmcneil}} with
#' \code{type = "finegray"}), giving confidence intervals, Wald tests, and an
#' omnibus test from one joint model. A GCE risk score is formed from the
#' model linear predictor.
#'
#' @details
#' The interface, the role of \code{t}, and the returned object are as for
#' \code{\link{gcecox}}. Predicted absolute \eqn{\tilde\omega^{+}(t)} values
#' are obtained from two separate Fine-Gray fits
#' (\code{\link[cmprsk]{crr}}), stored as \code{$fit1} and \code{$fit2}.
#'
#' @inheritParams gcecox
#' @param se standard-error method passed to \code{\link{lunnmcneil}}:
#'   \code{"robust"} (default; cluster-robust on subject, censoring weights
#'   treated as fixed, which errs on the conservative side) or \code{"boot"}
#'   (subject bootstrap that re-estimates the censoring weights within each
#'   resample, so the variance additionally accounts for their estimation, at
#'   higher computational cost).
#' @param B number of bootstrap resamples when \code{se = "boot"} (default
#'   \code{200}).
#' @param ... passed to \code{\link[survival]{coxph}} for the stacked model.
#'
#' @return An object of class \code{c("gcefg", "gcemod")} with the same
#'   components and methods as a \code{\link{gcecox}} fit, on the
#'   subdistribution scale; see \code{\link{gcemod-methods}}.
#'
#' @examples
#' data(hn)
#' fit <- gcefg(Surv(time, status) ~ age + smoker + p16, data = hn, t = 5)
#' summary(fit)
#' @seealso \code{\link{gcecox}}, \code{\link{lunnmcneil}},
#'   \code{\link{gcemod-methods}}
#' @export
gcefg <- function(formula, ...) {
  if (missing(formula)) {
    r <- .gce_legacy_redispatch(sys.call(), parent.frame())
    if (!is.null(r)) return(r$value)
  }
  UseMethod("gcefg")
}

#' @rdname gcefg
#' @export
gcefg.formula <- function(formula, data, subset, na.action, t = NULL,
                          event = NULL, competing = NULL, conf.level = 0.95,
                          standardize = FALSE, se = c("robust", "boot"),
                          B = 200, contrasts = NULL, model = TRUE, ...) {
  se <- match.arg(se)
  cl <- match.call(); cl[[1L]] <- quote(gcefg)
  d <- .gce_design_formula(formula, match.call(expand.dots = FALSE),
                           parent.frame(), event, competing, contrasts)
  .gcefg_fit(d, t = t, conf.level = conf.level, standardize = standardize,
             se = se, B = B, model = model, call = cl, ...)
}

#' @rdname gcefg
#' @export
gcefg.default <- function(formula, Ind, Cov, M = NULL, t = NULL,
                          conf.level = 0.95, standardize = FALSE,
                          se = c("robust", "boot"), B = 200, ...) {
  se <- match.arg(se)
  cl <- match.call(); cl[[1L]] <- quote(gcefg)
  .gce_M_deprecated(M)
  Time <- formula
  d <- .gce_design_default(Time, .gce_ind_cause(Time, Ind), Cov)
  .gcefg_fit(d, t = t, conf.level = conf.level, standardize = standardize,
             se = se, B = B, model = TRUE, call = cl, ...)
}

.gcefg_fit <- function(d, t, conf.level, standardize, se, B, model, call, ...) {
  t <- .gce_default_t(d, t)
  lm.fit <- .lm_engine(d, type = "finegray", conf.level = conf.level,
                       standardize = standardize, se_method = se, B = B, ...)
  obj <- .gce_hazard_object(d, lm.fit, t, conf.level, standardize, model,
                            call, method = "Fine-Gray (subdistribution)")
  ## The shared constructor labels the ratio column for the cause-specific
  ## scale; this fit reports the subdistribution ratio.
  colnames(obj$result)[1L] <- "exp(coef) (omega-tilde+ ratio)"
  Xd <- obj$design

  ## Per-subject subdistribution cumulative hazards (via crr), the basis of the
  ## predicted absolute omega+(t).
  obj$fit1 <- suppressWarnings(cmprsk::crr(d$time, d$cause, Xd, failcode = 1, cencode = 0))
  obj$fit2 <- suppressWarnings(cmprsk::crr(d$time, d$cause, Xd, failcode = 2, cencode = 0))
  H <- .gcefg_H(obj, Xd, t)
  obj$omegaplus <- as.numeric(H$H1 / H$H2)
  obj$omega     <- as.numeric(H$H1 / (H$H1 + H$H2))
  structure(obj, class = c("gcefg", "gcemod"))
}

# Subdistribution cumulative hazards of both causes for the rows of X at
# 'times': a list of two length(times) x nrow(X) matrices.
.gcefg_H <- function(object, X, times) {
  pc1 <- stats::predict(object$fit1, as.matrix(X))
  pc2 <- stats::predict(object$fit2, as.matrix(X))
  list(H1 = .crr_H(pc1, times), H2 = .crr_H(pc2, times))
}

Try the gcemod package in your browser

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

gcemod documentation built on Dec. 1, 2026, 1:06 a.m.