Ramon Diaz-Uriarte
2000-Oct-12 14:16 UTC
[Rd] works in R-1.1.1 but not in R-development; why?
Dear All, A library (PHYLOGR) that passed the usual tests in R-1.1.1 gives errors with R-devel; my (mis?)understanding of scoping rules is that it should have worked in both. The problems seem related to using the name of the data frame for extracting weights or subsets within a function call. The problems can be reproduced as follows: ********************** datai <- data.frame( y = rnorm(10), x1 = rnorm(10), x2 = abs(rnorm(10)), x3 = rep(seq(1,5),2), counter = rep(c(1,2),c(5,5))) formula <- as.formula(y ~ x1) # the following fails in R-1.2.0 but not in R-1.1.1 # > Error in eval(expr, envir, enclos) : Object "datos" not found lapply(split(datai,datai$counter), function(datos,formula) {lm(formula = formula, data = datos, weights = datos$x2)}, formula = formula) # fails in R-1.2.0, but not in R-1.1.1 # > Error in lm.fit(x, y, offset = offset) : 0 (non-NA) cases my.lm <- function(formula, data){ data <- data[data$x3 < 5, ] lm(formula = formula, data = data, subset = data$counter == 1, weights = data$x2) } my.lm(formula, datai) # In R-1.2.0 does NOT use weights (but it works in R-1.1.1) my.lm2 <- function(formula, data){ data <- data[data$x3 < 5, ] lm(formula = formula, data = data, subset = counter == 1, weights = data$x2) # lm(formula = formula, data = data, subset = counter == 1, weights = data$x2)$weights } my.lm2(formula, datai) ################## ## The following all work in both R-1.2.0 and R-1.1.1 datax <- datai[datai$x3 < 5 & datai$counter==1,] lm(formula, data = datax, weights = x2) lm(formula, data = datax, weights = datax$x2) lm(formula, data = datai, subset = (counter == 1 & x3 < 5), weight = x2) lm(formula, data = datai, subset = (datai$counter == 1 & datai$x3 < 5), weight = datai$x2) lapply(split(datai,datai$counter), function(datos,formula) {lm(formula = formula, data = datos, weights = x2)}, formula = formula) my.lm9 <- function(formula, data){ data <- data[data$x3 < 5, ] lm(formula = formula, data = data, weights = x2, subset = counter == 1) } my.lm9(formula, datai) ************************ versions: R-1.1.1 platform i586-pc-linux-gnu arch i586 os linux-gnu system i586, linux-gnu status Patched major 1 minor 1.1 year 2000 month October day 11 language R R-development: platform i586-pc-linux-gnu arch i586 os linux-gnu system i586, linux-gnu status Under development (unstable) major 1 minor 2.0 year 2000 month 10 day 10 language R ******************** -- Ramón Díaz-Uriarte Triana 47 28016 Madrid Spain email:ramon-diaz@teleline.es Phone: +-34-918-513-966 +-34-657-186-407 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Thu, 12 Oct 2000, Ramon Diaz-Uriarte wrote:> Dear All, > > A library (PHYLOGR) that passed the usual tests in R-1.1.1 gives errors with > R-devel; my (mis?)understanding of scoping rules is > that it should have worked in both. The problems seem related to using the > name of the data frame for extracting weights or subsets within a function > call. The problems can be reproduced as follows:There's something tricky happening with parent.frame() lapply(1:10,function(x,y) {browser();x+y),y=1) Browse[1]> eval(quote(parent.frame()),parent.frame()) <environment: 1004cb70> Browse[1]> eval(quote(sys.frame(sys.parent())),parent.frame()) <environment: 1004cdc88> so parent.frame() isn't an exact synonym for sys.frame(sys.parent()). I think this i There *has* also been a change in the scoping rules (only partly deliberate) in model.frame as Peter pointed out, though I don't think it is applying here. A formula now has an environment associated with it, which is used as the default environment for variables not specified in data If you do lapply(datai,function(w) model.frame(~w)) it works: the vector w is found in the environment of the anonymous function. If you do ff<- ~w lapply(datai,function(w) model.frame(ff)) it doesn't work, since the default environment is now the global one. -thomas Thomas Lumley Assistant Professor, Biostatistics University of Washington, Seattle> > ********************** > > datai <- data.frame( y = rnorm(10), x1 = rnorm(10), x2 = abs(rnorm(10)), > x3 = rep(seq(1,5),2), counter = rep(c(1,2),c(5,5))) > > formula <- as.formula(y ~ x1) > > > # the following fails in R-1.2.0 but not in R-1.1.1 > # > Error in eval(expr, envir, enclos) : Object "datos" not found > lapply(split(datai,datai$counter), > function(datos,formula) {lm(formula = formula, data = datos, > weights = datos$x2)}, > formula = formula) > > > > # fails in R-1.2.0, but not in R-1.1.1 > # > Error in lm.fit(x, y, offset = offset) : 0 (non-NA) cases > my.lm <- function(formula, data){ > data <- data[data$x3 < 5, ] > lm(formula = formula, data = data, subset = data$counter == 1, weights = data$x2) > } > > my.lm(formula, datai) > > > > # In R-1.2.0 does NOT use weights (but it works in R-1.1.1) > my.lm2 <- function(formula, data){ > data <- data[data$x3 < 5, ] > lm(formula = formula, data = data, subset = counter == 1, weights = data$x2) > # lm(formula = formula, data = data, subset = counter == 1, weights = data$x2)$weights > } > > my.lm2(formula, datai) > > > ################## > ## The following all work in both R-1.2.0 and R-1.1.1 > > datax <- datai[datai$x3 < 5 & datai$counter==1,] > lm(formula, data = datax, weights = x2) > lm(formula, data = datax, weights = datax$x2) > lm(formula, data = datai, subset = (counter == 1 & x3 < 5), weight = x2) > lm(formula, data = datai, subset = (datai$counter == 1 & datai$x3 < 5), weight = datai$x2) > > lapply(split(datai,datai$counter), > function(datos,formula) {lm(formula = formula, data = datos, > weights = x2)}, > formula = formula) > > my.lm9 <- function(formula, data){ > data <- data[data$x3 < 5, ] > lm(formula = formula, data = data, weights = x2, subset = counter == 1) > } > > my.lm9(formula, datai) > > > > > > ************************ > > versions: > > R-1.1.1 > platform i586-pc-linux-gnu > arch i586 > os linux-gnu > system i586, linux-gnu > status Patched > major 1 > minor 1.1 > year 2000 > month October > day 11 > language R > > > > R-development: > platform i586-pc-linux-gnu > arch i586 > os linux-gnu > system i586, linux-gnu > status Under development (unstable) > major 1 > minor 2.0 > year 2000 > month 10 > day 10 > language R > > ******************** > > > > -- > Ramón Díaz-Uriarte > Triana 47 > 28016 Madrid > Spain > > email:ramon-diaz@teleline.es > Phone: +-34-918-513-966 > +-34-657-186-407 > > > > > > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >Thomas Lumley Assistant Professor, Biostatistics University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2000-Oct-12 17:37 UTC
[Rd] works in R-1.1.1 but not in R-development; why?
Ramon Diaz-Uriarte <ramon-diaz@teleline.es> writes:> Dear All, > > A library (PHYLOGR) that passed the usual tests in R-1.1.1 gives errors with > R-devel; my (mis?)understanding of scoping rules is > that it should have worked in both. The problems seem related to using the > name of the data frame for extracting weights or subsets within a function > call. The problems can be reproduced as follows: > > ********************** > > datai <- data.frame( y = rnorm(10), x1 = rnorm(10), x2 = abs(rnorm(10)), > x3 = rep(seq(1,5),2), counter = rep(c(1,2),c(5,5))) > > formula <- as.formula(y ~ x1) > > > # the following fails in R-1.2.0 but not in R-1.1.1 > # > Error in eval(expr, envir, enclos) : Object "datos" not found > lapply(split(datai,datai$counter), > function(datos,formula) {lm(formula = formula, data = datos, > weights = datos$x2)}, > formula = formula)Ow!... This happens because of a change that makes formulas capture their environment of definition. A workaround is to explicitly set the environment of the formula to the current environment, like this: lapply(split(datai,datai$counter), function(datos,formula) { environment(formula)<-environment() lm(formula = formula, data = datos, weights = datos$x2) }, formula = formula) but I bet Luke wants to comment on this... -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._