Getting Started with wnpmle

Overview

The wnpmle package implements weighted nonparametric maximum likelihood estimators (wNPMLE) for time-to-event data with competing terminal events, in two large classes of semiparametric transformation models:

  1. Competing risks (type = "cmprsk"): regression for the subdistribution of a competing risk, i.e. the cumulative incidence of the event of interest (Bellach, Kosorok, Rüschendorf and Fine, 2019). The Box–Cox model with \(\rho = 1\) is the Fine–Gray model, in which \(1 - \exp(\beta)\) is the relative change of the subdistribution hazard. The logarithmic model with \(r = 1\) is the proportional odds model, in which \(1 - \exp(\beta)\) is the relative change of the odds of the cumulative incidence.
  2. Recurrent and terminal events (type = "recurrent", the default): regression for the marginal rate of recurrent events in the presence of a competing terminal event such as death (Bellach and Kosorok, 2026). The marginal rate has a one-to-one correspondence with the marginal mean. The Box–Cox model with \(\rho = 1\) is the Ghosh–Lin model, in which \(1 - \exp(\beta)\) is the relative change of the marginal mean. The logarithmic model with \(r = 1\) is the proportional odds model, in which \(1 - \exp(\beta)\) is the relative change of the odds.

The Fine–Gray model for the cumulative incidence and the Ghosh–Lin model for the marginal mean are valid under a proportionality assumption. With competing (terminal) events, this assumption can be violated (Bellach et al., 2019): the cumulative incidence and the marginal mean flatten over time, and a covariate that also affects the competing (terminal) event changes how quickly they flatten, so that its effect changes over time. The Box–Cox and logarithmic transformation models extend these models to two large classes of semiparametric models with non-proportional covariate effects: the transformation parameter determines how the effect of a covariate changes over time, with the Fine–Gray and Ghosh–Lin models (\(\rho = 1\)) and the proportional odds models (\(r = 1\)) as special cases. Because estimation is based on a pseudo-likelihood function, the Takeuchi criterion (TIC) is immediately available for choosing the transformation.

In all transformation models, the cumulative incidence \(F_1(t \mid z) = 1 - \exp[-G\{e^{\beta^T z}\Lambda(t)\}]\) and the marginal mean \(\mu(t \mid z) = G\{e^{\beta^T z}\Lambda(t)\}\) can be predicted directly from the fitted model for any covariate values, with pointwise confidence limits (predict()). This shows the size of covariate effects also for transformations in which the coefficients have no direct interpretation.

Subjects who experience the competing (terminal) event remain in a pseudo-risk set, and their unobserved censoring times are accounted for by inverse probability of censoring weighting (IPCW). This allows direct prediction of the cumulative incidence with competing risks and of the marginal mean with recurrent and terminal events for specific sets of covariates.

Models Link function G(x) Special cases
Box–Cox transformation models (model = "boxcox") \(((1 + x)^\rho - 1)/\rho\) \(\rho = 1\): Fine–Gray (competing risks), Ghosh–Lin (recurrent events); \(\log(1+x)\) as \(\rho \to 0\)
Logarithmic transformation models (model = "log") \(\log(1 + r x)/r\) Proportional odds (\(r = 1\)); \(\rho = 1\) model as \(r \to 0\)

All models are estimated via automatic differentiation using TMB, which provides exact gradients and fast convergence.


Installation

install.packages("wnpmle")

Or from GitHub:

# install.packages("remotes")
remotes::install_github("abellach/wnpmle")

1. Competing risks

Example: liver transplant waiting list

The data survival::transplant (Kim et al., 2006) describe 815 patients on the waiting list for a liver transplant between 1990 and 1999. The event of interest is transplantation; death on the list and withdrawal from the list are competing events. For type = "cmprsk" the data have one row per subject, with status 1 for the event of interest, 2 for a competing event and 0 for censoring. We use the 793 patients with a positive waiting time and complete covariates (age, sex and blood type).

library(wnpmle)
library(survival)
tp <- survival::transplant
tp <- tp[tp$futime > 0 & complete.cases(tp), ]
tp$status <- ifelse(tp$event == "ltx", 1, ifelse(tp$event == "censored", 0, 2))
table(tp$status)
#> 
#>   0   1   2 
#>  74 617 102

The Box–Cox model with \(\rho = 1\) is the Fine–Gray model (Fine and Gray, 1999):

fit_fg <- wnpmle_fit(Surv(futime, status) ~ age + sex + abo, data = tp,
                     type = "cmprsk", model = "boxcox", rho = 1)
summary(fit_fg)
#> 
#> Weighted NPMLE - Competing Risks (subdistribution of event type 1)
#> Model      : BOXCOX transformation ( rho = 1 )
#> Subjects   : 793 
#> Events     : event of interest = 616   competing = 102   censored = 75 
#> Log-lik    : -4336.792 
#> Convergence: relative convergence (4) 
#> 
#> Coefficients:
#>       Estimate     SE z value  Pr(>|z|)
#> age    -0.0056 0.0041  -1.362 1.731e-01
#> sexf    0.0863 0.0836   1.032 3.018e-01
#> aboB   -0.3691 0.1369  -2.697 7.007e-03
#> aboAB  -0.1306 0.2175  -0.600 5.482e-01
#> aboO   -0.5305 0.0907  -5.849 4.960e-09
#> 
#> Cumulative baseline function at time grid:
#>                   Lambda     SE
#> A(tau/4) = 513.75 2.3793 0.5072
#> A(tau/2) = 1027.5 2.7713 0.5933
#> A(tau)   = 2055   2.8562 0.6141

As in Bellach, Kosorok, Rüschendorf and Fine (2019), the censoring weights are stabilised by censoring the last subjects just after tau once the Kaplan–Meier estimate of the censoring distribution drops below 0.05. Here this concerns one transplant, so summary() reports 616 transplants and 75 censored waiting times.

Choosing the transformation

plot_tic() fits both model classes over a grid of transformation parameters and shows AIC (dashed, open circles) and TIC (solid, filled circles) relative to their optimum, with the logarithmic models (\(r\)) to the left and the Box–Cox models (\(\rho\)) to the right of zero (see Section 2 for details of the figure). The results are returned invisibly; saved, they can be plotted again with plot() without refitting.

res_tp <- plot_tic(Surv(futime, status) ~ age + sex + abo, data = tp,
                   type = "cmprsk",
                   rho_grid = seq(0.25, 2.5, by = 0.25),
                   r_grid   = seq(0.25, 2.5, by = 0.25))
plot(res_tp)

TIC selects the logarithmic model with \(\hat r = 1.75\); the largest log-likelihood (smallest AIC) is at \(r = 1.5\). The Fine–Gray model (\(\rho = 1\)) fits clearly worse under both criteria.

fit_tp <- wnpmle_fit(Surv(futime, status) ~ age + sex + abo, data = tp,
                     type = "cmprsk", model = "log", r = 1.75)
summary(fit_tp)
#> 
#> Weighted NPMLE - Competing Risks (subdistribution of event type 1)
#> Model      : LOG transformation ( r = 1.75 )
#> Subjects   : 793 
#> Events     : event of interest = 616   competing = 102   censored = 75 
#> Log-lik    : -4327.294 
#> Convergence: relative convergence (4) 
#> 
#> Coefficients:
#>       Estimate     SE z value  Pr(>|z|)
#> age    -0.0101 0.0075  -1.341 1.799e-01
#> sexf    0.1672 0.1519   1.101 2.710e-01
#> aboB   -0.7342 0.2415  -3.040 2.363e-03
#> aboAB   0.0260 0.3915   0.066 9.471e-01
#> aboO   -1.2514 0.1752  -7.143 9.119e-13
#> 
#> Cumulative baseline function at time grid:
#>                    Lambda      SE
#> A(tau/4) = 513.75 17.9804  7.4529
#> A(tau/2) = 1027.5 27.6336 11.6376
#> A(tau)   = 2055   30.2537 12.8787

Individualized prediction of the cumulative incidence

For competing risks, predict() returns the cumulative incidence of the event of interest with pointwise 95% confidence limits. The figure shows the cumulative incidence of transplantation for a 50-year-old man with blood type A or O under the selected model (solid, with confidence bands) and under the Fine–Gray model (dashed), and the difference between blood types A and O.

plot() draws the predicted curves with their confidence bands; add = TRUE adds the curves of a second model as dashed lines.

newdat <- data.frame(age = 50, sex = factor("m", levels = levels(tp$sex)),
                     abo = factor(c("A", "O"), levels = levels(tp$abo)))
times  <- seq(0, fit_tp$tau, length.out = 500)
p_tp   <- predict(fit_tp, newdata = newdat, times = times)
p_fg   <- predict(fit_fg, newdata = newdat, times = times)
yrs    <- times / 365.25
gcol   <- c("black", "grey55")                  # blood type A, O

op <- par(mfrow = c(1, 2), mar = c(4, 4, 1, 0.8), mgp = c(2.4, 0.7, 0), tcl = -0.3)
plot(p_tp, xscale = 365.25, type = "l", ylim = c(0, 1), xaxs = "i", yaxs = "i",
     legend = FALSE, xlab = "Years on the waiting list",
     ylab = "Cumulative incidence of transplant")
plot(p_fg, add = TRUE, xscale = 365.25, type = "l")
legend("bottomright", bty = "n", cex = 0.75, seg.len = 3,
       legend = c("A, log (r = 1.75)", "A, Fine–Gray", "O, log (r = 1.75)", "O, Fine–Gray"),
       col = gcol[c(1, 1, 2, 2)], lty = c(1, 2, 1, 2), lwd = c(2, 1.5, 2, 1.5))

d_tp <- p_tp$cif_1 - p_tp$cif_2
d_fg <- p_fg$cif_1 - p_fg$cif_2
plot(yrs, d_tp, type = "l", lwd = 2, xaxs = "i", ylim = range(0, d_tp, d_fg) * c(1, 1.08),
     xlab = "Years on the waiting list", ylab = "Difference A - O")
lines(yrs, d_fg, lwd = 1.5, lty = 2, col = "grey40")
abline(h = 0, lty = 3, col = "grey60")
legend("topright", bty = "n", cex = 0.75, seg.len = 3,
       legend = c("Log (r = 1.75)", "Fine–Gray"), lty = c(1, 2), lwd = c(2, 1.5),
       col = c("black", "grey40"))

par(op)

Under the selected model, the advantage of blood type A over O rises to about 25 percentage points within the first months and then decreases to about 13 points. The Fine–Gray model, with its constant subdistribution hazard ratio, underestimates the early difference and overestimates the long-term difference (about 17 points).


2. Recurrent and terminal events

For type = "recurrent" (the default) the data have one row per recurrent event (status 1) and, for each subject, a final row for the terminal event (status 2) or censoring (status 0); the subject identifier is given by id.

Quick start: bladder cancer data

The bladder cancer data from the Veterans Administration Cooperative Urological Research Group were previously analyzed by Ghosh and Lin (2002) and by Zeng and Lin (2006). bladder_prep() prepares the 86 patients treated with thiotepa or placebo from survival::bladder1, with treatment, the number of tumors and the size of the largest tumor at baseline as covariates. The Box–Cox model with \(\rho = 1\), i.e. the identity link \(G(x) = x\), is the Ghosh–Lin model, which is a special case of the weighted NPMLE (Bellach and Kosorok, 2026).

bdata <- bladder_prep()
fit_bladder <- wnpmle_fit(
  Surv(time, status) ~ treat + num + size,
  data  = bdata,
  id    = "id",
  model = "boxcox",
  rho   = 1,
  tau   = 59
)
fit_bladder
#> 
#> Weighted NPMLE - Recurrent Events with Competing Terminal Event
#> Type       : recurrent 
#> Model      : BOXCOX transformation ( rho = 1 )
#> Subjects   : 86 
#> Events     : recurrent = 132   terminal = 22   censored = 64 
#> Log-lik    : -683.5759 
#> Convergence: relative convergence (4) 
#> 
#> Coefficients:
#>       Estimate     SE z value Pr(>|z|)
#> treat  -0.5363 0.2684  -1.998  0.04570
#> num     0.1727 0.0587   2.940  0.00328
#> size   -0.0051 0.0680  -0.074  0.94080

Example: hospital readmissions after colorectal cancer surgery

The readmission data (Gonzalez et al., 2005) contain repeated hospital readmissions of 403 patients after surgery for colorectal cancer, with death as competing terminal event (109 deaths, 27%). readmission_prep() returns one row per readmission (status 1) and a final row for death (status 2) or censoring (status 0). Time is measured in days since surgery. The covariates chemotherapy, sex, Dukes’ tumor stage and the Charlson comorbidity index are taken at baseline. This is the analysis of Bellach and Kosorok (2026), with follow-up until the end of the study.

rdata <- readmission_prep()
head(rdata)
#>   id time status      chemo    sex dukes charlson
#> 1  1   24      1    Treated Female     D        3
#> 2  1  457      1    Treated Female     D        3
#> 3  1 1037      0    Treated Female     D        3
#> 4  2  489      1 NonTreated   Male     C        0
#> 5  2 1182      0 NonTreated   Male     C        0
#> 6  3   15      1 NonTreated   Male     C        3
table(rdata$status)
#> 
#>   0   1   2 
#> 294 458 109

Choosing the transformation

plot_tic() fits both model classes over a grid of transformation parameters and plots AIC and TIC, with the logarithmic models (\(r\)) to the left and the Box–Cox models (\(\rho\)) to the right of the dotted line at zero. Both criteria are shown relative to their optimum (\(-\Delta\mathrm{AIC}/2\), dashed, and \(-\Delta\mathrm{TIC}/2\), solid; 0 is best); the dotted line at \(-1\) marks a difference of 2. All transformations have the same number of parameters, so AIC compares log-likelihoods only, whereas the TIC penalty accounts for the dependence within patients and the estimated inverse probability of censoring weights, and can differ between transformations. The two classes meet at zero: \(r \to 0\) is the Ghosh–Lin model (\(\rho = 1\)) and \(\rho \to 0\) the proportional odds model (\(r = 1\)); these models are marked by points (open for AIC, filled for TIC).

The grid below is that of Bellach and Kosorok (2026): steps of 0.1, refined around the optimum. Every grid point needs a fit with sandwich standard errors, so the grid search takes several minutes. The results are returned invisibly; saved, they can be plotted again with plot() without refitting.

res <- plot_tic(
  Surv(time, status) ~ chemo + sex + dukes + charlson,
  data     = rdata,
  id       = "id",
  rho_grid = sort(unique(round(c(seq(0.1, 1.2, by = 0.1), seq(0.28, 0.46, by = 0.01)), 2))),
  r_grid   = sort(unique(round(c(seq(0.1, 1.2, by = 0.1), seq(0.30, 0.48, by = 0.01)), 2)))
)
plot(res, file = "tic_readmission.pdf")      # redraws the figure without refitting

TIC selects the Box–Cox model with \(\hat\rho = 0.37\) and the logarithmic model with \(\hat r = 0.39\), which are practically equivalent. The Ghosh–Lin model (\(\rho = 1\)) is 3.67 TIC units worse and is therefore not supported by TIC, whereas AIC, which ignores the dependence within patients and the estimated weights, does not distinguish it from the best model.

Fitting the selected model

fit_rd <- wnpmle_fit(
  Surv(time, status) ~ chemo + sex + dukes + charlson,
  data  = rdata,
  id    = "id",
  model = "boxcox",
  rho   = 0.37,
  se    = "sandwich_adj"
)
summary(fit_rd)
#> 
#> Weighted NPMLE - Recurrent Events with Competing Terminal Event
#> Type       : recurrent 
#> Model      : BOXCOX transformation ( rho = 0.37 )
#> Subjects   : 403 
#> Events     : recurrent = 458   terminal = 109   censored = 294 
#> Log-lik    : -3064.339 
#> Convergence: relative convergence (4) 
#> 
#> Coefficients:
#>              Estimate     SE z value Pr(>|z|)
#> chemoTreated  -0.5730 0.2038  -2.812 0.004922
#> sexFemale     -0.6472 0.2110  -3.068 0.002158
#> dukesC         0.2060 0.2181   0.944 0.344900
#> dukesD         0.7421 0.2704   2.744 0.006065
#> charlson1-2    1.0495 0.3798   2.763 0.005719
#> charlson3      0.6156 0.2501   2.461 0.013840
#> 
#> Cumulative baseline mean at time grid:
#>                 Lambda     SE
#> A(tau/4) = 544  0.9343 0.1980
#> A(tau/2) = 1088 1.4850 0.3108
#> A(tau)   = 2176 6.2039 1.3934
TIC(fit_rd)
#> [1] 7066.763

A positive coefficient increases and a negative coefficient decreases the expected number of readmissions at all times. In the Ghosh–Lin model (\(\rho = 1\)), \(\exp(\beta)\) is the ratio of the marginal means; in the proportional odds model (\(r = 1\)), \(\exp(\beta)\) is the ratio of \(\exp(\mu(t)) - 1\), where \(\mu(t)\) is the marginal mean. For other transformation models, the size of the effect depends on time and is best illustrated with predict().

Cumulative baseline mean

baseline() returns the cumulative baseline mean with pointwise 95% confidence limits.

bl <- baseline(fit_rd)
plot(bl$time, bl$Lambda, type = "s", lwd = 2,
     xlab = "Days since surgery", ylab = expression(hat(Lambda)(t)),
     ylim = range(c(bl$lower, bl$upper), na.rm = TRUE))
lines(bl$time, bl$lower, type = "s", lty = 2, col = "grey50")
lines(bl$time, bl$upper, type = "s", lty = 2, col = "grey50")

Individualized prediction of the marginal mean

predict() gives the marginal mean at new covariate values. The figure shows the expected number of readmissions within three years for a man with Dukes’ stage D and Charlson index \(\ge 3\), without and with chemotherapy, under the selected model (\(\rho = 0.37\), solid, with confidence bands) and under the Ghosh–Lin model (\(\rho = 1\), dashed). For a comparison with other approaches, see Bellach and Kosorok (2026).

newd <- data.frame(chemo    = factor(c("NonTreated", "Treated"), levels = levels(rdata$chemo)),
                   sex      = factor("Male", levels = levels(rdata$sex)),
                   dukes    = factor("D",    levels = levels(rdata$dukes)),
                   charlson = factor("3",    levels = levels(rdata$charlson)))
fit_gl <- wnpmle_fit(Surv(time, status) ~ chemo + sex + dukes + charlson,
                     data = rdata, id = "id", model = "boxcox", rho = 1, se = "none")
tt   <- seq(0, 365 * 3, by = 1)
p_rd <- predict(fit_rd, newdata = newd, times = tt)
p_gl <- predict(fit_gl, newdata = newd, times = tt)

plot(p_rd, legend = FALSE, xaxs = "i", yaxs = "i",
     xlab = "Days since surgery", ylab = "Expected number of readmissions")
plot(p_gl, add = TRUE)
legend("topleft", bty = "n", cex = 0.75, seg.len = 3,
       legend = c(expression("No chemotherapy, " * rho == 0.37),
                  "No chemotherapy, Ghosh–Lin",
                  expression("Chemotherapy, " * rho == 0.37),
                  "Chemotherapy, Ghosh–Lin"),
       col = c("black", "black", "grey55", "grey55"),
       lty = c(1, 2, 1, 2), lwd = c(2, 1.5, 2, 1.5))


Standard errors

Value Description
"sandwich_adj" Sandwich variance estimator with correction for the estimated censoring weights (default)
"sandwich" Sandwich variance estimator without the correction
"fisher" Inverse Fisher information
"none" No standard errors; faster, useful for profiling

All standard errors are computed with TMB and are fast also for large data sets.


S3 methods and helpers

Function Description
print(fit) Compact coefficient table with z-values and p-values
summary(fit) Adds the cumulative baseline at tau/4, tau/2, tau, or at chosen times (times = ...)
coef(fit) Named coefficient vector
vcov(fit) Full variance-covariance matrix for (beta, Lambda)
logLik(fit), AIC(fit), BIC(fit) Log-likelihood and information criteria
TIC(fit) Takeuchi’s information criterion
baseline(fit) Cumulative baseline mean with pointwise confidence limits
predict(fit, newdata) Marginal mean (recurrent events) or cumulative incidence (competing risks) at new covariate values
plot_loglik(), plot_tic() AIC and TIC over the transformation parameter
plot(res) Redraw the results of plot_loglik() or plot_tic() without refitting
bladder_prep(), readmission_prep() Example data sets

References

Bellach, A. and Kosorok, M.R. (2026). Pseudo-likelihood regression for the marginal rate of recurrent events with a competing terminal event. arXiv preprint arXiv:2605.25934. doi:10.48550/arXiv.2605.25934.

Bellach, A., Kosorok, M.R., Rüschendorf, L. and Fine, J.P. (2019). Weighted NPMLE for the subdistribution of a competing risk. Journal of the American Statistical Association, 114(525), 259–270. doi:10.1080/01621459.2017.1401540.

Fine, J.P. and Gray, R.J. (1999). A proportional hazards model for the subdistribution of a competing risk. Journal of the American Statistical Association, 94(446), 496–509. doi:10.1080/01621459.1999.10474144.

Ghosh, D. and Lin, D.Y. (2002). Marginal regression models for recurrent and terminal events. Statistica Sinica, 12, 663–688. doi:10.17615/pt0g-y207.

Gonzalez, J.R., Fernandez, E., Moreno, V., Ribes, J., Peris, M., Navarro, M., Cambray, M. and Borras, J.M. (2005). Sex differences in hospital readmission among colorectal cancer patients. Journal of Epidemiology and Community Health, 59(6), 506–511. doi:10.1136/jech.2004.028902.

Kim, W.R., Therneau, T.M., Benson, J.T., Kremers, W.K., Rosen, C.B., Gores, G.J. and Dickson, E.R. (2006). Deaths on the liver transplant waiting list: an analysis of competing risks. Hepatology, 43(2), 345–351.

Zeng, D. and Lin, D.Y. (2006). Semiparametric transformation models with random effects for recurrent events. Biometrika, 93(3), 627–640. doi:10.1093/biomet/93.3.627.