wrong environment in anova call inside function (PR#7937)
anova() is a generic function in R, and not implicated here: do read the error message which pinpoints anova.lme, part of package nlme. The FAQ asks only package maintainers to use R-bugs for contributed packages such as nlme. Please do read it.
On Tue, 14 Jun 2005 mulakken1 at llnl.gov wrote:
Hi,
I am working with R version 2.1.0, and I seem to have run into a bug. I get
the same bug when I run R on Windows as well as when I run it on Linux.
When I call anova to do a LR test from inside a function, I get an error.
The same call works outside of a function. I have provided the code below.
Thanks,
Nisha Mulakken
############################
myFunction <- function(myDataFrame) {
# Less restricted
fit1 <- gls(y ~ dose,
weights=varIdent(form=~1|dose),
data=myDataFrame)
# more restricted
fit2 <- gls(y ~ dose,
data=myDataFrame)
anova.results <- anova(fit1, fit2)
anova.results
}
df <- data.frame( y=c(12,3,45,1,53,6),
dose=c(0,10,200,0,10,200),
time=c("4.00 hrs", "4.00 hrs", "6.00 hrs", "6.00 hrs",
"8.00 hrs", "8.00 hrs"),
time.hours=c(4, 4, 6, 6, 8, 8),
rep=rep("a", 6)
)
## This leads to the following error:
## Error in anova.lme(object = fit1, fit2) : Object "fit2" not found
results <- myFunction(myDataFrame=df)
#####################################################
## The same thing outside of a function
# Less restricted
fit3 <- gls(y ~ dose,
weights=varIdent(form=~1|dose),
data=df)
# more restricted
fit4 <- gls(y ~ dose,
data=df)
## This works:
anova(fit3, fit4)
## The results:
## > anova(fit3, fit4)
## Model df AIC BIC logLik Test L.Ratio p-value
## fit3 1 5 57.98998 54.92145 -23.99499
## fit4 2 3 55.75284 53.91172 -24.87642 1 vs 2 1.76286 0.4142
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595