Hello Again Gurus and Lurkers:
I?m trying to build a very user-friendly function which does aov without
having the user type in a formula (which would be tedious in this case).
The idea is to take the response from a PCA score matrix, and the factors
from a list. A simple example is the function given below, along with test
data and a sample call to the function.
I'm certainly having trouble understanding the proper ways to work with
formulas and related items, but I think what I do in the function should
work (it's built on ideas dug out of the archives). However, when the data
is passed to aov (directly or via manova), something in the bowels of aov
complains with the following error:
Error in model.frame.default(formula = form, drop.unused.levels = TRUE) :
object is not a matrix
To me, the formula looks legitimate, and the variables in the formula are
all in the environment (I think: The way I am doing this is basically that
described in ?summary.manova where only a formula is passed, no data
argument). Based upon reading the archives, the problem might arise with
one of the deparse statements in aov, but I can't resolve it. It might also
be one of scoping/environment, but again, this is only an idea.
TIA for any assistance. Bryan
*************
Bryan Hanson
Professor of Chemistry & Biochemistry
DePauw University, Greencastle IN USA
hypTestScores <-
function(mylist, score.matrix, pcs = 1:3, fac = NULL, ...) {
scores <- score.matrix[,pcs]
# str(scores) # looks correct to me
form <- as.formula(paste("scores", "~", paste(fac,
collapse = "*")),
env = parent.frame())
# str(form) # looks correct to me
attach(mylist)
if (length(pcs) > 1) out <- manova(formula = form, ...)
if (length(pcs) == 1) out <- aov(formula = form, ...)
print(summary(out))
detach(mylist)
invisible(out)
}
# test data
td1 <- matrix(rnorm(50), ncol = 5) # like PCA scores
td2 <- list(
f1 = as.factor(sample(c("A", "B"), 10, replace = TRUE)),
f2 = as.factor(sample(c("C", "D"), 10, replace = TRUE)))
# test call
hypTestScores(mylist = td2, score.matrix = td1,
fac = c("f1", "f2"))
detach("mylist") # needed if there is an error
> 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
[8] methods base
other attached packages:
[1] faraway_1.0.4 GGally_0.2 xtable_1.5-6
[4] mvbutils_2.5.1 ggplot2_0.8.8 proto_0.3-8
[7] reshape_0.8.3 ChemoSpec_1.45 R.utils_1.4.0
[10] R.oo_1.7.2 R.methodsS3_1.2.0 rgl_0.91
[13] lattice_0.18-5 mvoutlier_1.4 plyr_1.0.3
[16] RColorBrewer_1.0-2 chemometrics_0.8 som_0.3-5
[19] robustbase_0.5-0-1 rpart_3.1-46 pls_2.1-0
[22] pcaPP_1.8-1 mvtnorm_0.9-9 nnet_7.3-1
[25] mclust_3.4.4 MASS_7.3-5 lars_0.9-7
[28] e1071_1.5-23 class_7.3-2
Hi,
First, some toy data would have helped a lot, and that can explain why
you got so few answers...
Second, I have maybe some thoughts about it, not sure if this can help.
The error tells you that the data passed to aov is not a matrix. In your
function, you don't specify explicitly the data object to be used, that
might be a problem. And that object has to be a matrix, it might not be.
Maybe something like:
form<- as.formula(paste("pcs", "~", paste(fac, collapse =
"*")),
...
if (length(pcs) == 1) out<- aov(formula = form, data=score.matrix)
That would work if I understood what you're doing (hum...) and if both
"pcs" and "fac" are parts of "score.matrix". But
I've always done aov from a dataframe, which makes sense if you have both
numerical variables and factors. How can you do it from a matrix? Maybe my
understanding is limited.
As I said, just some thoughts...
HTH,
Ivan
Le 8/26/2010 14:47, Bryan Hanson a écrit :> Hello Again Gurus and Lurkers:
>
> I¹m trying to build a very user-friendly function which does aov without
> having the user type in a formula (which would be tedious in this case).
> The idea is to take the response from a PCA score matrix, and the factors
> from a list. A simple example is the function given below, along with test
> data and a sample call to the function.
>
> I'm certainly having trouble understanding the proper ways to work with
> formulas and related items, but I think what I do in the function should
> work (it's built on ideas dug out of the archives). However, when the
data
> is passed to aov (directly or via manova), something in the bowels of aov
> complains with the following error:
>
> Error in model.frame.default(formula = form, drop.unused.levels = TRUE) :
> object is not a matrix
>
> To me, the formula looks legitimate, and the variables in the formula are
> all in the environment (I think: The way I am doing this is basically that
> described in ?summary.manova where only a formula is passed, no data
> argument). Based upon reading the archives, the problem might arise with
> one of the deparse statements in aov, but I can't resolve it. It might
also
> be one of scoping/environment, but again, this is only an idea.
>
> TIA for any assistance. Bryan
> *************
> Bryan Hanson
> Professor of Chemistry& Biochemistry
> DePauw University, Greencastle IN USA
>
>
> hypTestScores<-
> function(mylist, score.matrix, pcs = 1:3, fac = NULL, ...) {
>
> scores<- score.matrix[,pcs]
> # str(scores) # looks correct to me
> form<- as.formula(paste("scores", "~",
paste(fac, collapse = "*")),
> env = parent.frame())
> # str(form) # looks correct to me
> attach(mylist)
> if (length(pcs)> 1) out<- manova(formula = form, ...)
> if (length(pcs) == 1) out<- aov(formula = form, ...)
> print(summary(out))
> detach(mylist)
> invisible(out)
> }
>
> # test data
> td1<- matrix(rnorm(50), ncol = 5) # like PCA scores
> td2<- list(
> f1 = as.factor(sample(c("A", "B"), 10, replace =
TRUE)),
> f2 = as.factor(sample(c("C", "D"), 10, replace =
TRUE)))
>
> # test call
> hypTestScores(mylist = td2, score.matrix = td1,
> fac = c("f1", "f2"))
> detach("mylist") # needed if there is an error
>
>> 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
> [8] methods base
>
> other attached packages:
> [1] faraway_1.0.4 GGally_0.2 xtable_1.5-6
> [4] mvbutils_2.5.1 ggplot2_0.8.8 proto_0.3-8
> [7] reshape_0.8.3 ChemoSpec_1.45 R.utils_1.4.0
> [10] R.oo_1.7.2 R.methodsS3_1.2.0 rgl_0.91
> [13] lattice_0.18-5 mvoutlier_1.4 plyr_1.0.3
> [16] RColorBrewer_1.0-2 chemometrics_0.8 som_0.3-5
> [19] robustbase_0.5-0-1 rpart_3.1-46 pls_2.1-0
> [22] pcaPP_1.8-1 mvtnorm_0.9-9 nnet_7.3-1
> [25] mclust_3.4.4 MASS_7.3-5 lars_0.9-7
> [28] e1071_1.5-23 class_7.3-2
>
> ______________________________________________
> R-help@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.
>
--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra@uni-hamburg.de
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
[[alternative HTML version deleted]]
On Aug 26, 2010, at 8:47 AM, Bryan Hanson wrote:> Hello Again Gurus and Lurkers: > > I?m trying to build a very user-friendly function which does aov > without > having the user type in a formula (which would be tedious in this > case). > The idea is to take the response from a PCA score matrix, and the > factors > from a list. A simple example is the function given below, along > with test > data and a sample call to the function. > > I'm certainly having trouble understanding the proper ways to work > with > formulas and related items, but I think what I do in the function > should > work (it's built on ideas dug out of the archives). However, when > the data > is passed to aov (directly or via manova), something in the bowels > of aov > complains with the following error: > > Error in model.frame.default(formula = form, drop.unused.levels = > TRUE) : > object is not a matrixActually I got a different error (also on a Mac. but with a version that is two month later than yours and with quite a smaller number of packages loaded): > hypTestScores(mylist = td2, score.matrix = td1, + fac = c("f1", "f2")) Error in eval(expr, envir, enclos) : object 'scores' not found Which went away if I removed the "env=" argument. My guess is that you were telling manova to look further up the lexical tree than it should have (or that attach() operation messed up the environment somehow): > hypTestScores(mylist = td2, score.matrix = td1, + fac = c("f1", "f2")) Df Pillai approx F num Df den Df Pr(>F) f1 1 0.31663 0.61777 3 4 0.6392 f2 1 0.36652 0.77145 3 4 0.5673 f1:f2 1 0.15687 0.24808 3 4 0.8593 Residuals 6 > detach("mylist") # needed if there is an error Error in detach("mylist") : invalid 'name' argument You might want to do inside the function something like res =try( your function ) if (res="try-error") {stop() } else{ <process>}> > To me, the formula looks legitimate, and the variables in the > formula are > all in the environment (I think: The way I am doing this is > basically that > described in ?summary.manova where only a formula is passed, no data > argument). Based upon reading the archives, the problem might arise > with > one of the deparse statements in aov, but I can't resolve it. It > might also > be one of scoping/environment, but again, this is only an idea. > > TIA for any assistance. Bryan > ************* > Bryan Hanson > Professor of Chemistry & Biochemistry > DePauw University, Greencastle IN USA > > > hypTestScores <- > function(mylist, score.matrix, pcs = 1:3, fac = NULL, ...) { > > scores <- score.matrix[,pcs] > # str(scores) # looks correct to me > form <- as.formula(paste("scores", "~", paste(fac, collapse = > "*")), > env = parent.frame()) > # str(form) # looks correct to me > attach(mylist) > if (length(pcs) > 1) out <- manova(formula = form, ...) > if (length(pcs) == 1) out <- aov(formula = form, ...) > print(summary(out)) > detach(mylist) > invisible(out) > } > > # test data > td1 <- matrix(rnorm(50), ncol = 5) # like PCA scores > td2 <- list( > f1 = as.factor(sample(c("A", "B"), 10, replace = TRUE)), > f2 = as.factor(sample(c("C", "D"), 10, replace = TRUE))) > > # test call > hypTestScores(mylist = td2, score.matrix = td1, > fac = c("f1", "f2")) > detach("mylist") # needed if there is an error > >> 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 > [8] methods base > > other attached packages: > [1] faraway_1.0.4 GGally_0.2 xtable_1.5-6 > [4] mvbutils_2.5.1 ggplot2_0.8.8 proto_0.3-8 > [7] reshape_0.8.3 ChemoSpec_1.45 R.utils_1.4.0 > [10] R.oo_1.7.2 R.methodsS3_1.2.0 rgl_0.91 > [13] lattice_0.18-5 mvoutlier_1.4 plyr_1.0.3 > [16] RColorBrewer_1.0-2 chemometrics_0.8 som_0.3-5 > [19] robustbase_0.5-0-1 rpart_3.1-46 pls_2.1-0 > [22] pcaPP_1.8-1 mvtnorm_0.9-9 nnet_7.3-1 > [25] mclust_3.4.4 MASS_7.3-5 lars_0.9-7 > [28] e1071_1.5-23 class_7.3-2 > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Hi! I didn't see the toy data, my bad. I guess David's solution fixed the problem. Ivan Le 8/26/2010 16:10, Bryan Hanson a écrit :> Hi Ivan, there is toy data given in the original post. > > The object passed to aov is a matrix, it is called scores, and it is passed > via the formula that is created in the sense that the formula specifies > objects in the calling environment. At least that is what is supposed to > happen. > > I've not been able to find an example of manova using a data frame - aov, > yes. I think that is because the response vector in manova must be a > matrix, and you can't put a matrix in a data frame as a separate object. > You could put the columns of the matrix into the data frame, but then it is > not seen as a single entity, it's seen as separate columns, and specifying > this in the formula looks impossible as I understand it. > > But, obviously there is something I don't understand! Thanks, Bryan > > > On 8/26/10 9:50 AM, "Ivan Calandra"<ivan.calandra@uni-hamburg.de> wrote: > >> Hi, >> >> First, some toy data would have helped a lot, and that can explain why >> you got so few answers... >> >> Second, I have maybe some thoughts about it, not sure if this can help. >> >> The error tells you that the data passed to aov is not a matrix. In your >> function, you don't specify explicitly the data object to be used, that >> might be a problem. And that object has to be a matrix, it might not be. >> Maybe something like: >> >> form<- as.formula(paste("pcs", "~", paste(fac, collapse = "*")), >> ... >> if (length(pcs) == 1) out<- aov(formula = form, data=score.matrix) >> >> That would work if I uderstood what you're doing (hum...) and if both "pcs" >> and "fac" are parts of "score.matrix". But I've always done aov from a >> dataframe, which makes sense if you have both numerical variables and factors. >> How can you do it from a matrix? Maybe my understanding is limited. >> >> As I said, just some thoughts... >> HTH, >> Ivan >> >> >> Le 8/26/2010 14:47, Bryan Hanson a écrit : >>> Hello Again Gurus and Lurkers: >>> >>> I¹m trying to build a very user-friendly function which does aov without >>> having the user type in a formula (which would be tedious in this case). >>> The idea is to take the response from a PCA score matrix, and the factors >>> from a list. A simple example is the function given below, along with test >>> data and a sample call to the function. >>> >>> I'm certainly having trouble understanding the proper ways to work with >>> formulas and related items, but I think what I do in the function should >>> work (it's built on ideas dug out of the archives). However, when the data >>> is passed to aov (directly or via manova), something in the bowels of aov >>> complains with the following error: >>> >>> Error in model.frame.default(formula = form, drop.unused.levels = TRUE) : >>> object is not a matrix >>> >>> To me, the formula looks legitimate, and the variables in the formula are >>> all in the environment (I think: The way I am doing this is basically that >>> described in ?summary.manova where only a formula is passed, no data >>> argument). Based upon reading the archives, the problem might arise with >>> one of the deparse statements in aov, but I can't resolve it. It might also >>> be one of scoping/environment, but again, this is only an idea. >>> >>> TIA for any assistance. Bryan >>> ************* >>> Bryan Hanson >>> Professor of Chemistry& Biochemistry >>> DePauw University, Greencastle IN USA >>> >>> >>> hypTestScores<- >>> function(mylist, score.matrix, pcs = 1:3, fac = NULL, ...) { >>> >>> scores<- score.matrix[,pcs] >>> # str(scores) # looks correct to me >>> form<- as.formula(paste("scores", "~", paste(fac, collapse = "*")), >>> env = parent.frame()) >>> # str(form) # looks correct to me >>> attach(mylist) >>> if (length(pcs)> 1) out<- manova(formula = form, ...) >>> if (length(pcs) == 1) out<- aov(formula = form, ...) >>> print(summary(out)) >>> detach(mylist) >>> invisible(out) >>> } >>> >>> # test data >>> td1<- matrix(rnorm(50), ncol = 5) # like PCA scores >>> td2<- list( >>> f1 = as.factor(sample(c("A", "B"), 10, replace = TRUE)), >>> f2 = as.factor(sample(c("C", "D"), 10, replace = TRUE))) >>> >>> # test call >>> hypTestScores(mylist = td2, score.matrix = td1, >>> fac = c("f1", "f2")) >>> detach("mylist") # needed if there is an error >>> >>>> 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 >>> [8] methods base >>> >>> other attached packages: >>> [1] faraway_1.0.4 GGally_0.2 xtable_1.5-6 >>> [4] mvbutils_2.5.1 ggplot2_0.8.8 proto_0.3-8 >>> [7] reshape_0.8.3 ChemoSpec_1.45 R.utils_1.4.0 >>> [10] R.oo_1.7.2 R.methodsS3_1.2.0 rgl_0.91 >>> [13] lattice_0.18-5 mvoutlier_1.4 plyr_1.0.3 >>> [16] RColorBrewer_1.0-2 chemometrics_0.8 som_0.3-5 >>> [19] robustbase_0.5-0-1 rpart_3.1-46 pls_2.1-0 >>> [22] pcaPP_1.8-1 mvtnorm_0.9-9 nnet_7.3-1 >>> [25] mclust_3.4.4 MASS_7.3-5 lars_0.9-7 >>> [28] e1071_1.5-23 class_7.3-2 >>> >>> ______________________________________________ >>> R-help@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. >>> > >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra@uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php [[alternative HTML version deleted]]