[R] ANOVA problem
Richard M. Heiberger
rmh at temple.edu
Fri May 4 20:56:55 CEST 2012
The following constructs the data.frame that I think the original
poster asked for.
I don't understand the graph, so I didn't attempt it.
I agree with Bert that this might not make sense. Specifically, the distinction
between AB01 and AB02 is not modeled, and that is probably the critical factor.
I made several style changes in the dataset. The name "df" is a function name,
and its use as a data.frame name will lead the reader to confusion.
I constructed the data.frame directly, not by constructing vectors and putting
them together. I declared the factor variables to be factors. For factors with
more than two levels, this assures that they get the right number of degrees of
freedom in the anova table.
rg <- data.frame(G=c("AB01","AB01","AB01","AB01","AB01","AB01","AB01","AB01",
"AB02","AB02","AB02","AB02","AB02","AB02","AB02","AB02"),
L=factor(c(1,1,1,1,2,2,2,2,1,1,1,1,2,2,2,2)),
S=factor(c("m","m","f","f","m","m","f","f",
"m","m","f","f","m","m","f","f")),
R=factor(c(1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2)),
V=c(1,2,12,21,5,6,12,34,1,6,52,41,5,43,13,24))
summary(aov(V ~ S * L, data=rg[1:8,])) ## no Error term, to be sure we
understand
rg.aov <- lapply(split(rg, rg$G),
function(x) aov(V ~ S*L + Error(L), data=x))
summary(rg.aov[[1]]) ## same Sums of Squares as above, but now with Error term
anovaSumsOfSquares <- function(list.of.aov.objects) {
t(sapply(rg.aov, function(y) {
tmpy <-
sapply(y[-1], function(x) {
tmp <- summary(x)[[1]]
nt <- sub(" +$", "", rownames(tmp))
result <- tmp[,"Sum Sq"]
names(result) <- nt
result})
c(tmpy[[1]], tmpy[[2]])
}))
}
anovaSumsOfSquares(rg.aov)
More information about the R-help
mailing list