On Fri, 8 Apr 2005, Sebastian Luque wrote:
> I'm having trouble understanding how functions in the subset argument
for
> lmList search for the objects they need. This trivial example produces
> "Error in rownames(fakedf) : Object "fakedf" not
found":
>
> library(nlme)
>
> fitbyID <- function() {
> fakedf <- data.frame(ID = gl(5, 10, 50),
> A = sample(1:100, 50),
> B = rnorm(50))
> mycoefs <- lmList(B ~ A | ID,
> data = fakedf,
> subset = !is.element(rownames(fakedf),
> c("3", "10")))
> coef(mycoefs)
> }
>
> fitbyID()
>
>
> If fakedf is already in the workspace, then the function runs fine, so
> rownames seems to be looking for it in the global environment, although
> I'd expect it to search locally first. I suspect this shows some gaps
in
> my understanding of environments and related concepts. I'd be grateful
for
> some advice on this.
That's not how several functions in nlme were written (I have mentioned it
to the authors in the past). lmList.formula contains
if (!missing(subset)) {
data <-
data[eval(asOneSidedFormula(Call[["subset"]])[[2]],
data), , drop = FALSE]
}
So that evaluates 'subset' first in data and then in the body of the
lmList. As in S/R the parent frames are not in the scope for that
evaluation, it does not look in the body of your function 'fitbyID'.
Functions using the standard paradigm (such as lm) do arrange to do the
evaluation in the parent, but that can cause problems if nesting goes
deeper (as e.g. in step()). Things were complicated by the change around
1.2.0 to (in the standard paradigm) look in the environment of the formula
(not done here).
The simplest workaround is to assign 'fakedf' with some innocuous name
(usually beginning with a dot) in the workspace.
--
Brian D. Ripley, ripley at 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 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595