How to do one-way ANOVA in R with unequal sample s

2019-03-16 06:27发布

问题:

Trying to learn R. A question from an old stats text want's to know if there is a difference in break times at different construction sites. Trouble is, the text decided that each site employs a different number of workers. So, I am stuck and looking for help using ANOVA with unequal sample sizes.

site1 <- c(34,25,27,31,26,34,21)
site2 <- c(33,35,31,31,42,33)
site3 <- c(17,30,30,26,32,28,26,29)
site4 <- c(28,33,31,27,32,33,40)

回答1:

Try this one

site1 <- c(34,25,27,31,26,34,21)
site2 <- c(33,35,31,31,42,33)
site3 <- c(17,30,30,26,32,28,26,29)
site4 <- c(28,33,31,27,32,33,40)

Data <- data.frame(
       Y=c(site1, site2, site3, site4),
       Site =factor(rep(c("site1", "site2", "site3", "site4"), times=c(length(site1), length(site2), length(site3), length(site4))))
       )

Data

  Y  Site
 34 site1
 25 site1
 27 site1
 31 site1
 26 site1
 34 site1
 21 site1
 33 site2
 35 site2
 31 site2
 31 site2
 42 site2
 33 site2
 17 site3
 30 site3
 30 site3
 26 site3
 32 site3
 28 site3
 26 site3
 29 site3
 28 site4
 33 site4
 31 site4
 27 site4
 32 site4
 33 site4
 40 site4

ANOVA

fm1 <- aov(Y~Site, data=Data)
anova(fm1)

Output

Analysis of Variance Table

Response: Y
          Df Sum Sq Mean Sq F value  Pr(>F)  
Site       3 212.35  70.782  3.4971 0.03098 *
Residuals 24 485.76  20.240                  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1