[R] model.tables call fails with "Error in inherits(object, "formula")"
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Jul 23 14:50:41 CEST 2010
The error message actually is
Error in inherits(object, "formula") : object 'frmlc' not found
and it is the omitted-by-you part after the colon that matters: the
body of model.tables does not have 'frmlc' in its scope.
Here is one approach to using dynamic formulae that works in a lot of
similar situations:
test <- function(dat) {
frmlc <- as.formula("yield ~ block + Error(N/block)")
aovfit <- eval(substitute(aov(form, dat),list(form=frmlc)))
model.tables(aovfit, "means")
}
written without the pointless empty statements after the semicolons.
On Fri, 23 Jul 2010, Hannes wrote:
> Hello,
>
> I noticed that model.tables fails when applied to an aov() fit if called
> inside a function. The problem seems to occur when as.formula is used inside
> a function on a string containing
> "<formula> + Error( x / y )"
>
> The reason I tried to use as.formula is to generate dynamic calls to aov().
> Here is a minimal example illustrating the problem:
> ## Example
>
> test <- function(dat) {
> frmlb <- "yield ~ block"; # WORKS
> aovfit <- aov(as.formula(frmlb), dat);
> print(model.tables(aovfit, "means"));
>
> frmlc <- "yield ~ block + Error(N/block)"; # DOES NOT WORK
> aovfit <- aov(as.formula(frmlc), dat);
> print(model.tables(aovfit, "means"));
> }
>
> utils::data(npk, package="MASS");
> frmla <- "yield ~ block + Error(N/block)"; # WORKS
> aovfit <- aov(as.formula(frmla), npk);
> print(model.tables(aovfit, "means"));
> test(npk);
>
> ## End of example
>
>
> Output of sessionInfo():
> R version 2.11.1 (2010-05-31)
> i386-pc-mingw32
>
> locale:
> [1] LC_COLLATE=Finnish_Finland.1252 LC_CTYPE=Finnish_Finland.1252
> [3] LC_MONETARY=Finnish_Finland.1252 LC_NUMERIC=C
> [5] LC_TIME=Finnish_Finland.1252
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>
> loaded via a namespace (and not attached):
> [1] tools_2.11.1
>
> br Hannes G.
>
> ______________________________________________
> 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.
>
--
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
More information about the R-help
mailing list