Skip to content

anova.gls from nlme on multiple arguments within a function fails

4 messages · Markus Jäntti, Spencer Graves, Brian Ripley

#
Dear All -- 

I am trying to use within a little table producing code an anova
comparison of two gls fitted objects, contained in a list of such
object, obtained using nlme function gls.
The anova procedure fails to locate the second of the objects.

The following code, borrowed from the help page of anova.gls,
exemplifies:
--------------- start example code ---------------
library(nlme)

## stolen from example(anova.gls)
# AR(1) errors within each Mare
fm1 <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), Ovary,
           correlation = corAR1(form = ~ 1 | Mare))
anova(fm1)
# variance changes with a power of the absolute fitted values?
fm2 <- update(fm1, weights = varPower())
anova(fm1, fm2)

## now define a little function
dummy <- function(obj)
  {
    anova(obj[[1]], obj[[2]])
  }
dummy(list(fm1, fm2))

## compare with what happens in anova.lm:

lm1 <- lm(follicles ~ sin(2*pi*Time), Ovary)
lm2 <- lm(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), Ovary)
dummy(list(lm1, lm2))
------------- end example code ------------------

It is not the end of the world: I can easily work around this. 
But it would be nice to know why this does not work.

Digging around using options(error=recover) did not help my much, I'm
afraid.  

Best,

Markus
10 days later
#
You've posed an excellent question with simple and elegant, 
reproducible example.  I've seen no replies, so I will attempt a partial 
response.  RSiteSearch("lexical scoping") produced some potentially 
useful comments (e.g., 
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/37769.html), but nothing 
that allowed me to work around the problem.

	  The following modification of your example makes it clear that 
"anova.lme" (called by "anova.gls") choked on the second argument not 
the first:

 > dummy2 <- function(obj)
+   {
+     obj2 <- obj[[2]]
+     anova(obj[[1]], obj2)
+   }
 > dummy2(list(fm1, fm2))
Error in anova.lme(object = obj[[1]], obj2) :
	object "obj2" not found

	  The following helped isolate this further to "dots <- list(...)", the 
second line in "anova.lme":

debug(anova.lme)
dummy2(list(fm1, fm2))

	  I don't know why your example fails, especially "anova.lm" worked. 
Also, there should be a way  to use something like "assign" to work 
around this problem, but nothing I tried worked.

	  I know this is not a complete reply, but I hope it helps.
	  spencer graves
Markus Jantti wrote:

            

  
    
#
The error is in anova.gls(): traceback() would have told you that was 
involved.  It ends

do.call("anova.lme", as.list(match.call()[-1]))

and that call needs to be evaluated in the parent, not in the body of 
anova.lme.  Several similar errors (e.g. in the update methods) in package 
nlme have been corrected over the years.

Replacing anova() by anova.lme() in dummy() works.

If you want to do this sort of thing more generally (e.g. messing with 
the contents of '...'), the elegant way is

Call <- match.call()
Call[[1]] <- as.name("anova.lme")
eval.parent(Call)

and that works here.

Since at some later point substitute() is used to find the arguments, here 
you don't want to use do.call() with the evaluated arguments, which is the 
way it is intended to be used.  Similarly, anova.lme(object, ...) is not 
what you want.
On Sun, 27 Nov 2005, Spencer Graves wrote:

            
It actually chokes on both.

  
    
#
Dear Prof. Ripley:

	  Thanks very much.  I tried several superficially similar things but 
not either of the solutions you suggest.

	  Best Wishes,
	  spencer graves
Prof Brian Ripley wrote: