From rgentlem@stat.auckland.ac.nz  Mon Jul 24 23:21:09 2000
From: rgentlem@stat.auckland.ac.nz (Robert Gentleman)
Date: Tue, 25 Jul 2000 10:21:09 +1200
Subject: [Rd] scoping problems (PR#614)
In-Reply-To: <200007242151.XAA03966@pubhealth.ku.dk>; from kjetilh@umsanet.edu.bo on Mon, Jul 24, 2000 at 11:51:12PM +0200
References: <200007242151.XAA03966@pubhealth.ku.dk>
Message-ID: <20000725102109.Z6896@stat1.stat.auckland.ac.nz>

On Mon, Jul 24, 2000 at 11:51:12PM +0200, kjetilh@umsanet.edu.bo wrote:
> I am resubmitting this to r-bugs, since Thomas Lumley indicates that it
> might be an error:
> 
> On Wed, 5 Jul 2000, Thomas Lumley wrote:
> 
> > On Wed, 5 Jul 2000, halvorsen wrote:
> >
> > > Hola!
> > >
> > > I have the following simple function:
> > >
> > > > testcar
> > > function(pow){
> > >            ob <- glm(Pound~CG+Age+Vage,data=car,weights=No,
> > >
> subset=No>0,family=quasi(link=power(pow),var=mu^2))
> > >
> > >            deviance(ob)
> > > }
> > > But trying to run it gives:
> > >
> > > > testcar(1/2)
> > > Error in power(pow) : Object "pow" not found
> >
> > 'pow' isn't found because quasi() is looking for it in a strange
> place.
> > Normally you would expect this sort of syntax to work, but the family
> > functions do some tricky things to allow you to specify things with or
> 
> > without quotes and without or without parentheses.
> >
> > I suspect this might count as a bug.
> >
> > In any case, you can fix it by doing
> > eval(substitute(glm(Pound~CG+Age+Vage,data=car,weights=No subset=No>0,
> 
> > family=quasi(link=power(pow),var=mu^2))))
> >
> 
> As an additional note. This probably won't work if the argument to
> testcar
> is a variable.
> 
> A more reliable version is
>  eval(substitute(glm(Pound~CG+Age+Vage,data=car,weights=No subset=No>0,
>  family=quasi(link=power(pow),var=mu^2))),list(pow=pow))
> 
> ( I moved the ,list argument from "substitute" to "eval" to get it
> work.)
> 
> Kjetil Halvorsen
> 
> 
  The problem is with quasi:
> quasi
function (link = "identity", variance = "constant") 
{
    linktemp <- substitute(link)
    if (is.expression(linktemp)) 
        linktemp <- eval(linktemp)
    if (!is.character(linktemp)) {
        linktemp <- deparse(linktemp)
        if (linktemp == "link") 
            linktemp <- eval(link)
    }
   ...

  Note that neither of the eval's is what is wanted. At this point
  you just want to force the promise, ie evaluate the argument in
  the calling environment.
  The following piece of code seems to do that:

 > quasi
function (link = "identity", variance = "constant") 
{
    linktemp <- substitute(link)
    if ( is.expression(linktemp) || is.call(linktemp) ) 
        linktemp <- link
    else if (!is.character(linktemp)) 
        linktemp <- deparse(linktemp)
    if( is.character(linktemp) )
        stats <- make.link(linktemp)
    else
        stats <- linktemp
...
> 
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
+-------------------------------------------------------------------------+
| Robert Gentleman              phone : (64-9) 3737-599 ext 3946          | 
| Senior Lecturer               fax :   (64-9) 3737-018                   |
| Department of Statistics      office : Room 206 Maths/Physics           |
| University of Auckland	email : rgentlem@stat.auckland.ac.nz      |
+-------------------------------------------------------------------------+
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] scoping problems (PR#614)
Robert Gentleman