## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 4
)
library(CoxAalenCR)

## -----------------------------------------------------------------------------
set.seed(123)
dat <- simulate_coxaalen(
  n = 150,
  p = 0.3,
  alpha = 1.0,
  beta1 = 1.0,
  beta2 = 1.0,
  censoring = "cox",
  cens_rate = 0.20
)
head(dat)
table(dat$status)

## -----------------------------------------------------------------------------
fit <- cox_aalen(
  formula = survival::Surv(time, status) ~ X | Z,
  data = dat,
  W = ~ W,
  weight_type = "cox"
)
summary(fit)

## -----------------------------------------------------------------------------
# Multiplicative coefficient
coef(fit)

# Variance-covariance matrix
vcov(fit)

# 95% Confidence interval
confint(fit)

## -----------------------------------------------------------------------------
set.seed(123)
test_res <- time_varying_test(fit, B = 100)
print(test_res)

## -----------------------------------------------------------------------------
new_profiles <- data.frame(
  X = c(0.2, 0.8),
  Z = c(0.5, 0.5)
)

pred <- predict(fit, newdata = new_profiles, se.fit = TRUE)

# Plot predicted CIF curves
plot_cif(fit, newdata = cbind(1, new_profiles$X), newZ = matrix(new_profiles$Z, ncol = 1))

## -----------------------------------------------------------------------------
plot_cumulative_coef(fit)

## -----------------------------------------------------------------------------
data(tamoxifen)
head(tamoxifen)
table(tamoxifen$status)

# Fit Cox-Aalen model with age and treatment in additive part and pathsize in multiplicative part
fit_tam <- cox_aalen(
  formula = survival::Surv(time, status) ~ age + treatment | pathsize,
  data = tamoxifen,
  W = ~ age,
  weight_type = "cox"
)
summary(fit_tam)

