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

## ----toy-data-----------------------------------------------------------------
set.seed(123)
toy_df <- simulate_coxmnar_data(n = 10, beta0 = 0.2, mechanism = "mnar", seed = 123)
head(toy_df, 10)

## ----sim-walkthrough----------------------------------------------------------
set.seed(42)
sim_data <- simulate_coxmnar_data(n = 300, beta0 = 0.2, mechanism = "mnar", seed = 42)

comp <- coxmnar_compare(
  formula = cause ~ Z,
  time = "time",
  data = sim_data,
  methods = c("ai", "ac", "mar", "cc", "full"),
  B = 100L,
  seed = 42
)

summary(comp)

## ----se-comparison------------------------------------------------------------
fit_both <- coxmnar(
  formula = cause ~ Z,
  time = "time",
  data = sim_data,
  method = "ai",
  se_method = "both",
  B = 100L,
  seed = 42
)

cat("Bootstrap SE:", fit_boot_se <- sqrt(fit_both$vcov_boot[1, 1]), "\n")
cat("Asymptotic SE:", fit_asymp_se <- sqrt(fit_both$vcov_asymp[1, 1]), "\n")

## ----pbc-illustration---------------------------------------------------------
data(pbc, package = "survival")
pbc_clean <- na.omit(pbc[, c("time", "status", "bili", "albumin")])
pbc_clean$cause <- ifelse(pbc_clean$status == 2, 1, 0)
pbc_clean$log_bili <- log(pbc_clean$bili)
pbc_clean$log_alb <- log(pbc_clean$albumin)

# Synthetically induce 25% MNAR missingness
set.seed(999)
prob_obs <- 1 / (1 + 0.3 * (pbc_clean$log_bili^(0.1 + 2 * pbc_clean$cause)))
xi <- rbinom(nrow(pbc_clean), 1, prob_obs)
pbc_clean$cause[xi == 0] <- NA

fit_pbc_ai <- coxmnar(
  cause ~ log_bili + log_alb,
  time = "time",
  data = pbc_clean,
  method = "ai",
  B = 100L,
  seed = 999
)

summary(fit_pbc_ai)

## ----pbc-plot, fig.width = 6, fig.height = 4.5--------------------------------
plot(fit_pbc_ai, compare_km = TRUE)

