stanSPsurv fits Bayesian split-population (cure)
survival models using Stan. In a standard survival model every unit is
at risk of experiencing the event. Split-population models relax that
assumption: a latent mixture separates units that are
susceptible to the event from those that are immune
(cured). The package estimates both stages jointly—the probability of
susceptibility and the duration until the event—while optionally
accounting for group-level or spatial heterogeneity through
frailties.
Three model families are available:
| Function | Structure | Frailties |
|---|---|---|
stan_pooled_spsurv() |
Pooled | None |
stan_exchange_spsurv() |
Exchangeable | iid normal per group |
stan_spatial_spsurv() |
Spatial CAR | BYM2 proper-CAR per area |
Each supports Weibull or log-logistic baseline hazards and both
Surv(time, event) and counting-process
Surv(t0, time, event) responses.
stanSPsurv requires a working CmdStan installation.
Install CmdStan once via cmdstanr:
install.packages("cmdstanr", repos = c("https://stan-dev.r-universe.dev",
getOption("repos")))
cmdstanr::install_cmdstan()Then install stanSPsurv from GitHub:
The simplest specification has no group-level heterogeneity. We use
the lung dataset from the survival package as
a quick illustration.
library(survival)
library(stanSPsurv)
data(lung)
lung <- na.omit(lung[, c("time", "status", "age", "sex")])
fit_pooled <- stan_pooled_spsurv(
formula = Surv(time, status) ~ age + sex,
split_formula = ~ age + sex,
data = lung,
form = "weibull",
chains = 4,
parallel_chains = 4,
iter_warmup = 500,
iter_sampling = 500,
seed = 123
)plot() produces a forest plot of posterior means and 90
% credible intervals for both stages, omitting intercepts by
default:
plot_survival() draws posterior-mean survival curves
with pointwise credible bands for one or more covariate profiles:
plot_susceptibility() shows how the posterior-mean
probability of being susceptible varies with one covariate, holding the
others at their sample means:
predict() returns posterior-mean susceptibility
probabilities or survival probabilities for new covariate profiles:
When observations are grouped (e.g., by country, hospital, or region), the exchangeable specification adds iid normal frailties for both the survival and split stages:
lung$inst <- factor(lung$inst)
fit_exch <- stan_exchange_spsurv(
formula = Surv(time, status) ~ age + sex,
split_formula = ~ age,
data = lung,
group = inst,
form = "weibull",
chains = 4,
parallel_chains = 4,
iter_warmup = 500,
iter_sampling = 500,
seed = 123
)For areal data with a known neighbourhood structure, the spatial specification uses a scaled proper-CAR (BYM2) prior. You supply a symmetric binary adjacency matrix:
# Example with simulated data and 6 regions on a chain graph
N <- 300; J <- 6
d <- data.frame(
time = rexp(N),
event = rbinom(N, 1, 0.6),
x = rnorm(N),
area = factor(rep(1:J, length.out = N))
)
A <- matrix(0, J, J)
for (j in 1:(J - 1)) { A[j, j + 1] <- 1; A[j + 1, j] <- 1 }
fit_sp <- stan_spatial_spsurv(
formula = Surv(time, event) ~ x,
split_formula = ~ x,
data = d,
area = area,
adjacency = A,
form = "weibull",
chains = 4,
parallel_chains = 4,
iter_warmup = 500,
iter_sampling = 500,
seed = 123
)plot_choropleth() maps posterior-mean frailties onto a
world map using the maps package. Pass a character vector
of region names matching the maps::map("world")
database:
plot_choropleth(fit_sp, stage = "survival",
regions = c("Colombia", "Venezuela", "Ecuador",
"Peru", "Bolivia", "Brazil"))For arbitrary spatial geometries (municipalities, districts), use the
sf back-end instead:
For time-varying covariates in panel data, use the counting-process
format Surv(t0, time, event) together with the
spell argument. Each row represents one interval within a
spell; the model evaluates susceptibility once per spell:
fit_cp <- stan_pooled_spsurv(
formula = Surv(t0, duration, failure) ~ x1 + x2,
split_formula = ~ z1,
data = panel_data,
spell = panel_data$spell_id,
form = "weibull",
chains = 4,
parallel_chains = 4
)Split-formula covariates must be constant within each spell.
The underlying CmdStan fit is accessible via fit$fit,
which exposes all standard CmdStan diagnostics: