# Calculating sum of squares between groups

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?

• To me it seems you don't calculate the sum of squares correctly; you have to subtract the total mean of the group from the group mean in the first step. Feb 4, 2014 at 10:38

First, you are treating the number of cylinders as three distinct groups rather than a quantitative regression. Therefore, you have to treat them as factors.

> summary(aov(drat~as.factor(cyl),mtcars))
Df Sum Sq Mean Sq F value   Pr(>F)
as.factor(cyl)  2  4.364  2.1822   14.07 5.36e-05 ***
Residuals      29  4.498  0.1551
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


Also, if you were to subtract the means from each group from the values, this will give you the sum of squares residuals, not the sum of squares from the treatments. In this case, you do not need to multiply by the number in each group.

> 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

> sum(cyl_4_minus_mean,cyl_6_minus_mean,cyl_8_minus_mean)
 4.497955


If you did want to find the sum of squares between groups, then you need to subtract the mean from each group from the total mean.

> mean4 = mean(mtcars$drat[mtcars$cyl == 4])
> mean6 = mean(mtcars$drat[mtcars$cyl == 6])
> mean8 = mean(mtcars$drat[mtcars$cyl == 8])
> meantotal = mean(mtcars\$drat)
> 11*(mean4-meantotal)^2 + 7*(mean6-meantotal)^2 + 14*(mean8-meantotal)^2
 4.364367


Here is a more generalized solution using tidyverse.

library(tidyverse)
mtcars %>%
select(cyl, drat) %>%
group_by(cyl) %>%
mutate(
n = n(),
drat_mean = mean(drat),
diff = (drat - drat_mean) ^ 2
) %>%
pull(diff) %>%
sum()

 4.497955

mtcars %>%
select(cyl, drat) %>%
mutate(grand_mean = mean(drat)) %>%
group_by(cyl) %>%
summarise(n = n(),
treatment_mean = mean(drat),
grand_mean = mean(grand_mean)) %>%
mutate(ss_treatment = n * (treatment_mean - grand_mean)^2) %>%
pull(ss_treatment) %>%
sum()

 4.364367