## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

## ----install-cmdstan, eval = FALSE--------------------------------------------
# install.packages("cmdstanr", repos = c("https://stan-dev.r-universe.dev",
#                                         getOption("repos")))
# cmdstanr::install_cmdstan()

## ----install-pkg, eval = FALSE------------------------------------------------
# # install.packages("remotes")
# remotes::install_github("Sergio-Bejar/stanSPsurv")

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

## ----pooled-results, eval = FALSE---------------------------------------------
# # Posterior summaries for all parameters
# summary(fit_pooled)
# 
# # Survival-stage coefficients only
# coef(fit_pooled, component = "survival")
# 
# # Split-stage coefficients only
# coef(fit_pooled, component = "split")

## ----coef-plot, eval = FALSE, fig.width = 6, fig.height = 3.5-----------------
# plot(fit_pooled)

## ----surv-curves, eval = FALSE, fig.width = 7, fig.height = 4.5---------------
# profiles <- data.frame(age = c(50, 65, 80), sex = 1)
# 
# plot_survival(
#   fit_pooled,
#   newdata = profiles,
#   times   = seq(1, 1000, length.out = 100),
#   labels  = c("Age 50", "Age 65", "Age 80"),
#   cols    = c("steelblue", "grey30", "firebrick")
# )

## ----suscept, eval = FALSE, fig.width = 6, fig.height = 4---------------------
# plot_susceptibility(fit_pooled, variable = "age", data = lung)

## ----predict, eval = FALSE----------------------------------------------------
# # Susceptibility
# predict(fit_pooled, newdata = profiles, type = "susceptibility")
# 
# # Survival at specific times
# predict(fit_pooled, newdata = profiles, type = "survival",
#         times = c(100, 365, 730))

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

## ----frailty, eval = FALSE----------------------------------------------------
# # Posterior summaries of group-level effects
# frailty(fit_exch, stage = "survival")
# frailty(fit_exch, stage = "split")

## ----frailty-plot, eval = FALSE, fig.width = 6, fig.height = 5----------------
# plot_frailty(fit_exch, stage = "survival")

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

## ----choropleth, eval = FALSE, fig.width = 8, fig.height = 5------------------
# plot_choropleth(fit_sp, stage = "survival",
#                 regions = c("Colombia", "Venezuela", "Ecuador",
#                             "Peru", "Bolivia", "Brazil"))

## ----choropleth-sf, eval = FALSE----------------------------------------------
# plot_choropleth(fit_sp, stage = "survival",
#                 sf_map = my_sf_object, sf_key = "region_id")

## ----counting-process, eval = FALSE-------------------------------------------
# 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
# )

## ----diagnostics, eval = FALSE------------------------------------------------
# # Divergences and treedepth
# fit_pooled$fit$diagnostic_summary()
# 
# # Trace plots (requires bayesplot)
# # bayesplot::mcmc_trace(fit_pooled$fit$draws("beta"))
# 
# # LOO-CV (requires loo)
# # loo::loo(fit_pooled$fit$draws("log_lik"))

