I want code to generate survival curves in a setting with both
- time dependent covariates and
- time varying coefficients.
The goal is to demonstrate how billing method affects life insurance policy lapse. It’s complex in that
- a customers billing method (invoice or EFT) changes over time,
- the effect of billing method on lapse wears off over time, and
- the effect of billing method on lapse depends on other covariates.
After reading the vignette on time dependent covariates, I don’t know how to generate survival curves from a model that has both time-dependent covariates and time-varying coefficients.
library(survival)
Samp <- data.frame(
id = c(143,151,680,134),
time = c(17,16,17,18) ,
censor= rep(1,4) ,
covariate = seq(5,20,length.out = 4))
# Lookup provides the values of a tdc
Lookup <- data.frame(
id =c(rep(134,2),680,143,rep(151,3)) ,
billing.mode = c("INV",rep("EFT",2),rep("INV",2),"EFT","INV") ,
switch.time = c(0,3,rep(0,3),2,7))
# create the tdc
Samp.tdc <- tmerge(data1=Samp,data2=Samp,id=id,
lapse=event(time,censor))
Samp.tdc <- tmerge(data1=Samp.tdc,data2=Lookup,id=id,
billing.mode=tdc(switch.time,billing.mode))
Samp.tdc$inv = as.numeric(Samp.tdc$billing.mode == "INV")
# the call looks something like this
fit <-coxph(Surv(tstart, tstop, lapse) ~ inv + tt(inv) +
covariate*inv, data = Samp.tdc,
tt = function(x, t, ...) x * t)
When I say I want to generate survival curves, I mean predicted survival for a fixed set of times and covariate values. Let's say for the LpsData
below.
LpsData <- data.frame(
tstart = rep(c(0,16,17),times=4),
tstop = rep(16:18,times=4) ,
lapse = 0 ,
covariate = rep(c(10,20),each=3,times=2) ,
inv = rep(c(0,1),each=6) ,
curve=rep(c('eft','inv'), each=6)
)