Reputation: 191
Using tmerge()
to code time-varying covariate for survival: What if I don't know what covariate I want to code until runtime?
Pretend data: two people, and two time-varying covariates: bmi
, and modifiedBmi
.
personWise <- data.frame(personId=1:2, startDay=0, endDay=1:2 * 300,
event=c(TRUE, FALSE))
print.data.frame(personWise)
personId startDay endDay event
1 1 0 300 TRUE
2 2 0 600 FALSE
visitWise <- data.frame(
personId=c(1, 1, 1, 2, 2),
visitDay=c(50, 150, 200, 20, 500),
bmi=c(3, 4, 3, 6, 5),
modifiedBmi=c(1, 7, 8, 2, 6)
)
print.data.frame(visitWise)
personId visitDay bmi modifiedBmi
1 1 50 3 1
2 1 150 4 7
3 1 200 3 8
4 2 20 6 2
5 2 500 5 6
If I want to code bmi
as my time-varying covariate, I can use tmerge()
like this:
tv <- tmerge(data1=personWise, data2=personWise, id=personId, tstart=startDay,
tstop=endDay, event=event(endDay, event))
tv <- tmerge(data1=tv, data2=visitWise, id=personId,
bmiMeasure=tdc(visitDay, bmi))
Or, if I'm interested in modifiedBmi
, I could change that second tv()
call to include . . . bmiMeasure=tdc(visitDay, modifiedBmi))
But what if I want to run a number of models, possibly on different datasets, changing which BMI measure I care about programatically?
Ideally I want something like bmiCovariate <- "bmi"
and . . . bmiMeasure=tdc(visitDay, bmiCovariate))
, but of course that doesn't work because the arguments to tdc()
are getting some version of nonstandard evaluation?
Is there a way to do this?
Upvotes: 0
Views: 41
Reputation: 2259
Perhaps you could set up control logic that determines which measure is used?
bmiCovariate <- "bmi"
if(bmiCovariate == "bmi"){
tv <- tmerge(data1=tv, data2=visitWise, id=personId,
bmiMeasure=tdc(visitDay, bmi))
} else {
tv <- tmerge(data1=tv, data2=visitWise, id=personId,
bmiMeasure=tdc(visitDay, modifiedBmi))
}
Upvotes: 1