inst/doc/wnpmle.R

## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse  = TRUE,
  comment   = "#>",
  fig.width = 6,
  fig.height = 4.5
)

## ----eval = FALSE-------------------------------------------------------------
# install.packages("wnpmle")

## ----eval = FALSE-------------------------------------------------------------
# # install.packages("remotes")
# remotes::install_github("abellach/wnpmle")

## ----tp-data------------------------------------------------------------------
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)

## ----tp-fg--------------------------------------------------------------------
fit_fg <- wnpmle_fit(Surv(futime, status) ~ age + sex + abo, data = tp,
                     type = "cmprsk", model = "boxcox", rho = 1)
summary(fit_fg)

## ----tp-tic, eval = FALSE-----------------------------------------------------
# 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)

## ----tp-tic-figure, echo = FALSE, out.width = "70%", fig.align = "center"-----
knitr::include_graphics("tic_transplant.png")

## ----tp-fit-------------------------------------------------------------------
fit_tp <- wnpmle_fit(Surv(futime, status) ~ age + sex + abo, data = tp,
                     type = "cmprsk", model = "log", r = 1.75)
summary(fit_tp)

## ----tp-predict, fig.width = 7.5, fig.height = 3.5----------------------------
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)

## ----quick--------------------------------------------------------------------
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

## ----readmission-data---------------------------------------------------------
rdata <- readmission_prep()
head(rdata)
table(rdata$status)

## ----tic-plot, eval = FALSE---------------------------------------------------
# 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-figure, echo = FALSE, out.width = "70%", fig.align = "center"--------
knitr::include_graphics("tic_readmission.png")

## ----fit-readmission----------------------------------------------------------
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)
TIC(fit_rd)

## ----baseline-----------------------------------------------------------------
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")

## ----predict------------------------------------------------------------------
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))

Try the wnpmle package in your browser

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

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