[R] environments again

Heberto Ghezzo heberto.ghezzo at mcgill.ca
Mon Dec 17 19:28:31 CET 2001


In a previous message I was not clear enough in my querry.
I have the following program:

tst<- function() {
 x <- c(32.7,32.3,31.5,32.1,29.7,29.1,35.7,35.9,33.1,
         36.0,34.2,31.2,31.8,28.0,29.2,38.2,37.8,31.9,
         32.5,31.1,29.7)
 g <- rep(1:7,rep(3,7))
 s <- rep(1:3,7)
 cat(" Only x and g \n")
 aov1(x,g)
 cat("\n\n  Now x, g and s \n")
 aov1(x,g,s=s)
}

aov1 <- function(y,g,s=NULL,comp="mca",meth="Sidak") {
#
  fun <- function(x)
    c(mean(x,na.rm=T),sd(x,na.rm=T),length(x[!is.na(x)]))
#
  li <- length(unique(g))
  cat("   Analysis of Variance with Multiple comparisons\n\n")
  cat(" Groups : ",li,"\n")
  t <- tapply(y,g,fun)

  a <- array(c(t,recursive=T),c(3,li))
  dimnames(a) <- list(c("Mean","S.Dev","n"),1:li)
  df <- length(y)-li
  cat(" Means : ",a[1,],"\n")
  cat(" S.Dev : ",a[2,],"\n")
  cat("  n    : ",a[3,],"\n\n")
#
  if(is.null(s)) {
    b <- aov(y ~ as.factor(g))
    d <- summary(b)
    df <- b$df
    e <- d[[1]][3]
    sig <- e[[1]][2]
  }
  if(!is.null(s)) {
    b <- aov(y ~ as.factor(g) + Error(as.factor(s)))
    d <- summary(b)
    df <- b$Within[8]
    e <- sqrt(d[[2]][[1]][3])
    sig <- unlist(e[[1]][2])
  }
  cat("   Anova \n")
  print(d)
  cat("\n")
}

now aov1 after printing d normally call a function to compute multiple
comparisos that I wrote. I do not have S.
I need to call aov in order to get the residual errors for the test
under the 1 way anova case 1 or the randomized block design case 2.
If I run the program I get with R 1.23 on Win98

tst()
 Only x and g
   Analysis of Variance with Multiple comparisons

 Groups :  7
 Means :  32.16667 30.3 34.9 33.8 29.66667 35.96667 31.1
 S.Dev :  0.6110101 1.587451 1.56205 2.424871 1.942507 3.527511 1.4
  n    :  3 3 3 3 3 3 3

   Anova
             Df  Sum Sq Mean Sq F value  Pr(>F)
as.factor(g)  6 103.151  17.192  4.0947 0.01397 *
Residuals    14  58.780   4.199
---
Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1



  Now x, g and s
   Analysis of Variance with Multiple comparisons

 Groups :  7
 Means :  32.16667 30.3 34.9 33.8 29.66667 35.96667 31.1
 S.Dev :  0.6110101 1.587451 1.56205 2.424871 1.942507 3.527511 1.4
  n    :  3 3 3 3 3 3 3

Error in eval(expr, envir, enclos) : Object "y" not found
>
Querry: Why do I get the Error?
aov1 runs the first time calling aov(y~x) and does not complain of y not
being found
> traceback()
12: eval(expr, envir, enclos)
11: eval(attr(formula, "variables"), data, env)
10: model.frame.default(formula = y ~ as.factor(s), drop.unused.levels =
TRUE)
9: model.frame(formula = y ~ as.factor(s), drop.unused.levels = TRUE)
8: eval(expr, envir, enclos)
7: eval(mf, parent.frame())
6: lm(formula = y ~ as.factor(s), singular.ok = TRUE, method = "qr",
       qr = TRUE)
5: eval(expr, envir, enclos)
4: eval(ecall, parent.frame())
3: aov(y ~ as.factor(g) + Error(as.factor(s)))
2: aov1(x, g, s = s)
1: tst()
>
Sorry but I do not have a clue as to which environment is active when
the error was called
Help anyone?
Thanks
.
Heberto Ghezzo
Meakins-Christie Labs
McGill University
Montreal Canada


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list