Hide Solution
$$ \begin{align}
\text{SSW} &= \sum_{i=1}^g \sum_{j=1}^{n_i}\ (x_{i,j} - \bar{x}_i)^2 \\[3em]
&= \sum_{i=1}^{5} \sum_{j=1}^{n_i}\ (x_{i,j} - \bar{x}_i)^2 \\[1em]
&= \sum_{j=1}^{3}\ (x_{1,j} - 13.3333)^2 + \sum_{j=1}^{9}\ (x_{2,j} - 8.8889)^2 + \sum_{j=1}^{7}\ (x_{3,j} - 10.2857)^2 + \sum_{j=1}^{10}\ (x_{4,j} - 12.8)^2 + \sum_{j=1}^{7}\ (x_{5,j} - 8)^2 \\[1em]
&= (x_{1,1} - 13.3333)^2\ + (x_{1,2} - 13.3333)^2\ + (x_{1,3} - 13.3333)^2\ + \\
& \qquad
(x_{2,1} - 8.8889)^2\ + (x_{2,2} - 8.8889)^2\ + (x_{2,3} - 8.8889)^2\ + (x_{2,4} - 8.8889)^2\ + (x_{2,5} - 8.8889)^2\ + (x_{2,6} - 8.8889)^2\ + (x_{2,7} - 8.8889)^2\ + (x_{2,8} - 8.8889)^2\ + (x_{2,9} - 8.8889)^2\ + \\
& \qquad
(x_{3,1} - 10.2857)^2\ + (x_{3,2} - 10.2857)^2\ + (x_{3,3} - 10.2857)^2\ + (x_{3,4} - 10.2857)^2\ + (x_{3,5} - 10.2857)^2\ + (x_{3,6} - 10.2857)^2\ + (x_{3,7} - 10.2857)^2\ + \\
& \qquad
(x_{4,1} - 12.8)^2\ + (x_{4,2} - 12.8)^2\ + (x_{4,3} - 12.8)^2\ + (x_{4,4} - 12.8)^2\ + (x_{4,5} - 12.8)^2\ + (x_{4,6} - 12.8)^2\ + (x_{4,7} - 12.8)^2\ + (x_{4,8} - 12.8)^2\ + (x_{4,9} - 12.8)^2\ + (x_{4,10} - 12.8)^2\ + \\
& \qquad
(x_{5,1} - 8)^2\ +\ (x_{5,2} - 8)^2\ +\ (x_{5,3} - 8)^2\ +\ (x_{5,4} - 8)^2\ +\ (x_{5,5} - 8)^2\ +\ (x_{5,6} - 8)^2\ +\ (x_{5,7} - 8)^2 \\[1em]
&= (10 - 13.3333)^2\ + (11 - 13.3333)^2\ + (19 - 13.3333)^2\ + \\
& \qquad
(5 - 8.8889)^2\ + (20 - 8.8889)^2\ + (7 - 8.8889)^2\ + (1 - 8.8889)^2\ + (13 - 8.8889)^2\ + (9 - 8.8889)^2\ + (21 - 8.8889)^2\ + (-1 - 8.8889)^2\ + (5 - 8.8889)^2\ + \\
& \qquad
(10 - 10.2857)^2\ + (0 - 10.2857)^2\ + (12 - 10.2857)^2\ + (20 - 10.2857)^2\ + (4 - 10.2857)^2\ + (13 - 10.2857)^2\ + (13 - 10.2857)^2\ + \\
& \qquad
(22 - 12.8)^2\ + (20 - 12.8)^2\ + (0 - 12.8)^2\ + (8 - 12.8)^2\ + (8 - 12.8)^2\ + (23 - 12.8)^2\ + (8 - 12.8)^2\ + (10 - 12.8)^2\ + (9 - 12.8)^2\ + (20 - 12.8)^2\ + \\
& \qquad
(3 - 8)^2\ +\ (-4 - 8)^2\ +\ (14 - 8)^2\ +\ (11 - 8)^2\ +\ (3 - 8)^2\ +\ (11 - 8)^2\ +\ (18 - 8)^2 \\[1em]
&= (-3.3333)^2\ + (-2.3333)^2\ + (5.6667)^2\ + \\
& \qquad
(-3.8889)^2\ + (11.1111)^2\ + (-1.8889)^2\ + (-7.8889)^2\ + (4.1111)^2\ + (0.1111)^2\ + (12.1111)^2\ + (-9.8889)^2\ + (-3.8889)^2\ + \\
& \qquad
(-0.2857)^2\ + (-10.2857)^2\ + (1.7143)^2\ + (9.7143)^2\ + (-6.2857)^2\ + (2.7143)^2\ + (2.7143)^2\ + \\
& \qquad
(9.2)^2\ + (7.2)^2\ + (-12.8)^2\ + (-4.8)^2\ + (-4.8)^2\ + (10.2)^2\ + (-4.8)^2\ + (-2.8)^2\ + (-3.8)^2\ + (7.2)^2\ + \\
& \qquad
(-5)^2\ +\ (-12)^2\ +\ (6)^2\ +\ (3)^2\ +\ (-5)^2\ +\ (3)^2\ +\ (10)^2 \\[1em]
&= (11.1111)\ + (5.4444)\ + (32.1111)\ + \\
& \qquad
(15.1235)\ + (123.4568)\ + (3.5679)\ + (62.2346)\ + (16.9012)\ + (0.0123)\ + (146.679)\ + (97.7901)\ + (15.1235)\ + \\
& \qquad
(0.0816)\ + (105.7959)\ + (2.9388)\ + (94.3673)\ + (39.5102)\ + (7.3673)\ + (7.3673)\ + \\
& \qquad
(84.64)\ + (51.84)\ + (163.84)\ + (23.04)\ + (23.04)\ + (104.04)\ + (23.04)\ + (7.84)\ + (14.44)\ + (51.84)\ + \\
& \qquad
(25)\ +\ (144)\ +\ (36)\ +\ (9)\ +\ (25)\ +\ (9)\ +\ (100) \\[1em]
&= 48.6667\ + 480.8889\ + 257.4286\ + 547.6\ + 248 \\[1em]
&= 1682.5841 \\[1em]
\end{align}
$$
From these calculations, the within sum of squares is SSW = 1682.5841.
Hide the R Code
There are two ways of performing these calculations in R. The method you select will depend on how your data are stored.
Method 1: Wide Format
Copy and paste the following code into your R script window, then run it from there.
## Import data
treatment1 = c(10, 11, 19)
treatment2 = c(5, 20, 7, 1, 13, 9, 21, -1, 5)
treatment3 = c(10, 0, 12, 20, 4, 13, 13)
treatment4 = c(22, 20, 0, 8, 8, 23, 8, 10, 9, 20)
treatment5 = c(3, -4, 14, 11, 3, 11, 18)
## Change to Long Format
mmt = c( treatment1, treatment2, treatment3, treatment4, treatment5 )
grp = c( rep("trt1",3), rep("trt2",9), rep("trt3",7), rep("trt4",10), rep("trt5",7) )
## Model the data
mod = aov(mmt~grp)
summary(mod)
In the R output, the value of the sum of squares within is the number in the table under Sum Sq
and to the right of Residuals
. If you would like better precision for that value, or if you would like to have only that value, run the following code in addition to that above:
modSummary = summary(mod)
modSummary[[1]][2,2]
Here, the number outputted is the sum of squares between. How did you get the number? The summary table (also known as an ANOVA table) is just a table. Thus, the first line saves the table as the variable modSummary
the last line looks inside that variable, selects the ANOVA table ([[1]]
), and then selects the row 2, column 2 value.
Method 2: Long Format
Copy and paste the following code into your R script window, then run it from there.
## Import data
yields = c(10, 11, 19, 5, 20, 7, 1, 13, 9, 21, -1, 5, 10, 0, 12, 20, 4, 13, 13, 22, 20, 0, 8, 8, 23, 8, 10, 9, 20, 3, -4, 14, 11, 3, 11, 18)
grp = c('trt1', 'trt1', 'trt1', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt5', 'trt5', 'trt5', 'trt5', 'trt5', 'trt5', 'trt5')
## Model the data
mod = aov(yields~grp)
summary(mod)
As discussed above, in the R output, the value of the sum of squares within is the number in the table under Sum Sq
and to the right of Residuals
. If you would like better precision for that value, or if you would like to have only that value, run the following code in addition to that above:
modSummary = summary(mod)
modSummary[[1]][2,2]
Here, the number outputted is the sum of squares between. How did you get the number? The summary table (also known as an ANOVA table) is just a table. Thus, the first line saves the table as the variable modSummary
the last line looks inside that variable, selects the ANOVA table ([[1]]
), and then selects the row 2, column 2 value.
Note: The difference between wide and long formats is this: In wide formatted data, each group has its own variable. In long formatted data, the group number is a variable.