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}^{12}\ (x_{1,j} - 8.25)^2 + \sum_{j=1}^{9}\ (x_{2,j} - 9.3333)^2 + \sum_{j=1}^{6}\ (x_{3,j} - 9.6667)^2 + \sum_{j=1}^{7}\ (x_{4,j} - 9)^2 + \sum_{j=1}^{9}\ (x_{5,j} - 10.8889)^2 \\[1em]
&= (x_{1,1} - 8.25)^2\ + (x_{1,2} - 8.25)^2\ + (x_{1,3} - 8.25)^2\ + (x_{1,4} - 8.25)^2\ + (x_{1,5} - 8.25)^2\ + (x_{1,6} - 8.25)^2\ + (x_{1,7} - 8.25)^2\ + (x_{1,8} - 8.25)^2\ + (x_{1,9} - 8.25)^2\ + (x_{1,10} - 8.25)^2\ + (x_{1,11} - 8.25)^2\ + (x_{1,12} - 8.25)^2\ + \\
& \qquad
(x_{2,1} - 9.3333)^2\ + (x_{2,2} - 9.3333)^2\ + (x_{2,3} - 9.3333)^2\ + (x_{2,4} - 9.3333)^2\ + (x_{2,5} - 9.3333)^2\ + (x_{2,6} - 9.3333)^2\ + (x_{2,7} - 9.3333)^2\ + (x_{2,8} - 9.3333)^2\ + (x_{2,9} - 9.3333)^2\ + \\
& \qquad
(x_{3,1} - 9.6667)^2\ + (x_{3,2} - 9.6667)^2\ + (x_{3,3} - 9.6667)^2\ + (x_{3,4} - 9.6667)^2\ + (x_{3,5} - 9.6667)^2\ + (x_{3,6} - 9.6667)^2\ + \\
& \qquad
(x_{4,1} - 9)^2\ + (x_{4,2} - 9)^2\ + (x_{4,3} - 9)^2\ + (x_{4,4} - 9)^2\ + (x_{4,5} - 9)^2\ + (x_{4,6} - 9)^2\ + (x_{4,7} - 9)^2\ + \\
& \qquad
(x_{5,1} - 10.8889)^2\ +\ (x_{5,2} - 10.8889)^2\ +\ (x_{5,3} - 10.8889)^2\ +\ (x_{5,4} - 10.8889)^2\ +\ (x_{5,5} - 10.8889)^2\ +\ (x_{5,6} - 10.8889)^2\ +\ (x_{5,7} - 10.8889)^2\ +\ (x_{5,8} - 10.8889)^2\ +\ (x_{5,9} - 10.8889)^2 \\[1em]
&= (13 - 8.25)^2\ + (4 - 8.25)^2\ + (11 - 8.25)^2\ + (6 - 8.25)^2\ + (10 - 8.25)^2\ + (11 - 8.25)^2\ + (4 - 8.25)^2\ + (6 - 8.25)^2\ + (8 - 8.25)^2\ + (13 - 8.25)^2\ + (8 - 8.25)^2\ + (5 - 8.25)^2\ + \\
& \qquad
(11 - 9.3333)^2\ + (5 - 9.3333)^2\ + (7 - 9.3333)^2\ + (10 - 9.3333)^2\ + (7 - 9.3333)^2\ + (14 - 9.3333)^2\ + (10 - 9.3333)^2\ + (14 - 9.3333)^2\ + (6 - 9.3333)^2\ + \\
& \qquad
(7 - 9.6667)^2\ + (10 - 9.6667)^2\ + (10 - 9.6667)^2\ + (12 - 9.6667)^2\ + (8 - 9.6667)^2\ + (11 - 9.6667)^2\ + \\
& \qquad
(9 - 9)^2\ + (11 - 9)^2\ + (7 - 9)^2\ + (11 - 9)^2\ + (6 - 9)^2\ + (14 - 9)^2\ + (5 - 9)^2\ + \\
& \qquad
(6 - 10.8889)^2\ +\ (11 - 10.8889)^2\ +\ (11 - 10.8889)^2\ +\ (8 - 10.8889)^2\ +\ (13 - 10.8889)^2\ +\ (17 - 10.8889)^2\ +\ (14 - 10.8889)^2\ +\ (10 - 10.8889)^2\ +\ (8 - 10.8889)^2 \\[1em]
&= (4.75)^2\ + (-4.25)^2\ + (2.75)^2\ + (-2.25)^2\ + (1.75)^2\ + (2.75)^2\ + (-4.25)^2\ + (-2.25)^2\ + (-0.25)^2\ + (4.75)^2\ + (-0.25)^2\ + (-3.25)^2\ + \\
& \qquad
(1.6667)^2\ + (-4.3333)^2\ + (-2.3333)^2\ + (0.6667)^2\ + (-2.3333)^2\ + (4.6667)^2\ + (0.6667)^2\ + (4.6667)^2\ + (-3.3333)^2\ + \\
& \qquad
(-2.6667)^2\ + (0.3333)^2\ + (0.3333)^2\ + (2.3333)^2\ + (-1.6667)^2\ + (1.3333)^2\ + \\
& \qquad
(0)^2\ + (2)^2\ + (-2)^2\ + (2)^2\ + (-3)^2\ + (5)^2\ + (-4)^2\ + \\
& \qquad
(-4.8889)^2\ +\ (0.1111)^2\ +\ (0.1111)^2\ +\ (-2.8889)^2\ +\ (2.1111)^2\ +\ (6.1111)^2\ +\ (3.1111)^2\ +\ (-0.8889)^2\ +\ (-2.8889)^2 \\[1em]
&= (22.5625)\ + (18.0625)\ + (7.5625)\ + (5.0625)\ + (3.0625)\ + (7.5625)\ + (18.0625)\ + (5.0625)\ + (0.0625)\ + (22.5625)\ + (0.0625)\ + (10.5625)\ + \\
& \qquad
(2.7778)\ + (18.7778)\ + (5.4444)\ + (0.4444)\ + (5.4444)\ + (21.7778)\ + (0.4444)\ + (21.7778)\ + (11.1111)\ + \\
& \qquad
(7.1111)\ + (0.1111)\ + (0.1111)\ + (5.4444)\ + (2.7778)\ + (1.7778)\ + \\
& \qquad
(0)\ + (4)\ + (4)\ + (4)\ + (9)\ + (25)\ + (16)\ + \\
& \qquad
(23.9012)\ +\ (0.0123)\ +\ (0.0123)\ +\ (8.3457)\ +\ (4.4568)\ +\ (37.3457)\ +\ (9.679)\ +\ (0.7901)\ +\ (8.3457) \\[1em]
&= 120.25\ + 88\ + 17.3333\ + 62\ + 84.5432 \\[1em]
&= 380.4722 \\[1em]
\end{align}
$$
From these calculations, the within sum of squares is SSW = 380.4722.
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(13, 4, 11, 6, 10, 11, 4, 6, 8, 13, 8, 5)
treatment2 = c(11, 5, 7, 10, 7, 14, 10, 14, 6)
treatment3 = c(7, 10, 10, 12, 8, 11)
treatment4 = c(9, 11, 7, 11, 6, 14, 5)
treatment5 = c(6, 11, 11, 8, 13, 17, 14, 10, 8)
## Change to Long Format
mmt = c( treatment1, treatment2, treatment3, treatment4, treatment5 )
grp = c( rep("trt1",12), rep("trt2",9), rep("trt3",6), rep("trt4",7), rep("trt5",9) )
## 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(13, 4, 11, 6, 10, 11, 4, 6, 8, 13, 8, 5, 11, 5, 7, 10, 7, 14, 10, 14, 6, 7, 10, 10, 12, 8, 11, 9, 11, 7, 11, 6, 14, 5, 6, 11, 11, 8, 13, 17, 14, 10, 8)
grp = c('trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt5', 'trt5', '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.