## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5
)

## -----------------------------------------------------------------------------
library(exnexSurv)
library(survival)

set.seed(42)
d <- simulate_data(
  n = 20,
  beta = 0.5,
  sigma = 1.1,
  outlier_baskets = c(2, 8),
  resist_delta = -1.0,
  censoring_rate = 0.3,
  seed = 1
)

head(d)

## -----------------------------------------------------------------------------
attr(d, "true_theta")
attr(d, "true_beta")
attr(d, "true_sigma")

## -----------------------------------------------------------------------------
fit <- exnex_surv(
  Surv(time, event) ~ group + x1,
  data = d,
  iter = 2000,
  warmup = 1000,
  chains = 2,
  seed = 7
)
print(fit, show_trace = FALSE)

## -----------------------------------------------------------------------------
curves <- survival_curves(fit)
head(curves)

## ----fig.show = "hold"--------------------------------------------------------
plot(curves)

## -----------------------------------------------------------------------------
nd <- data.frame(group = d$group[1:3], x1 = c(0, 0.2, -0.1))
curves_nd <- survival_curves(fit, newdata = nd)
table(curves_nd$group)

## -----------------------------------------------------------------------------
median_survival(fit)

## -----------------------------------------------------------------------------
rmst(fit, tmax = 10)

## -----------------------------------------------------------------------------
w <- compute_waic(fit)
str(w[c("waic", "se_elpd_waic", "lpd", "p_waic", "elpd_waic")])

## -----------------------------------------------------------------------------
fit_group <- exnex_surv(
  Surv(time, event) ~ group,
  data = d,
  iter = 2000, warmup = 1000, chains = 2, seed = 7
)

compare_waic(without_covariate = fit_group, with_covariate = fit)

## -----------------------------------------------------------------------------
# Median survival
probability_superiority(fit, a = 1, b = 3, function_of = "median")

# Survival probability at t = 3
probability_superiority(fit, a = 1, b = 2, function_of = "survival", times = 3)

# RMST up to t = 10
probability_superiority(fit, a = 1, b = 2, function_of = "rmst", tmax = 10)

