s-news
[Top] [All Lists]

Re: Unexplainable error in using the syntax 'function'

To: "Alan Zaslavsky" <zaslavsk@hcp.med.harvard.edu>
Subject: Re: Unexplainable error in using the syntax 'function'
From: "Tan Chuen Seng" <cmetcs@nus.edu.sg>
Date: Sat, 19 Apr 2003 08:56:07 +0800
Cc: <s-news@lists.biostat.wustl.edu>
Thread-index: AcME+lxJnLvC/PgQRzij9suvYklonQBErp9Q
Thread-topic: [S] Unexplainable error in using the syntax 'function'
Hi Splus users,

Thanks for the suggestions you have given me. The problem as pointed by
Alan is due to be the object dat.ma.

Thanks for the suggestions you all have given and the time taken to
reply this email. Now my program is running fine:)

Many thanks,
chuen seng

-----Original Message-----
From: Alan Zaslavsky [mailto:zaslavsk@hcp.med.harvard.edu] 
Sent: Friday, April 18, 2003 12:00 AM
To: Tan Chuen Seng
Cc: s-news@lists.biostat.wustl.edu
Subject: Re: [S] Unexplainable error in using the syntax 'function'


"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>