---
title: "Generalized Competing Event Modeling with gcemod"
author: "gcemod"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Generalized Competing Event Modeling with gcemod}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4)
library(gcemod)
```

## What gcemod does

`gcemod` estimates covariate effects on **omega+** --- the ratio of the hazard
for an *event of interest* to the hazard for a *competing event* --- on either
the cause-specific (Cox) or subdistribution (Fine-Gray) scale. Confidence
intervals and p-values come from the **Lunn-McNeil** stacked-model construction.
Per-subject omega = omega+/(1+omega+) is reported as a descriptive quantity so
model-predicted values can be compared with observed ones.

The workflow: (1) fit the GCE model, (2) build a risk score, (3) find cutpoints
that maximize the omega+ difference between groups, (4) draw alligator plots of
cumulative incidence by group, and (5) check calibration of predicted vs.
observed omega+.

## Two contrasting cohorts

The package bundles two competing-risks example datasets that illustrate
contrasting GCE applications. They are provided for illustration only and do not
represent real patients. In `hn` (head and neck) the event of interest (cancer
recurrence) is *more* frequent than the competing event (omega+ > 1). In `prostate`
the event of interest (distant metastasis or prostate-cancer death) is *less* frequent
than the competing event (other-cause death) (omega+ < 1). Both use `status`:
0 = censored, 1 = event of interest, 2 = competing event, with `time` in years.

```{r data}
data(hn)
table(hn$status)
```

### 1. Fit the GCE model (cause-specific)

Models are specified with a formula whose response is `Surv(time, status)`.

```{r cox}
fit <- gcecox(Surv(time, status) ~ age + smoker + t_cat + n_cat + p16,
              data = hn, t = 5)
fit
```

Printing gives a short display. `summary()` returns the full coefficient table
as an object:

```{r summary}
s <- summary(fit)
s
```

Each p-value tests whether that covariate shifts omega+ (event of interest vs.
competing event); the omnibus test asks the same jointly. The usual extractors
are available:

```{r extract}
coef(fit)
confint(fit, exponentiate = TRUE)
```

The Fine-Gray version uses the same interface:

```{r fg, eval = FALSE}
fit_fg <- gcefg(Surv(time, status) ~ age + smoker + t_cat + n_cat + p16,
                data = hn, t = 5)
summary(fit_fg)
```

### 2. Risk score and predictions

The risk score is the linear predictor centered at the covariate means, so the
average subject has score 0. `predict()` returns it, and the predicted omega+
and omega at any time, for the fitting data or for new subjects.

```{r rs}
new <- hn[1:3, ]
predict(fit, new, type = "lp")
predict(fit, new, type = "ratio", times = c(2, 5))   # omega+(t)
predict(fit, new, type = "share")                    # omega at t = 5
```

### 3. Cutpoints maximizing the omega+ difference

```{r cut}
cuts <- gce_cutpoints(fit, groups = 2, method = "optimal")
cuts
```

### 4. Alligator plot (cumulative incidence by risk group)

Risk groups are distinguished by color and event types by line type (event of
interest solid, competing event dashed) on a single panel. The display
functions return objects; `plot()` draws them.

```{r alligator, fig.height=4}
plot(gce_alligator(fit, groups = cuts))
```

### 5. Calibration of predicted vs. observed omega+

```{r calib}
cal <- gce_calibration(fit, which = "omegaplus", groups = 5)
cal
plot(cal)
```

### 6. Is the relative hazard constant over time?

```{r omegatime}
plot(gce_omegatime(fit, which = "omegaplus", groups = cuts, xmax = 10))
```

### Discrimination

```{r perf}
gce_performance(fit)
```

## The competing-dominant contrast: prostate

Running the same workflow on `prostate` shows the opposite regime. Here the
competing event (other-cause death) dominates, so omega+ ratios and the risk
score point the other way, and the alligator jaws open with the competing-event
CIF on top.

```{r prostate, eval = FALSE}
data(prostate)
fit_p <- gcecox(Surv(time, status) ~ age + psa + gleason + t2b + comorbidity,
                data = prostate, t = 5)
summary(fit_p)
plot(fit_p, which = "alligator", groups = 2)
```

## The matrix interface

Earlier versions took the follow-up times, a two-column data frame of event
indicators, and a covariate data frame. That interface is still available and
gives the same fit:

```{r legacy}
Ind <- data.frame(event     = as.integer(hn$status == 1),
                  competing = as.integer(hn$status == 2))
Cov <- hn[, c("age", "smoker", "t_cat", "n_cat", "p16")]
fit_m <- gcecox(hn$time, Ind, Cov, t = 5)
all.equal(coef(fit_m), coef(fit))
```

## References

Lunn M, McNeil D (1995) Applying Cox regression to competing risks.
*Biometrics* 51:524--32.

Carmona R, et al. (2014) Validated competing event model for the stage I-II
endometrial cancer population. *Int J Radiat Oncol Biol Phys* 89:888--98.

Carmona R, et al. (2016) Improved method to stratify elderly patients with
cancer at risk for competing events. *J Clin Oncol* 34:1270--77.

Mell LK, et al. (2019) Nomogram to predict the benefit of intensive treatment
for locoregionally advanced head and neck cancer. *Clin Cancer Res*
25:7078--7088.

Zakeri K, et al. (2020) Predictive classifier for intensive treatment of head
and neck cancer. *Cancer* 126:5263--5273.

Mell LK, et al. (2024) Effects of androgen deprivation therapy on prostate
cancer outcomes according to competing event risk. *Eur Urol* 85:373--381.
