Comparison of mean cumulative count curves via the area under the curve (AUC)

Zachary R. McCaw
Updated: 2025-06-15

knitr::opts_chunk$set(cache = TRUE)
library(dplyr)

Description

This package provides functions for inference on the difference and ratio in AUCs comparing two mean cumulative count (MCC) curves. The MCC curves are estimated using an approach based on the method of Ghosh and Lin (2000), which accounts for the presence of terminal competing risks. Also see:

Installation

```{R, eval=FALSE} remotes::install_github("zrmacc/MCC", build_vignettes = TRUE)

## Methods

### Estimation of the mean cumulative count curve

For each study arm, the MCC is estimated as follows. Define $N(\cdot)$ as the counting process for events of interest, both terminal and non-terminal, $Y(\cdot)$ as the number of subjects who remain at risk, and $S(\cdot)$ as the probability of not having experienced a terminal event. The MCC $\mu(t)$ at time $t$ is estimated by:

$$
\mu(t) = \int_{0}^{t}\ \hat{S}(u)\ \frac{ dN(u) }{ Y(u) }
$$

Here $\hat{S}(u)$ is the Kaplan-Meier estimate of the probability of being terminal event-free, estimated from *all* terminal events, both those of interest and those regarded as a competing risk; $dN(u)$ is the number of events of interest, both non-terminal and terminal, occurring at time $u$; and $Y(u)$ is the number of subjects who remain at risk, which are subjects who have neither been censored nor experienced a terminal event.

### Standard error calibration

See the [calibration vignette](https://github.com/zrmacc/MCC/blob/master/vignettes/calibration.pdf).

### Data

The function `GenData` simulates example data in the format expected by this package. The recurrent event times are generated from a Poisson process that continues until censoring or death, whichever occurs first. Optionally, a gamma `frailty_variance` may be specified such that the patient-specific event and death rates are correlated. The example data includes 100 patients in each of the treatment and control arms. The maximum duration of follow-up is `tau = 4` (e.g. years). The rate of recurrent events for patients in the treatment arm is 80% the rate for patients in the control arm. 

```{R}
library(MCC)
covariates <- data.frame(
  arm = c(rep(1, 100), rep(0, 100))
)
data <- MCC::GenData(
  beta_event = c(log(0.8)),
  covariates = covariates,
  frailty_variance = 0.2,
  tau = 4
)
head(data)

The essential data are:

For analyzing other data sets, arm and status should have the same coding. Each subject should experience an observation-terminating event, i.e. either death or censoring.

The example data also include:

Observation-terminating events

In contrast to the time to first event setting, in the multiple or recurrent events setting, a subject may remain at risk after experiencing an event of interest. An observation-terminating event, either censoring or the occurrence of a competing risk, is therefore necessary to remove a subject from the risk set. Conversely, a subject who lacks an observation-terminating event is implicitly assumed to remain at risk indefinitely. If a subject lacks an observation-terminating event, then by default CompareAUCs will add a censoring time immediately after their last event of interest. For example, if the data for subject 1 were:

df <- data.frame(
  idx = c(1, 1, 1),
  time = c(2, 3, 5),
  status = c(1, 1, 1)
)
show(df)

then, for analysis, the subject is assumed to have been censored after the last event, as in the following:

df <- data.frame(
  idx = c(1, 1, 1, 1),
  time = c(2, 3, 5, 5),
  status = c(1, 1, 1, 0)
)
show(df)

If a subject who lacks an observation-terminating event should, in fact, remain at risk indefinitely, set cens_after_last = FALSE.

Terminal events of interest

Suppose the endpoint of interest includes a fatal event. One such endpoint is heart failure hospitalization (HFH) or cardiovascular (CV)-death. In this setting, it becomes necessary to distinguish non-fatal events of interest (e.g. HFH), after which the subject remains in the risk set, from fatal events of interest (e.g. CV-death), after which the subject is removed from the risk set. To achieve this, a fatal event of interest should be recorded using two records. The first, with status = 1, indicates that an event of interest has occurred. The second, with status = 2, indicates that the event was terminal. For example, the following data indicate that subject 1 had 3 events of interest, and that the 3rd event, occurring at time = 5, was terminal.

df <- data.frame(
  idx = c(1, 1, 1, 1),
  time = c(2, 3, 5, 5),
  status = c(1, 1, 1, 2)
)
show(df)

By contrast, the following data indicate that subject 2 had 3 events of interest, none of which was terminal:

df <- data.frame(
  idx = c(2, 2, 2),
  time = c(2, 3, 5),
  status = c(1, 1, 1)
)
show(df)

Note that, by default, subject 2 is assumed to have been censored after their 3rd event of interest, as in the following:

df <- data.frame(
  idx = c(2, 2, 2, 2),
  time = c(2, 3, 5, 5),
  status = c(1, 1, 1, 0)
)
show(df)

Although censoring (status = 0) and a terminal event (status = 2) both remove a subject from the risk set, there is an important distinction. Censoring leaves open the possibility that the subject experienced more events of interest in the future, whereas a terminal event precludes the possibility of any future events of interest.

Analyses

Single-arm AUC

To calculate the areas under the mean cumulative count curve for a single arm up to time $\tau = 4$:

auc <- MCC::SingleArmAUC(
  data %>% dplyr::filter(arm == 0),
  boot = TRUE,
  reps = 200,
  tau = 4
)
show(auc)

AUCs

To compare the AUCs of two treatment arms up to time $\tau = 4$:

aucs <- MCC::CompareAUCs(
  data,
  tau = 4,
  boot = TRUE,
  perm = TRUE,
  reps = 200,
  alpha = 0.05
)
show(aucs)

Here:

Weighted Analysis

Weights may be supplied to control the size of the jump in the cumulative count curve at each event time (i.e. each time with status == 1). The following example weights each event by how many events a patient has experienced. For example, if a patient has 3 events before censoring, the first contributes a jump of size 1, the second a jump of size 2, and the third a jump of size 3. Other weighting schemes are of course possible. Note that the weights assigned to censoring (status == 0) and terminal event (status == 2) records are not used, and may be set to any value.

data <- data %>%
  dplyr::group_by(idx) %>%
  dplyr::mutate(weights = dplyr::row_number()) %>%
  dplyr::ungroup()

cat("Visualization of weights for the first 10 records.\n")
data %>%
  dplyr::select(idx, time, status, weights) %>%
  dplyr::slice(1:10)
aucs <- MCC::CompareAUCs(
  data,
  tau = 4,
  alpha = 0.05,
  weights = data$weights
)
show(aucs)

Stratified Analysis

CompareAUCs also allows for stratified analysis. Consider a data set, similar to that described previously, but with the additional of a binary stratification factor. The event rate for individuals in stratum 1 is increased by 20%.

# Generate data with strata.
covariates <- data.frame(
  arm = c(rep(1, 100), rep(0, 100)),
  strata = stats::rbinom(200, 1, 0.25)
)
data <- MCC::GenData(
  beta_event = c(log(0.8), log(1.2)),
  covariates = covariates,
  frailty_variance = 0.2,
  tau = 4
)

# Stratified AUC analysis.
aucs <- MCC::CompareAUCs(
  data,
  strata = data$strata,
  tau = 4,
  boot = TRUE,
  perm = TRUE,
  reps = 200,
  alpha = 0.05
)
show(aucs)

Outputs

The output of CompareAUCs is an object with these slots.

aucs@StratumAreas
aucs@MargAreas
aucs@CIs
head(aucs@MCF)
aucs@Pvals

Adjusted AUCs

The previous estimator allows for stratification, but a different approach is needed to accommodate continuous covariates. If covariates are provided, then CompareAUCs uses an augmentation estimator to adjust for differences between the treatment groups. Note that strata and covariates should not both be provided. If adjustment for both is needed, use model.matrix to generate a design matrix including both covariates and stratum indicators, e.g. model.matrix(~ 0 + covar + strata, data = data), then supply the design matrix covar argument.

set.seed(100)

# Generate data with a continuous covariate.
n <- 1000
covariates <- data.frame(
  arm = c(rep(1, n), rep(0, n)),
  x1 = c(stats::rnorm(n, mean = -1), stats::rnorm(n, mean = 1)),
  x2 = c(stats::rnorm(n, mean = 1), stats::rnorm(n, mean = -1))
)
data <- MCC::GenData(
  beta_event = c(log(0.5), log(0.8), log(1.2)),
  covariates = covariates,
  base_death_rate = 0.25,
  base_event_rate = 1,
  frailty_variance = 0.2,
  tau = 4
)

# Unadjusted.
paste("Unadjusted AUCs:")
unadj_aucs <- MCC::CompareAUCs(
  data,
  tau = 4,
  alpha = 0.05
)
show(unadj_aucs)

# Adjusted.
paste("Adjusted AUCs:")
adj_aucs <- MCC::CompareAUCs(
  data,
  tau = 4,
  alpha = 0.05,
  covar = data %>% dplyr::select(x1, x2)
)
show(adj_aucs)

Plotting

See the plotting vignette.



zrmacc/MCC documentation built on July 16, 2025, 4:04 p.m.