
Time-dependent Cox Proportional Hazards Analysis with Splines and Diagnostics
Source:R/cox_analysis.R
analysis_cox.RdFits a Cox proportional hazards model with time-dependent exposure, optional spline-modeled continuous covariates, and categorical covariates. The function constructs start-stop (counting process) survival data, applies reference level releveling, fits the model, and returns diagnostic plots and proportional hazards tests.
Arguments
- dpop
A data frame containing population-level variables including at minimum: `ID`, `DATE_BIRTH`, `DATE_DEATH`, `DATE_MIGRATION`, `exp.DATE`, and `resp.DATE`.
- data_dates
A data frame containing baseline date information. Must include `ID` and `vpvmbl` (baseline date).
- data_socioeconomic
A data frame containing socioeconomic or additional covariates (e.g., education).
- normal_vars
Character vector of covariates to include in the model as standard (non-spline) terms.
- spline_vars
Character vector of continuous variables to be modeled using cubic B-splines (`splines::bs()`).
- reference_values
Named list specifying reference levels for categorical variables. Used internally for releveling.
- censoring_date
Administrative censoring date (Date).
Value
A list containing:
- model
Fitted `coxph` model object.
- test_residual
Result of `cox.zph()` proportional hazards test.
- plot_diagnostics
Schoenfeld residual diagnostic plot.
- plot_splines
Named list of spline effect plots.
- plot_forest
Forest plot of model estimates.
Details
Designed for registry-based longitudinal data with:
Baseline date (`vpvmbl`)
Exposure date (`exp.DATE`)
Response/event date (`resp.DATE`)
Migration and death censoring dates
The function performs the following steps:
Merges baseline, population, and socioeconomic data.
Calculates age at baseline and follow-up time variables.
Handles exposure occurring before baseline by shifting exposure to baseline.
Constructs counting-process format survival data (`Surv(tstart, tstop, event)`).
Creates a time-dependent exposure indicator.
Fits a Cox model using `survival::coxph()`.
Produces:
Forest plot (`survminer::ggforest`)
Spline effect plots (`ggeffects::ggpredict`)
Proportional hazards test (`cox.zph`)
Schoenfeld residual plots (`ggcoxzph`)
Tied event times are handled using default `coxph` tie handling.