Re: [R] stumped by eval
Berwin A Turlach wrote: G'day Peter, On Wed, 13 Feb 2008 08:03:07 +0100 Peter Dalgaard [EMAIL PROTECTED] 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. It is, however, an expression that can be evaluated in the global environment, and that 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' I told you it was elusive... The thing is that lm() is using nonstandard evaluation, so it sees the _symbol_ subset --- er, wait a minute, too many subsets, let us define it like this instead f0 - function(formula, data, s, na.action) { lm(formula, data, subset=s, na.action=na.action) } Ok, lm() sees the _symbol_ s passed as subset and then looks for it in data and then in environment(formula). It never gets the idea to look for s in the evaluation frame of f0. One workaround is this: f0 function(formula, data, s, na.action) { eval(bquote(lm(formula, data, subset=.(s), na.action=na.action))) } Another, I think better, way is f0 function(formula, data, s, na.action) { eval(bquote(lm(formula, data, subset=.(substitute(s)), na.action=na.action))) } The latter also allows f0(Reading~0+Spec+Reader, netto, Spec0 ) Call: lm(formula = formula, data = data, subset = Spec 0, na.action = na.action) Coefficients: Spec Reader -1.2976 0.7934 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 11 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. I think the issue here is still that model.matrix ends up being called with subset=subset rather than subset= Spec==1. Reading up on `subset' in ?model.frame also does not seem to shed light onto what is going on. Remaining confused. Cheers, Berwin -- 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 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@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.
Re: [R] stumped by eval
G'day Peter, On Wed, 13 Feb 2008 08:03:07 +0100 Peter Dalgaard [EMAIL PROTECTED] 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 11 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@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.
Re: [R] stumped by eval
On Wed, 2008-02-13 at 11:18 +0100, Peter Dalgaard wrote: Berwin A Turlach wrote: G'day Peter, On Wed, 13 Feb 2008 08:03:07 +0100 Peter Dalgaard [EMAIL PROTECTED] 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. Oh my! Thank you; in several hours of staring/debugging I didn't get that, and I'm not sure how many more it would have taken. Is there any way I could have discovered that through debugging? The fact that eval was a primitive made it hard to see what it was doing, although I guess the relevant behavior was in model.frame. I thought at one point that it mattered that subset was not a named argument of model.frame, although it is an argument for the default model.frame method. Does that make any difference? Also, when I return to my original problem in which the subset argument (to f0) is missing, will I encounter more difficulties? Maybe the safest thing to do would be to testing for missing at the top level and eliminate the corresponding entries in the calll. Finally, the sample code I gave was a distillation of some code that started with the code of lm. Ross I told you it was elusive... The thing is that lm() is using nonstandard evaluation, so it sees the _symbol_ subset --- er, wait a minute, too many subsets, let us define it like this instead f0 - function(formula, data, s, na.action) { lm(formula, data, subset=s, na.action=na.action) } Ok, lm() sees the _symbol_ s passed as subset and then looks for it in data and then in environment(formula). It never gets the idea to look for s in the evaluation frame of f0. One workaround is this: f0 function(formula, data, s, na.action) { eval(bquote(lm(formula, data, subset=.(s), na.action=na.action))) } Another, I think better, way is f0 function(formula, data, s, na.action) { eval(bquote(lm(formula, data, subset=.(substitute(s)), na.action=na.action))) } The latter also allows f0(Reading~0+Spec+Reader, netto, Spec0 ) Call: lm(formula = formula, data = data, subset = Spec 0, na.action = na.action) Coefficients: Spec Reader -1.2976 0.7934 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 11 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. I think the issue here is still that model.matrix ends up being called with subset=subset rather than subset= Spec==1. 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@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.
[R] stumped by eval
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? f0 - function(formula, data, subset, na.action ) { f1(formula, data, subset, na.action ) } f1 - function (formula, data, subset, na.action ) { mf - match.call() mf[[1]] - as.name(model.frame) mf - eval(mf, parent.frame()) } netto - data.frame(Reading=seq(3), Spec=seq(3), Reader=seq(3)) t3 - f0(Reading~0+Spec+Reader, netto, c(1)) Error in xj[i] : invalid subscript type 'closure' traceback() 7: `[.data.frame`(list(Reading = 1:3, Spec = 1:3, Reader = 1:3), function (x, ...) UseMethod(subset), , FALSE) 6: model.frame.default(formula = formula, data = data, subset = subset, na.action = na.action) 5: model.frame(formula = formula, data = data, subset = subset, na.action = na.action) 4: eval(expr, envir, enclos) 3: eval(mf, parent.frame()) 2: f1(formula, data, subset, na.action) 1: f0(Reading ~ 0 + Spec + Reader, netto, c(1)) I started with a case in which f0 was called with only 2 arguments (i.e., subset was missing), and that is the case I'm ultimately interested in. However, even the situation above is failing. According to resume, the class of subset is numeric in frames 2 and 4, but a function in frames 3 and 5. That may be correct for frame 3, since it is the evaluation function without a named argument subset (and thus it picks up the global value). But it's wrong for 5 (and higher). eval itself is primitive; I don't understand exactly what is going on with the 2 entries for it (frames 3 + 4). R package 2.6.1-1 on Debian GNU/Linux. __ R-help@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.
Re: [R] stumped by eval
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? f0 - function(formula, data, subset, na.action ) { f1(formula, data, subset, na.action ) } f1 - function (formula, data, subset, na.action ) { mf - match.call() mf[[1]] - as.name(model.frame) mf - eval(mf, parent.frame()) } netto - data.frame(Reading=seq(3), Spec=seq(3), Reader=seq(3)) t3 - f0(Reading~0+Spec+Reader, netto, c(1)) Error in xj[i] : invalid subscript type 'closure' traceback() 7: `[.data.frame`(list(Reading = 1:3, Spec = 1:3, Reader = 1:3), function (x, ...) UseMethod(subset), , FALSE) 6: model.frame.default(formula = formula, data = data, subset = subset, na.action = na.action) 5: model.frame(formula = formula, data = data, subset = subset, na.action = na.action) 4: eval(expr, envir, enclos) 3: eval(mf, parent.frame()) 2: f1(formula, data, subset, na.action) 1: f0(Reading ~ 0 + Spec + Reader, netto, c(1)) I started with a case in which f0 was called with only 2 arguments (i.e., subset was missing), and that is the case I'm ultimately interested in. However, even the situation above is failing. According to resume, the class of subset is numeric in frames 2 and 4, but a function in frames 3 and 5. That may be correct for frame 3, since it is the evaluation function without a named argument subset (and thus it picks up the global value). But it's wrong for 5 (and higher). eval itself is primitive; I don't understand exactly what is going on with the 2 entries for it (frames 3 + 4). Yes, this is elusive, but it is not actually eval() that is doing you in. It is the notion of a model environment, see the last bit of ?formula. 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. This behaviour is generally a good thing because it prevents you from accidentally picking up internal variables of f1, but working around it can be a little painful. As far as I recall, you can use an explicit substitute of the subset argument. R package 2.6.1-1 on Debian GNU/Linux. __ R-help@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. -- 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 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@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.