From p.dalgaard at biostat.ku.dk  Sun Apr 12 21:37:52 2009
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: Sun, 12 Apr 2009 21:37:52 +0200
Subject: [Rd] data argument and environments
In-Reply-To: <B402D9D9-0F6F-4D47-99E8-4712B219C4A8@ysidro.econ.uiuc.edu>
References: <5CBE3B72-0ABC-45C5-90EA-9CAA89A36A5E@ysidro.econ.uiuc.edu>	<49E12B0A.4020407@stats.uwo.ca>
	<B402D9D9-0F6F-4D47-99E8-4712B219C4A8@ysidro.econ.uiuc.edu>
Message-ID: <49E24310.5060404@biostat.ku.dk>

roger koenker wrote:
> Thanks.  Yes,  I wrote rqss,  and attempted to follow the structure of 
> lm, and various analogues,
> for example in survival4.  My problem seems to be that my lam variable 
> is not part of
> the data frame d, and I don't know how to manipulate the environment for 
> the formula
> so that it is found.  There is an untangle.specials() call
> 
>     tmpc <- untangle.specials(Terms, "qss")
> 
> and then each of the "specials"  terms are evaluated in:
> 
>     qss <- lapply(tmpc$vars, function(u) eval(parse(text = u), data))
> 
> which is fine if the data hasn't been specified so it defaults to 
> parent.frame(), since in
> this case variables and lam can all be found in the parent.frame,  but if
> it is specified as a data frame for the variables of the model, then the 
> lam value is
> unavailable.  My impression is that it is somewhat unusual to pass data 
> other than
> variables from the data frame itself for evaluation of the formula -- I 
> thought there
> were examples in mgcv, but I now see that  lamdas in gam() are passed as 
> separate
> arguments, rather than in the special components of the formula.  
> Perhaps I need
> to revert to this strategy, but I'd prefer not to.  Surely, there is 
> some good way to modify
> the above lapply so  that eval finds both stuff in data and in the 
> parent.frame?  It
> appears that I can simply define pf <- parent.frame()  and then add 
> enclos = pf
> to the above eval() call,  is this ok?

I think more likely you want enclos=environment(formula). This is the 
point, the formula-with-environment construction allows both

h <- function(x,y) mymodel(y~x)
h(u,v)

and

h <- function(f) mymodel(f)
h(u~v)

to find  their variables in the right place.




-- 
    O__  ---- Peter Dalgaard             ?ster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



[Rd] data argument and environments
Peter Dalgaard