I have carried out this ANOVA:
summary(aov(drat ~ cyl, mtcars))
Df Sum Sq Mean Sq F value Pr(>F)
cyl 1 4.342 4.342 28.81 8.24e-06 ***
Residuals 30 4.521 0.151
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
According to the output, the sum of squares between groups is 4.342. I am trying to calculate the sum of squares using R code
First I have taken substracted the means of each group from the values and squared the result:
cyl_4_minus_mean <- (mtcars$drat[mtcars$cyl == 4] - mean(mtcars$drat[mtcars$cyl ==4]))^2
cyl_6_minus_mean <- (mtcars$drat[mtcars$cyl == 6] - mean(mtcars$drat[mtcars$cyl ==6]))^2
cyl_8_minus_mean <- (mtcars$drat[mtcars$cyl == 8] - mean(mtcars$drat[mtcars$cyl ==8]))^2
I then tried to multiply the results by the numbers of values within each group and the sum the result:
sum((11*cyl_4_minus_mean), (7*cyl_6_minus_mean), (14*cyl_8_minus_mean))
This gives 49.4459, which is different to 4.342 given by summary(aov(drat ~ cyl, mtcars))
.
Where am I going wrong here?