[R] Help in Programming using Methods

Gabor Grothendieck ggrothendieck at gmail.com
Sun Nov 23 17:33:44 CET 2008


Its a matter of where aov is looking for objects.  Try
replacing the av<- line in test.data.frame with the
following which substitutes in the formula and
then evaluates the statement (so that it does not
have to look for it):

 av <- eval(substitute(aov(fo, x), list(fo = formula(model))))


On Sun, Nov 23, 2008 at 10:53 AM, Enio Jelihovschi
<eniojelihovs at gmail.com> wrote:
> I WROTE THIS FUNCTION BELOW
>
> test <- function(x, ...) UseMethod('test', x)
>
> test.data.frame = function(x, model, which, error, ...)
> {
>  av  <- aov(formula(model), data = x)
>  res <- test.aovlist(av, which = which, error = error)
>  return(res)
> }
>
> test.aovlist <- function(x, which, error, ...)
> {
>  mm      <- model.tables(x, "means")
>  tabs    <- mm$tables[-1]
>  tabs    <- tabs[which]
>  res     <- list(av=x, which=which)
>  return(res)
> }
>
> GENERETING DATA
>
> var <- sort(gl(3, 24, lab=c('Ladak', 'Cossack', 'Ranger')))
> man <- rep(gl(4, 6, lab=LETTERS[1:4]), 3)
> sub <- rep(gl(4, 6), 3)
> blo <- factor(rep(1:6, 12))
> y   <- c(2.17, 1.88, 1.62, 2.34, 1.58, 1.66,
>        1.58, 1.26, 1.22, 1.59, 1.25, 0.94,
>        2.29, 1.60, 1.67, 1.91, 1.39, 1.12,
>        2.23, 2.01, 1.82, 2.10, 1.66, 1.10,
>        2.33, 2.01, 1.70, 1.78, 1.42, 1.35,
>        1.38, 1.30, 1.85, 1.09, 1.13, 1.06,
>        1.86, 1.70, 1.81, 1.54, 1.67, 0.88,
>        2.27, 1.81, 2.01, 1.40, 1.31, 1.06,
>        1.75, 1.95, 2.13, 1.78, 1.31, 1.30,
>        1.52, 1.47, 1.80, 1.37, 1.01, 1.31,
>        1.55, 1.61, 1.82, 1.56, 1.23, 1.13,
>        1.56, 1.72, 1.99, 1.55, 1.51, 1.33)
> dfm <- data.frame(var, sub, man, blo, y)
>
> te <- test(dfm, model = 'y ~ blo + man*var + Error(blo/var)',
>                   which = 'man', error ='Within' )
>
> THE RESULT AFTER TRYING TO RUN test IS :
> Error in formula(model) : object "model" not found
>
> IF I TRY THIS WAY, AGAIN THE SAME ERROR
>  tes <- test.data.frame(dfm, model = 'y ~ blo + man*var + Error(blo/var)',
>                    which = 'man', error ='Within' )
> Error in formula(model) : object "model" not found
>
> BUT, THAT WAY IT WORKS
>  av  <- aov(formula('y ~ blo + man*var + Error(blo/var)'), data = dfm)
>  res <- test.aovlist(av, which = 'man', error ='Within')
>
>  res
> $av
>
> Call:
> aov(formula = formula("y ~ blo + man*var + Error(blo/var)"),
>    data = dfm)
>
> Grand Mean: 1.596806
>
> Stratum 1: blo
>
> Terms:
>                     blo
> Sum of Squares  4.149824
> Deg. of Freedom        5
>
> Estimated effects may be unbalanced
>
> Stratum 2: blo:var
>
> Terms:
>                      var Residuals
> Sum of Squares  0.1780194 1.3623472
> Deg. of Freedom         2        10
>
> Residual standard error: 0.3690999
> 6 out of 8 effects not estimable
> Estimated effects may be unbalanced
>
> Stratum 3: Within
>
> Terms:
>                      man   man:var Residuals
> Sum of Squares  1.9624708 0.2105583 1.2585458
> Deg. of Freedom         3         6        45
>
> Residual standard error: 0.1672354
> Estimated effects may be unbalanced
>
> $which
> [1] "man"
>
> I RUN THE TRACING THRU IT AND COULD FIND WHY IT DOES NOT WORK. THE PROBLEM
> OCCURS IN THE FUNCTION 'model.tables.aovlist' BUT i CANNOT FIGURE IT OUT WHY
> IT HAPPENS
> MANY THANKS
>
> Enio Jelihovschi
> Universidade Estadual de Santa Cruz
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list