Re: [R] stumped by eval

From: Berwin A Turlach <berwin_at_maths.uwa.edu.au>
Date: Wed, 13 Feb 2008 17:39:39 +0800

G'day Peter,

On Wed, 13 Feb 2008 08:03:07 +0100
Peter Dalgaard <p.dalgaard_at_biostat.ku.dk> wrote:

> Ross Boylan wrote:
> > In the following example, the inner evaluation pulls in the global
> > value of subset (a function) rather than the one I thought I was
> > passing in (a vector). Can anyone help me understand what's going
> > on, and what I need to do to fix the problem?

[...]

> The point is that subset (and offset) arguments are subject to the
> same evaluation rules as the terms inside the formula: First look in
> "data", then in the environment of the formula, which in this case is
> the global environment.

Perhaps I have a [senior|blonde]-day today, but this does not seem to be the full explanation about what is going on to me. According to this explanation the following should not work:

> lm(Reading~0+Spec+Reader, netto, subset=c(1) )

Call:
lm(formula = Reading ~ 0 + Spec + Reader, data = netto, subset = c(1))

Coefficients:
  Spec Reader

     1 NA

since the value passed to subset is not part of "data" and not in the global environment. But, obviously, it works. OTOH, if we change f0 to

> f0

function(formula, data, subset, na.action) {
  lm(formula, data, subset=subset, na.action=na.action) }

then we get the same behaviour as with Ross's use of f1 inside of f0:

> t3 <- f0(Reading~0+Spec+Reader, netto, c(1) )
Error in xj[i] : invalid subscript type 'closure'

More over, with the original definition of f0:
> f0

function(formula, data, subset, na.action) {
  f1(formula, data, subset, na.action)
}
> (f1(Reading~0+Spec+Reader, netto, subset= Spec==1 ))
  Reading Spec Reader
1 1 1 1
> f0(Reading~0+Spec+Reader, netto, subset= Spec==1 )
Error in xj[i] : invalid subscript type 'closure'

Given your explanation, I would have expected this to work.

Reading up on `subset' in ?model.frame also does not seem to shed light onto what is going on.

Remaining confused.....

Cheers,

        Berwin



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 13 Feb 2008 - 09:44:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 13 Feb 2008 - 10:30:13 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive