如何计算c-index?求Fine Gray模型的Stata计算代码
Hey there! Let's break this down clearly—first covering what c-index is and how it works, then diving straight into the Stata code for Fine Gray models since you mentioned you already have R solutions sorted out.
一、c-index的基本计算逻辑
The c-index (concordance statistic) measures how well a survival model predicts the order of event times. Here's the core idea:
- For any pair of subjects, if the one with a higher predicted risk experiences the target event earlier, that's a concordant pair.
- If their event times are identical, it's a half-concordant pair.
- If the higher-risk subject has a later event time, that's a discordant pair.
The formula is straightforward:
c-index = (Number of concordant pairs + 0.5 * Number of half-concordant pairs) / Total number of comparable pairs
Comparable pairs are those where at least one subject experienced the target event (we exclude pairs where both are censored or had a competing event).
二、Fine Gray模型下的Stata实现
Stata doesn't have a built-in command for Fine Gray c-index, but we can build it using existing tools or custom code. Here are two reliable methods:
Method 1: Use stcompet + somersd (Fast for large datasets)
First, install the stcompet package if you haven't already:
ssc install stcompet
Then follow these steps (adjust variables to match your dataset):
- Fit your Fine Gray model:
* Define survival data: time = event time, status=1 is target event, status=2 is competing event stset time, failure(status==1) * Fit the Fine Gray model with covariates x1, x2 stcrreg x1 x2, compete(status==2)
- Generate predicted cumulative incidence functions (CIF) for each observation:
predict cif_values, cif
- Calculate the c-index using
somersd(since c-index is equivalent to0.5 + 0.5 * Somers' Dfor survival data):
* Mark observations that experienced the target event gen target_event = (status == 1) * Compute Somers' D between predicted CIF and actual event times somersd cif_values time if target_event | (status == 0), by(target_event) * Calculate and display the c-index display "Fine Gray Model c-index: " 0.5 + 0.5*r(somersd)
Method 2: Custom Loop (Precise for small datasets)
If you want to manually count concordant pairs (great for small samples, but slow for large datasets), use this code:
* First fit your Fine Gray model as before stset time, failure(status==1) stcrreg x1 x2, compete(status==2) * Generate predicted risk scores predict risk_scores, risk * Initialize counters local concordant = 0 local tied_pairs = 0 local total_pairs = 0 * Loop through all subject pairs forvalues i = 1/`=_N' { * Skip if subject i didn't experience the target event if status[`i'] != 1 continue forvalues j = 1/`=_N' { * Skip self-pairs and subjects with competing events if `i' == `j' | status[`j'] == 2 continue local total_pairs = `total_pairs' + 1 * Check concordance if risk_scores[`i'] > risk_scores[`j'] { if time[`i'] < time[`j'] local concordant = `concordant' + 1 } else if risk_scores[`i'] == risk_scores[`j'] { if time[`i'] == time[`j'] local tied_pairs = `tied_pairs' + 1 } else { if time[`i'] > time[`j'] local concordant = `concordant' + 1 } } } * Calculate and display c-index local c_index = (`concordant' + 0.5*`tied_pairs') / `total_pairs' display "Fine Gray Model c-index: " `c_index'
内容的提问来源于stack exchange,提问作者Nawar

