[Rd] works in R-1.1.1 but not in R-development; why?

Ramon Diaz-Uriarte ramon-diaz@teleline.es
Thu, 12 Oct 2000 16:16:52 +0200


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._