2

I'm trying to write a function for a task I need to do many times (running a cox proportional hazards function over multiple imputed datasets). When I pass the necessary objects to my user-defined function, however, it gives an error, stating that the object cannot be found. I think this is because the object is defined within a dataframe that is specified with the "data=" argument within the cch() function. Can anyone help me with this?

Example data:

my.list<-list(my.df1 <- data.frame(my.id = 1:100, my.time = rlnorm(100), 
my.event= c(rbinom(50,1,0.2),rep(1,50)), my.det=rbinom(100,1,0.5), 
sub= c(rep(1,50), rbinom(50, 1, 0.1))),  my.df2 <- data.frame(my.id = 1:100, 
my.time = rlnorm(100), my.event= c(rbinom(50,1,0.2),rep(1,50)), 
my.det=rbinom(100,1,0.5), sub= c(rep(1,50), rbinom(50, 1, 0.1))))

Outside my user-defined function, this works:

library(KMsurv)
library(survival)
cch(Surv(my.time,my.event)~as.factor(my.det), data=my.df1, subcoh=~sub, 
id=~my.id, cohort.size=500)

However, this does not work (this is an example function, not the real function as the real function is more complex and runs analyses on multiple datasets, then combines them):

myfun<-function(dflist,time,event){
for (i in 1:length(dflist)){
out<-cch(Surv(time,event)~as.factor(my.det), data=dflist[[i]], 
subcoh=~sub, id=~my.id, cohort.size=500)
print(out)}
}   
myfun(my.list,my.time,my.event)

I get this error: "Error in Surv(time, event) : object 'my.time' not found".

I found some posts about using an eval(substitute()) function to deal with a similar problem, but I can't get it to work. Any suggestions are greatly appreciated!

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
eva_utrecht
  • 95
  • 1
  • 2
  • 6
  • are you planning on doing it on more variables, thus it is important to be able to input time/event? or do you just want to do it for multiple data.frames? – James Tobin Mar 24 '14 at 19:44
  • You sure you got that right? First thing: you should pass `my.det` to your function to avoid depending on it being in the parent environment. Second, I bet you've got your `my.list` with one too many sub-levels of lists, so the `dflist[[i]]` isn't pointing to what you think it is. – Carl Witthoft Mar 24 '14 at 19:48
  • @ James Tobin, yes, I need to be able to put in different time/event variables. Also, I'd like to be able to put in different determinants/explanatory variables, but for the sake of simplicity I didn't mention that. Gives the same error, though. – eva_utrecht Mar 25 '14 at 10:02
  • @ Carl Witthoft: On your first comment: I don't understand it, could you explain please? I would like to pass my.det to the function too (to be able to vary that, too), but for the sake of simplicity I left it out now. You think that causes a problem? – eva_utrecht Mar 25 '14 at 10:05
  • @ Carl Witthoft: on your second comment: I dont have any sublists. The only objects listed are data frames. – eva_utrecht Mar 25 '14 at 10:06

1 Answers1

3

Try this. You need to keep in mind that R doesn't know what's my.time and my.event. You have to parse them with quotes and then unqoute them in order to parse it into Surv

myfun<-function(dflist,time,event){
  for (i in 1:length(dflist)){
    time <- noquote(time)
    event <- noquote(event)
    out<-cch(Surv(dflist[[i]][, time], dflist[[i]][, event])~as.factor(my.det), data=dflist[[i]], 
             subcoh=~sub, id=~my.id, cohort.size=500)
    print(out)}
}   
myfun(my.list,"my.time","my.event")
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • Thank you for your suggestion. Your solution works, but doesn't really solve my problem, as I need to be able to put in different "time variables" and "event variables". Preferably, I'd also be flexible in "my.det", the determinant(s). Any more suggestions? – eva_utrecht Mar 25 '14 at 09:58
  • Yes, I've edited my answer, it will work now for every variables you parse, check above – David Arenburg Mar 25 '14 at 10:19
  • Wow it worked, thanks so much!! I"ll look into this parsing thing for future purposes :) – eva_utrecht Mar 28 '14 at 15:11