pa_sim_cox_weibull: Simulate Cox survival data with optional interaction terms...

View source: R/pa_sim_data.R

pa_sim_cox_weibullR Documentation

Simulate Cox survival data with optional interaction terms and targeted censoring

Description

Generates survival data under a Cox proportional hazards model with a Weibull baseline cumulative hazard H_0(t) = (0.5 t)^v. Event times are sampled via inverse transform:

Y = H_0^{-1}(-\log U \cdot \exp(-\beta^\top Z)), \quad U \sim \mathrm{Unif}(0,1),

where H_0^{-1}(s) = 2 s^{1/v}.

Covariates are simulated from a standard normal distribution. When interact = FALSE, the design matrix Z contains only main effects. When interact = TRUE, the function automatically detects whether length(beta) equals 3 or 7:

  • If length(beta) = 3, two covariates (x_1, x_2) and their two-way interaction (x_1 x_2) are included.

  • If length(beta) = 7, three covariates (x_1, x_2, x_3), their three two-way interactions (x_1 x_2, x_1 x_3, x_2 x_3), and their three-way interaction (x_1 x_2 x_3) are included.

Right-censoring times are generated as C_i = \exp(\mu + r_i) with r_i \sim \mathcal{N}(0, \mathrm{sd}^2). By default, sd is set to \mathrm{sd}(\log Y), and mu is calibrated so that the expected censoring proportion equals pi_c. If mu and/or sd are supplied, calibration is skipped accordingly:

  • mu and sd provided: use them directly (no calibration).

  • sd only: solve for mu using the given sd.

  • mu only: use the given mu and estimate \mathrm{sd}(\log Y).

  • neither: estimate both \mathrm{sd}(\log Y) and mu.

Usage

pa_sim_cox_weibull(
  n,
  pi_c,
  v,
  beta,
  mu = NULL,
  sd = NULL,
  seed = NULL,
  interact = FALSE,
  nonlinear = FALSE
)

Arguments

n

Integer. Sample size.

pi_c

Numeric in [0, 0.99]. Target censoring proportion. Ignored if both mu and sd are provided.

v

Positive numeric. Weibull shape in H_0(t) = (0.5 t)^v.

beta

Numeric vector of regression coefficients. If interact = FALSE, its length defines the number of main effects. If interact = TRUE, its length must be either 3 (two main + one interaction) or 7 (three main + four interactions).

mu

Optional numeric. Mean of \log C. If supplied with sd, no calibration is performed.

sd

Optional positive numeric. Standard deviation used in \log C = \mu + r_i. If NULL, defaults to \mathrm{sd}(\log Y).

seed

Optional integer seed for reproducibility.

interact

Logical (default FALSE). If TRUE, include pre-defined interaction terms as described above.

nonlinear

Logical. If 'TRUE', generates two covariates plus their interaction and a quadratic term; 'beta' must then have length 4.

Value

A data.frame with columns:

  • time: observed time T_i = \min(Y_i, C_i).

  • status: event indicator 1\{Y_i \le C_i\}.

  • x1, x2, ...: simulated covariates.

  • x1:x2, x1:x3, ...: interaction terms (if interact = TRUE).

  • y_true: latent event time Y_i.

  • cens_time: censoring time C_i.

Attributes include:

  • mu: censoring shift used.

  • sd_log: standard deviation used in \log C.

  • sd_logY: \mathrm{sd}(\log Y) from the generated events.

  • pi_c_target: requested censoring rate.

  • pi_c_observed: realized censoring rate.

  • v, beta: model parameters.

  • interact: logical flag indicating whether interactions were included.

  • nonlinear: logical flag indicating whether polynomial and interactions term were included.

Examples

# (1) Main effects only
set.seed(1)
d0 <- pa_sim_cox_weibull(500, 0.3, v = 1.2, beta = c(0.4, -0.2))

# (2) Two covariates + their interaction
d1 <- pa_sim_cox_weibull(500, 0.3, v = 1.2,
                               beta = c(0.5, -0.3, 0.2), 
                               interact = TRUE)

# (3) Three covariates + all two-way and three-way interactions
b <- c(0.4, -0.2, 0.1, 0.15, -0.05, 0.08, 0.02)
d2 <- pa_sim_cox_weibull(500, 0.3, v = 1.0, beta = b, interact = TRUE)


PAmeasures documentation built on Oct. 29, 2026, 1:08 a.m.