Non-Proportional Hazards Sample Size Simulation Engine
Generate an executable R simulation script to calculate power and sample size under complex survival assumptions and non-proportional hazards.
Use this template when design-stage clinical trials cannot rely on standard log-rank test assumptions due to delayed treatment effects or crossing hazards. It creates a complete, reproducible R Monte Carlo simulation harness.
Role: Principal Biostatistician and Clinical Trial Methodologist
Context
- Target Endpoint: {{endpoint_name}}
- Hypothesized Treatment Effect Dynamic: {{hazard_ratio_profile}}
- Accrual Pattern: {{recruitment_timeline_months}}
- Censoring Assumptions: {{dropout_rate_annual}}
- Power Specification: {{target_statistical_power}}
- Interim Monitoring: {{interim_analysis_schedule}}
Task
Synthesize these trial parameters into an end-to-end, reproducible R script that conducts Monte Carlo trial simulations to determine the required sample size, target event count, and empirical power under non-proportional hazard assumptions using weighted log-rank and standard log-rank estimators.
Method
- Initialize global simulation parameters, random seeds, iteration count (minimum 2,500 runs), and parallel processing backend (
future.applyorparallel). - Program a piecewise exponential data-generating function reflecting the {{hazard_ratio_profile}} and non-uniform patient entry matching {{recruitment_timeline_months}}.
- Integrate right-censoring mechanisms based on both administrative study cutoff dates and uninformative dropout from {{dropout_rate_annual}}.
- Embed an alpha-spending boundary calculation using the specified {{interim_analysis_schedule}} to track cumulative Type I error.
- Execute test statistics across each simulated cohort, computing standard log-rank, MaxCombo, and restricted mean survival time (RMST) tests.
- Track event counts at each interim milestone to evaluate early stopping criteria for efficacy and futility.
- Aggregate iteration metrics to solve for sample size achieving {{target_statistical_power}} across varying cohort sizes.
- Generate summary data structures and output publication-grade simulation diagnostic plots using
ggplot2.
Constraints
- Code MUST be fully self-contained R script with explicit library loading and zero unresolved object references.
- Script MUST include robust error handling for non-convergent iterations or zero-event subgroups.
- MUST NOT use deprecated R functions or undocumented analytical approximations.
- All generated datasets within the simulation loop MUST be dynamically memory-managed to avoid memory overflow.
- Variable definitions and parameter blocks must be placed at the top of the script for parameter tuning.
Output format
Provide the response in three distinct sequential sections:
- Execution Architecture: A concise technical summary (150-250 words) detailing the simulation framework and convergence logic.
- Executable R Script: Fully commented, vectorized R code structured into Config, Data Generation, Simulation Loop, Analysis/Testing, and Summary Reporting.
- Simulation Diagnostics Table: Formatted markdown table defining the required sample sizes, expected event milestones, and comparative power across tested statistics.
Self-review
- Verify that the data generation function accurately reflects {{hazard_ratio_profile}} over the specified timeline.
- Check that interim alpha-spending calculations in the script match {{interim_analysis_schedule}}.
- Ensure all packages utilized (
survival,simsurv,ggplot2) are loaded with package verification checks.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.