---
title: "Introduction to CoxAalenCR"
author: "Shikhar Tyagi, Arvind Pandey, Bhupendra Singh, Vrijesh Tripathi"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to CoxAalenCR}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 4
)
library(CoxAalenCR)
```

## Overview

The `CoxAalenCR` package implements the additive-multiplicative Cox-Aalen subdistribution hazard regression model for competing risks data as developed by Li and Long (2019). The package offers:

1. **Flexible covariate modeling**: Time-varying non-parametric additive effects via Aalen's model (Aalen, 1980) and constant multiplicative effects via a Cox proportional hazard model.
2. **Covariate-adjusted censoring weights**: Inverse probability of censoring weighting (IPCW) using both Kaplan-Meier weights (Fine and Gray, 1999) and covariate-dependent Cox censoring weights (He et al., 2016; Li and Long, 2019).
3. **Cumulative Incidence Function (CIF) prediction**: Pointwise standard errors and confidence intervals based on asymptotic influence functions.
4. **Goodness-of-fit testing**: Supremum and Cramer-von Mises resampling tests for evaluating constant vs. time-varying covariate effects.
5. **Monte Carlo simulation**: Data generation matching the exact simulation setups in Li and Long (2019).

## Model Specification

Let $T_i = \min(\tilde{T}_i, C_i)$ be the observed time, $\Delta_i = I(\tilde{T}_i \le C_i)$ the censoring indicator, and $\varepsilon_i \in \{1, 2\}$ the cause of failure. The subdistribution hazard for cause 1 (the event of interest) is:

$$\lambda_1(t; X, Z) = (\alpha^T(t)X) \exp(\beta^T Z)$$

where:
- $X$ is a $q \times 1$ vector of additive covariates with time-varying effects $\alpha(t)$,
- $Z$ is a $p \times 1$ vector of multiplicative covariates with constant effects $\beta$.

The cumulative incidence function (CIF) for cause 1 is:

$$F_1(t; X, Z) = 1 - \exp\left\{-\int_0^t (\alpha^T(u)X) \exp(\beta^T Z) du\right\}$$

## Simulating Competing Risks Data

We simulate a dataset under the Cox censoring scenario with 20% censoring:

```{r}
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)
```

## Model Fitting

### Formula Interface

The package provides a formula interface where additive and multiplicative covariates are separated by `|`:

```{r}
fit <- cox_aalen(
  formula = survival::Surv(time, status) ~ X | Z,
  data = dat,
  W = ~ W,
  weight_type = "cox"
)
summary(fit)
```

### Parameter Inference

```{r}
# Multiplicative coefficient
coef(fit)

# Variance-covariance matrix
vcov(fit)

# 95% Confidence interval
confint(fit)
```

## Goodness-of-Fit Test for Time-Varying Effects

We can test whether the effect of covariate $X$ is indeed time-varying using the supremum test:

```{r}
set.seed(123)
test_res <- time_varying_test(fit, B = 100)
print(test_res)
```

## CIF Prediction

We predict cumulative incidence functions for subjects with different covariate values:

```{r}
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))
```

## Cumulative Additive Coefficient Paths

```{r}
plot_cumulative_coef(fit)
```

## Real Data Application: Tamoxifen Breast Cancer Study

The package includes the clinical trial dataset of 641 women aged 50 or older with early breast cancer:

```{r}
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)
```

## References

- Aalen, O. O. (1980). A model for non-parametric regression analysis of counting processes. *Mathematical Statistics and Probability Theory*, 2, 1-25. <doi:10.1007/978-1-4612-6072-1_1>
- Fine, J. P. and Gray, R. J. (1999). A proportional hazards model for the subdistribution of a competing risk. *Journal of the American Statistical Association*, 94(446), 496-509. <doi:10.1080/01621459.1999.10474144>
- Fyles, A. W., McCready, D. R., Manchul, L. A., et al. (2004). Tamoxifen with or without breast irradiation in women 50 years of age or older with early breast cancer. *New England Journal of Medicine*, 351(10), 963-970. <doi:10.1056/NEJMoa040595>
- He, P., Ewell, M. and Scheike, T. H. (2016). A proportional hazards regression model for the subdistribution with covariates-adjusted censoring weight for competing risks data. *Scandinavian Journal of Statistics*, 43(1), 103-122. <doi:10.1111/sjos.12172>
- Li, W. and Long, Y. (2019). An Additive-Multiplicative Cox-Aalen Subdistribution Hazard Model for Competing Risks Data. *Journal of Systems Science and Complexity*, 32(6), 1727-1746. <doi:10.1007/s11424-019-7281-6>
- Martinussen, T. and Scheike, T. H. (2002). A flexible additive multiplicative hazard model. *Biometrika*, 89(2), 283-298. <doi:10.1093/biomet/89.2.283>
- Pintilie, M. (2006). *Competing Risks: A Practical Perspective*. John Wiley & Sons, Chichester. ISBN:978-0-470-87068-6.
- Scheike, T. H. and Zhang, M. J. (2002). An additive-multiplicative Cox-Aalen regression model. *Scandinavian Journal of Statistics*, 29(1), 75-88. <doi:10.1111/1467-9469.00269>
