[R] Gathering different data types for aov etc

Bryan Hanson hanson at depauw.edu
Thu Aug 19 22:55:29 CEST 2010


Hello List Folks.  I¹m trying to do manova using a function which assembles
the response (LHS) and factors (RHS) from different sources.  When I do so,
the model routines complain that I'm passing a list for the RHS when I don't
think I am.

Here's a toy example:

r <- matrix(rnorm(30), ncol = 3) # toy data
td <- list(d = rnorm(10),
    f1 = sample(c("A", "B"), 10, replace = TRUE),
    f2 = sample(c("C", "D"), 10, replace = TRUE))

toyFn <-
function(my.list, my.data, fac = NULL) {
    response <- my.data
    fac <- data.frame(my.list[fac])
#    str(fac) # a data frame!
    man <- manova(response ~ fac)
    print(summary(man))
    invisible(man)
    }

toyFn(td, r, fac = "f1")
toyFn(td, r, fac = c("f1", "f2"))

Bot attempts just above gives the following error:

Error in model.frame.default(formula = response ~ fac, drop.unused.levels =
TRUE) : 
  invalid type (list) for variable 'fac'

But fac inside the function is a data frame.  What am I missing here?

Also, I want the function to use this formula: response ~ f1:f2 but once I
get past my problem of data type, I don't think what I have so far will do
the interaction, in fact, I suspect it will use only f1 or perhaps f1 + f2,
just a hunch.  Any suggestions to make this more elegant and robust? I've
been reading about model frames etc but don't quite see how to do a simple
example such as this.

Thanks, Bryan
*************
Bryan Hanson
Professor of Chemistry & Biochemistry
DePauw University, Greencastle IN USA

> sessionInfo()
R version 2.11.0 (2010-04-22)
x86_64-apple-darwin9.8.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] datasets  tools     grid      graphics  grDevices utils     stats
methods   base     

other attached packages:
 [1] faraway_1.0.4      GGally_0.2         xtable_1.5-6       mvbutils_2.5.1
ggplot2_0.8.8     
 [6] proto_0.3-8        reshape_0.8.3      ChemoSpec_1.45     R.utils_1.4.0
R.oo_1.7.2        
[11] R.methodsS3_1.2.0  rgl_0.91           lattice_0.18-5     mvoutlier_1.4
plyr_1.0.3        
[16] RColorBrewer_1.0-2 chemometrics_0.8   som_0.3-5
robustbase_0.5-0-1 rpart_3.1-46
[21] pls_2.1-0          pcaPP_1.8-1        mvtnorm_0.9-9      nnet_7.3-1
mclust_3.4.4      
[26] MASS_7.3-5         lars_0.9-7         e1071_1.5-23       class_7.3-2



More information about the R-help mailing list