s-news
[Top] [All Lists]

Re: Unexplainable error in using the syntax 'function'

To: cmetcs@nus.edu.sg
Subject: Re: Unexplainable error in using the syntax 'function'
From: Alan Zaslavsky <zaslavsk@hcp.med.harvard.edu>
Date: Thu, 17 Apr 2003 11:59:37 -0400 (EDT)
Cc: s-news@lists.biostat.wustl.edu
"Tan Chuen Seng" <cmetcs@nus.edu.sg> asked
> The program repeatedly does a cox proportional hazard regression to the
> data, dat.ma, where the observations are in the rows and the different
> scenario in the columns. It works fine as a plain program but when I
> start to use the syntax function, it was unable to recognize the index
> 'w' in the for-loop (in S-Plus output: Object "w" not found).

This problem is a consequence of the approach taken to finding the data
in many S-Plus modeling functions.  (I am writing generalities now, 
because I haven't looked at the specifics of this function and version.)
Your function passes coxph() a formula argument

        Surv(dat.ma[,1],dat.ma[,2]) ~ dat.ma[,(w+2)]

The object dat.ma is probably being found not because it is the data argument
to the function, but rather because you are using the same name for an
object that is in an attached data directory.  You would get badly bitten
if you called rep.cox() with a different argument.  However, the object w 
containing the index lives in the frame of the function rep.cox, which is
not in the default search path for coxph when the latter is called.

Sometimes people handle this problem by assigning the object to the data
directory from the calling function using
assign("dataframename",computed.data.frame,wh=1).  In your case since the
form of the object is pretty simple, you can do it more neatly with something
like the following:

for(w in 1:no.obs) {
   sur.tmp <- coxph(Surv(a,b) ~ d,
        data=list(a=dat.ma[[1]],b=dat.ma[[2]],d=dat.ma[[w+2]]), 
        method='breslow')
    .... etc. ....
   }
  

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