Dear Robert, It is easier to use lm instead of aov if you want coefficients for each group. Note that you can use rnorm vectorised.
set.seed(0) N <- 100 # sample size MEAN <- c(10, 20, 30, 40, 50) VAR <- c(20,20,1, 20, 20) LABELS <- factor(c("A", "B", "C", "D", "E")) # create a data frame with labels df <- data.frame(Label=rep(LABELS, each=N)) df$Value <- rnorm(nrow(df), mean = MEAN[df$Label], sd = sqrt(VAR[df$Label])) mod_aov <- aov(Value ~ Label, data=df) mod_lm <- lm(Value ~ Label, data = df) all.equal(anova(mod_aov), anova(mod_lm)) summary(mod_aov) summary(mod_lm) summary(mod_lm)$coef confint(mod_lm) #without intercept mod_lm0 <- lm(Value ~ 0 + Label, data = df) summary(mod_lm0)$coef confint(mod_lm0) Best regards, Thierry ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium + 32 2 525 02 51 + 32 54 43 61 85 thierry.onkel...@inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Namens Robert Latest Verzonden: vrijdag 11 mei 2012 9:37 Aan: r-help@r-project.org Onderwerp: [R] ANOVA question Hello all, I'm very satisfied to say that my grip on both R and statistics is showing the first hints of firmness, on a very greenhorn level. I'm faced with a problem that I intend to analyze using ANOVA, and to test my understanding of a primitive, one-way ANOVA I've written the self-contained practice script below. It works as expected. But here's my question: How can I not only get the values of the coefficients for the different levels of the explanatory factor(s), but also the corresponding standard errors and confidence levels? Below I have started doing that "on foot" by looping over the levels of my single factor, but I suppose this gets complicated and messy with more complex models. Any ideas? Thanks, robert set.seed(0) N <- 100 # sample size MEAN <- c(10, 20, 30, 40, 50) VAR <- c(20,20,1, 20, 20) LABELS <- c("A", "B", "C", "D", "E") # create a data frame with labels df <- data.frame(Label=rep(LABELS, each=N)) df$Value <- NA # fill in random data for each factor level for (i in 1:length(MEAN)) { df$Value[(1+N*(i-1)):(N*i)] <- rnorm(N, MEAN[i], sqrt(VAR[i])) } par(mfrow=c(2,2)) plot(df) # Box plot of the data plot(df$Value) # scatter plot mod_aov <- aov(Value ~ Label, data=df) print(summary(mod_aov)) print(mod_aov$coefficients) rsd <- mod_aov$residuals plot(rsd) # find and print mean() and var() for each level for (l in levels(df$Label)) { index <- df$Label == l # Method 1: directly from data smp <- df$Value[index] # extract sample for this label ssq_smp <- var(smp)*(length(smp)-1) # sum of squares is variance # times d.f. # Method 2: from ANOVA residuals rsd_grp <- rsd[index] # extract residuals ssq_rsd <- sum(rsd_grp **2) # compute sum of squares # print mean, variance, and difference between SSQs from the two # methods. write(sprintf("%s: mean=%5.1f var=%5.1f (%.2g)", l, mean(smp), var(smp), ssq_smp-ssq_rsd), "") # ...and it works like expected! But is there a shortcut that would give me # the same result in a one-liner? } ______________________________________________ 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. * * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * * Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document. ______________________________________________ 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.