The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.
Bayesian fits use the same Stan likelihood as MLE
but sample from the posterior with rstan::sampling.
Brief exploratory context for the larynx data (see Getting started for full EDA):
library(spsurv)
library(generics)
library(KMsurv)
library(survival)
library(ggplot2)
data(larynx)
larynx$stage <- factor(larynx$stage)km_stage <- survfit(Surv(time, delta) ~ stage, data = larynx)
km_long <- data.frame(
time = km_stage$time,
surv = km_stage$surv,
stage = rep(levels(larynx$stage), km_stage$strata)
)
ggplot(km_long, aes(x = time, y = surv, color = stage)) +
geom_step(linewidth = 0.6) +
labs(x = "Time (years)", y = "Survival probability", color = "Stage") +
theme_bw() +
theme(legend.position = "bottom")fit <- bpph(
Surv(time, delta) ~ age + stage,
degree = 5,
data = larynx,
approach = "bayes",
iter = 400,
warmup = 200,
chains = 1,
cores = 1,
init = 0,
priors = list(
beta = c("normal(0,4)"),
gamma = c("lognormal(0,4)")
)
)
#>
#> SAMPLING FOR MODEL 'spbp' NOW (CHAIN 1).
#> Chain 1:
#> Chain 1: Gradient evaluation took 5.2e-05 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.52 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1:
#> Chain 1:
#> Chain 1: Iteration: 1 / 400 [ 0%] (Warmup)
#> Chain 1: Iteration: 40 / 400 [ 10%] (Warmup)
#> Chain 1: Iteration: 80 / 400 [ 20%] (Warmup)
#> Chain 1: Iteration: 120 / 400 [ 30%] (Warmup)
#> Chain 1: Iteration: 160 / 400 [ 40%] (Warmup)
#> Chain 1: Iteration: 200 / 400 [ 50%] (Warmup)
#> Chain 1: Iteration: 201 / 400 [ 50%] (Sampling)
#> Chain 1: Iteration: 240 / 400 [ 60%] (Sampling)
#> Chain 1: Iteration: 280 / 400 [ 70%] (Sampling)
#> Chain 1: Iteration: 320 / 400 [ 80%] (Sampling)
#> Chain 1: Iteration: 360 / 400 [ 90%] (Sampling)
#> Chain 1: Iteration: 400 / 400 [100%] (Sampling)
#> Chain 1:
#> Chain 1: Elapsed Time: 0.063 seconds (Warm-up)
#> Chain 1: 0.043 seconds (Sampling)
#> Chain 1: 0.106 seconds (Total)
#> Chain 1:
summary(fit)
#> Call:
#> bpph(formula = Surv(time, delta) ~ age + stage, degree = 5L,
#> data = larynx, approach = "bayes", iter = 400, warmup = 200,
#> chains = 1, cores = 1, init = 0, priors = list(beta = c("normal(0,4)"),
#> gamma = c("lognormal(0,4)")), model = "ph")
#>
#> Bayesian Bernstein PH model (degree = 5):
#> Regression coefficients:
#> Estimate 2.5% 97.5% Std. Error
#> age 0.0189 -0.0064 0.0467 0.0
#> stage2 0.0978 -0.9101 1.0516 0.5
#> stage3 0.6286 0.0146 1.3254 0.4
#> stage4 1.7080 0.7091 2.5156 0.4
#>
#> Exponentiated coefficients:
#> Estimate 2.5% 97.5%
#> age 1.02 0.99 1.0
#> stage2 1.25 0.37 2.6
#> stage3 2.00 0.80 3.5
#> stage4 6.08 2.03 12.4
#>
#> ---
#> DIC = 295 WAIC = -148td_b <- tidy(fit, conf.int = TRUE, exponentiate = TRUE)
hpd <- credint(fit, prob = 0.95, type = "HPD")
td_b$hpd.low <- exp(hpd[, 1])
td_b$hpd.high <- exp(hpd[, 2])
td_b$term <- factor(td_b$term, levels = rev(td_b$term))
ggplot(td_b, aes(x = estimate, y = term)) +
geom_vline(xintercept = 1, linetype = "dashed", color = "grey50") +
geom_pointrange(aes(xmin = hpd.low, xmax = hpd.high), linewidth = 0.4, color = "steelblue") +
labs(x = "Hazard ratio (posterior median)", y = NULL, title = "95% HPD intervals") +
theme_bw()td_m <- tidy(fit_mle, conf.int = TRUE, exponentiate = TRUE)
cmp <- merge(
td_m[, c("term", "estimate", "conf.low", "conf.high")],
td_b[, c("term", "estimate", "hpd.low", "hpd.high")],
by = "term",
suffixes = c("_mle", "_bayes")
)
cmp_long <- rbind(
data.frame(term = cmp$term, method = "MLE", low = cmp$conf.low, high = cmp$conf.high,
est = cmp$estimate_mle),
data.frame(term = cmp$term, method = "Bayes", low = cmp$hpd.low, high = cmp$hpd.high,
est = cmp$estimate_bayes)
)
cmp_long$term <- factor(cmp_long$term, levels = rev(unique(cmp$term)))
ggplot(cmp_long, aes(x = est, y = term, color = method)) +
geom_vline(xintercept = 1, linetype = "dashed", color = "grey50") +
geom_pointrange(aes(xmin = low, xmax = high), position = position_dodge(width = 0.5), linewidth = 0.4) +
labs(x = "Hazard ratio", y = NULL, color = NULL) +
theme_bw()Posterior intervals show whether data overwhelm priors. Agreement between MLE and Bayes supports data-driven conclusions; wide HPDs flag weak information.
adapt_delta, more iter).Install bayesplot for trace and pairs plots:
Always inspect divergences, split R-hat, and ESS before interpreting results.
Install posterior and tidybayes for
long-format draws and interval plots. After
library(spsurv), spread_draws() and
tidy_draws() dispatch on Bayes fits when
tidybayes is loaded.
library(tidybayes)
dr <- as_draws_df.spbp(fit)
spread_draws(fit, `beta[age]`)
#> # A tibble: 200 x 4
#> .chain .iteration .draw `beta[age]`
#> <int> <int> <int> <dbl>
#> 1 1 1 1 0.00732
#> 2 1 2 2 0.0242
#> 3 1 3 3 0.0288
#> 4 1 4 4 0.00398
#> 5 1 5 5 0.0285
#> 6 1 6 6 0.00172
#> 7 1 7 7 -0.00993
#> 8 1 8 8 0.0250
#> 9 1 9 9 0.0239
#> 10 1 10 10 0.00896
#> # i 190 more rowsDraw-level survival curves:
head(spread_surv_draws.spbp(fit, times = c(1, 2, 3), newdata = larynx[1, ]))
#> stage time age diagyr delta .chain .iteration .draw time surv id
#> 1 1 0.6 77 76 1 1 1 1 1 0.8990325 1
#> 1.1 1 0.6 77 76 1 1 1 1 2 0.8294656 1
#> 1.2 1 0.6 77 76 1 1 1 1 3 0.7703621 1
#> 1.3 1 0.6 77 76 1 1 2 2 1 0.9002921 1
#> 1.4 1 0.6 77 76 1 1 2 2 2 0.8073166 1
#> 1.5 1 0.6 77 76 1 1 2 2 3 0.7264237 1See vignette("tidymodels", package = "spsurv") for
workflows and parsnip engines.
Posterior mean survival is a Bernstein polynomial in time, so the
curve is smooth (geom_line). Kaplan–Meier
in the EDA plot above remains a step function.
pr <- predict(fit, times = seq(0, max(larynx$time), length.out = 121))
head(pr)
#> id time surv lower upper cumhaz std.err
#> 1 1 0.00000000 1.0000000 1.0000000 1.0000000 0.00000000 0.000000000
#> 2 1 0.08916667 0.9891783 0.9820701 0.9954463 0.01088641 0.003363782
#> 3 1 0.17833333 0.9786260 0.9654140 0.9911488 0.02162729 0.006439199
#> 4 1 0.26750000 0.9683257 0.9491618 0.9862236 0.03223225 0.009250955
#> 5 1 0.35666667 0.9582612 0.9352173 0.9821201 0.04271075 0.011822230
#> 6 1 0.44583333 0.9484173 0.9208919 0.9768465 0.05307218 0.014174726
ggplot(pr, aes(x = time, y = surv)) +
geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.2, colour = NA) +
geom_line(linewidth = 0.6) +
labs(x = "Time (years)", y = "Survival probability") +
theme_bw()See the Survival prediction and ggplot vignette for covariate-specific ribbons.
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.