[R] Some seemingly odd behavior of survfit in library survival

John Kolassa ko|@@@@ @end|ng |rom @t@t@rutger@@edu
Tue Mar 17 03:23:26 CET 2020


I ran across an issue that looks like variable scoping in survfit is not acting as I would expect.  Here's a minimal example:

new<-as.data.frame(list(t=1:4,d=rep(1,4),s=c(1,0,0,1)))
library(survival)
mine<-function(ff,df){
        fit<-coxph(ff,data=df)
        out<-survfit(fit,df)
}
mine(as.formula("Surv(t,d)~s"),new)

I would expect this to fit the proportional hazards regression model using formula Surv(t,d)~s, using data set new, and then calculate a separate fitted survival curve for each member of the data set.  Instead I get an error

Error in eval(predvars, data, env) :
  invalid 'envir' argument of type 'closure'

The code runs without error if I modify it by copying the data set new to the local variable within the function mine before running:

new<-as.data.frame(list(t=1:4,d=rep(1,4),s=c(1,0,0,1)))
library(survival)
mine<-function(ff,df){
        fit<-coxph(ff,data=df)
        out<-survfit(fit,df)
}
df<-new
mine(as.formula("Surv(t,d)~s"),new)

which leads me to believe that there's some variable scoping error.  Can anyone point out what I'm doing wrong?  Thanks, John

	[[alternative HTML version deleted]]



More information about the R-help mailing list