s-news
[Top] [All Lists]

Re: [S] survfit + coxph

To: "Paul Y. Peng" <peng@stat.ualberta.ca>
Subject: Re: [S] survfit + coxph
From: Prof Brian D Ripley <ripley@stats.ox.ac.uk>
Date: Mon, 25 May 1998 08:06:23 +0100 (BST)
Cc: s-news@wubios.wustl.edu
In-reply-to: <199805250450.WAA08268@bimodal.stat.ualberta.ca>
Sender: owner-s-news@wubios.wustl.edu
On Sun, 24 May 1998, Paul Y. Peng wrote:

> Why
> 
> > survfit(coxph(Surv(time,cens)~group,data = my.data))
> 
> works, but
> 
> > (function(data)survfit(coxph(Surv(time,cens)~group,data = data)))(my.data)
> 
> doesn't work? It might be a simple question, but my brain doesn't seem
> to work for me now. Thanks for help.
> 

It is the usual story: when you embed a call within a function you have to
remember the scope rules. To find out, look at the traceback, e.g.

> jj <- function(data)
+   survfit(coxph(Surv(time, cens) ~ treat, data=data))
> jj(gehan)
Error in model.frame.coxph(object): Object "data" not found
Dumped
> traceback()
Message: Object "data" not found 
6: model.frame.default(formula = Surv(time, cens) ~ treat, data = data)
5: eval(Call)
4: model.frame.coxph(object)
3: survfit.coxph(coxph(Surv(time, cens) ~ treat, data = data))
2: jj(gehan)
1: 

and if you go into the debugger you will find that Call in frame 4 is

d(4)> Call
model.frame(formula = Surv(time, cens) ~ treat, data = data)

Now data is defined in frame 2 and so not visible. (Incidentally, I think
there is a problem in model.frame.coxph which should evaluate Call in a
different frame, perhaps sys.parent. A common problem in the system S
code.) 


One answer is

jj <- function(data) {
  assign(".my.data", data, f=1)
  survfit(coxph(Surv(time, cens) ~ treat, data=.my.data))
}

> jj(gehan)
Call: survfit.coxph(object = coxph(Surv(time, cens) ~ treat, data = 
.my.data))

  n events mean se(mean) median 0.95LCL 0.95UCL 
 42     30 13.8     1.35     13      10      23

As usual with such things, the call component is not helpful.


A better way is to assemble the call you want and evaluate it in the
calling frame, for example

jj2 <- function(data) {
  dd <- deparse(substitute(data))
  call <- paste("survfit(coxph(Surv(time, cens) ~ treat, data=", dd, "))")
  eval(parse(text=call), sys.parent())
}

jj2(gehan)
Call: survfit.coxph(object = coxph(Surv(time, cens) ~ treat, data =
gehan))

  n events mean se(mean) median 0.95LCL 0.95UCL 
 42     30 13.8     1.35     13      10      23


-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>