含时变协变量与时变系数的生存曲线生成代码需求
Hey there! I totally get that wrapping your head around survival curves when you have both time-dependent covariates and time-varying coefficients can be tricky—even after digging through the vignettes. Let's walk through exactly how to generate your desired curves using your LpsData and existing model setup.
First, Quick Recap of Your Model
You've already built a solid Cox model that hits all your requirements:
- Time-dependent covariate:
inv(indicator for invoice billing, changes over time viatmerge) - Time-varying coefficient:
tt(inv)withx*t(so the impact of invoice billing weakens linearly over time) - Effect modification:
covariate*inv(billing mode impact depends on your other covariate)
Your fitted model looks like this (just to confirm):
library(survival) # ... [your tmerge code to build Samp.tdc] ... fit <- coxph(Surv(tstart, tstop, lapse) ~ inv + tt(inv) + covariate*inv, data = Samp.tdc, tt = function(x, t, ...) x * t)
Step 1: Generate Predictions with LpsData
Your LpsData is perfectly structured for prediction—it defines the time intervals, covariate values, and billing mode groups you want to compare. To generate survival curves, we'll use survfit() with a key parameter: id=curve to group intervals into continuous curves.
# Generate predicted survival curves from your fitted model surv_pred <- survfit(fit, newdata = LpsData, id = curve)
Why id=curve?
This tells survfit to connect the time intervals for each unique curve group (e.g., all rows labeled "eft" with covariate=10) into a single, continuous survival curve instead of treating each interval as a separate segment.
Step 2: Visualize the Curves
You can use base R plotting for a quick view, or ggplot2 for more polished visuals.
Base R Plot
plot(surv_pred, col = c("steelblue", "firebrick", "steelblue4", "firebrick4"), lty = c(1, 1, 2, 2), xlab = "Time (Months/Years)", ylab = "Probability of Policy Not Lapsing", main = "Billing Mode Impact on Policy Lapse (Time-Varying Effects)") # Add a legend to clarify groups legend("bottomleft", legend = c("EFT (covariate=10)", "Invoice (covariate=10)", "EFT (covariate=20)", "Invoice (covariate=20)"), col = c("steelblue", "firebrick", "steelblue4", "firebrick4"), lty = c(1,1,2,2), bty = "n")
ggplot2 + survminer (More Customizable)
library(ggplot2) library(survminer) library(broom) # Convert survival predictions to a tidy data frame surv_tidy <- tidy(surv_pred) # Map curve groups to readable labels (matches LpsData's structure) surv_tidy$group <- rep(c("EFT (cov=10)", "Invoice (cov=10)", "EFT (cov=20)", "Invoice (cov=20)"), each = 3) # Build the plot ggplot(surv_tidy, aes(x = time, y = estimate, color = group, linetype = group)) + geom_step(size = 1) + labs(x = "Time", y = "Survival Probability", title = "Policy Survival Curves by Billing Mode & Covariate", subtitle = "Time-varying coefficient: Invoice impact weakens over time") + theme_minimal() + scale_color_manual(values = c("steelblue", "firebrick", "steelblue4", "firebrick4")) + scale_linetype_manual(values = c(1,1,2,2)) + theme(plot.title = element_text(hjust = 0.5))
Key Notes to Keep in Mind
- Your
LpsDatasetslapse=0—this is totally fine for prediction; we don't need event outcomes here, just the covariate and time structure. - The time-varying coefficient (
tt(inv) = x*t) is automatically incorporated into predictions:survfitcalculates the coefficient value at each time pointtin your intervals. - The
covariate*invinteraction ensures curves differ between covariate values, reflecting that billing mode impact depends on this other factor.
Verify Predictions (Optional)
If you want to check the linear predictor values (the underlying risk scores) at each time point, use:
# Get linear predictor values for each row in LpsData lp_values <- predict(fit, newdata = LpsData, type = "lp") LpsData$lp <- lp_values
This will show you how the risk changes over time for each group, confirming the time-varying effect is working as intended.
内容的提问来源于stack exchange,提问作者PdV

