This function simulates a survival time (e.g., time to event or censoring) for each patient, with an option to specify treatment effect on hazard. It assumes an exponential survival distribution for simplicity.
Arguments
- data
A data frame of patients that includes a treatment group column (typically
Armfromsimulate_allocation). If noArmcolumn is present or only one level, all patients are treated as a single group.- median_time_control
Numeric, median event time for the control group (e.g., median survival in control arm).
- hr
Numeric or named numeric vector specifying hazard ratios for treatment arms relative to control. If a single numeric is given (default = 1), and exactly two arms exist, it is interpreted as the hazard ratio for the treatment arm vs control. If multiple arms or a named vector is provided, each arm's hazard is adjusted accordingly (the control arm should have hazard ratio 1).
- study_duration
Numeric, maximum follow-up time (e.g., study end in time units). If provided, events beyond this time are censored at
study_duration. (Default = NULL, meaning no administrative censoring apart from any later dropout handling.)
Value
The input data frame with two new columns:
EventTime: Simulated event time or censoring time.EventStatus: Event occurrence indicator (1 = event occurred, 0 = censored by end of follow-up).
Details
Exponential survival times are generated. The control arm event rate is derived from median_time_control (rate = log(2)/median). Hazard ratios < 1 indicate longer survival in treatment arms (reduced hazard). For multiple arms, provide hazard ratios as a named vector (names matching Arm levels; the control arm can be omitted or set to 1). If no Arm column is present in data, all patients are assumed to share the same baseline hazard.
Examples
df <- simulate_patients(100)
df <- simulate_allocation(df)
# Simulate survival with median 12 months in control, and hazard ratio 0.75 for treatment
df <- simulate_survival(df, median_time_control = 12, hr = 0.75, study_duration = 24)
head(df[c("Arm","EventTime","EventStatus")])
#> Arm EventTime EventStatus
#> 1 Control 11.67065 1
#> 2 Control 24.00000 0
#> 3 Control 24.00000 0
#> 4 Control 10.11397 1
#> 5 Treatment 24.00000 0
#> 6 Treatment 24.00000 0