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}^{9}\ (x_{1,j} - 11.8889)^2 + \sum_{j=1}^{8}\ (x_{2,j} - 11.625)^2 + \sum_{j=1}^{9}\ (x_{3,j} - 11.8889)^2 + \sum_{j=1}^{10}\ (x_{4,j} - 11.8)^2 + \sum_{j=1}^{3}\ (x_{5,j} - 14.3333)^2 \\[1em]
&= (x_{1,1} - 11.8889)^2\ + (x_{1,2} - 11.8889)^2\ + (x_{1,3} - 11.8889)^2\ + (x_{1,4} - 11.8889)^2\ + (x_{1,5} - 11.8889)^2\ + (x_{1,6} - 11.8889)^2\ + (x_{1,7} - 11.8889)^2\ + (x_{1,8} - 11.8889)^2\ + (x_{1,9} - 11.8889)^2\ + \\
& \qquad
(x_{2,1} - 11.625)^2\ + (x_{2,2} - 11.625)^2\ + (x_{2,3} - 11.625)^2\ + (x_{2,4} - 11.625)^2\ + (x_{2,5} - 11.625)^2\ + (x_{2,6} - 11.625)^2\ + (x_{2,7} - 11.625)^2\ + (x_{2,8} - 11.625)^2\ + \\
& \qquad
(x_{3,1} - 11.8889)^2\ + (x_{3,2} - 11.8889)^2\ + (x_{3,3} - 11.8889)^2\ + (x_{3,4} - 11.8889)^2\ + (x_{3,5} - 11.8889)^2\ + (x_{3,6} - 11.8889)^2\ + (x_{3,7} - 11.8889)^2\ + (x_{3,8} - 11.8889)^2\ + (x_{3,9} - 11.8889)^2\ + \\
& \qquad
(x_{4,1} - 11.8)^2\ + (x_{4,2} - 11.8)^2\ + (x_{4,3} - 11.8)^2\ + (x_{4,4} - 11.8)^2\ + (x_{4,5} - 11.8)^2\ + (x_{4,6} - 11.8)^2\ + (x_{4,7} - 11.8)^2\ + (x_{4,8} - 11.8)^2\ + (x_{4,9} - 11.8)^2\ + (x_{4,10} - 11.8)^2\ + \\
& \qquad
(x_{5,1} - 14.3333)^2\ +\ (x_{5,2} - 14.3333)^2\ +\ (x_{5,3} - 14.3333)^2 \\[1em]
&= (11 - 11.8889)^2\ + (8 - 11.8889)^2\ + (12 - 11.8889)^2\ + (8 - 11.8889)^2\ + (17 - 11.8889)^2\ + (12 - 11.8889)^2\ + (12 - 11.8889)^2\ + (11 - 11.8889)^2\ + (16 - 11.8889)^2\ + \\
& \qquad
(13 - 11.625)^2\ + (12 - 11.625)^2\ + (7 - 11.625)^2\ + (14 - 11.625)^2\ + (13 - 11.625)^2\ + (8 - 11.625)^2\ + (14 - 11.625)^2\ + (12 - 11.625)^2\ + \\
& \qquad
(12 - 11.8889)^2\ + (11 - 11.8889)^2\ + (12 - 11.8889)^2\ + (11 - 11.8889)^2\ + (14 - 11.8889)^2\ + (16 - 11.8889)^2\ + (2 - 11.8889)^2\ + (14 - 11.8889)^2\ + (15 - 11.8889)^2\ + \\
& \qquad
(14 - 11.8)^2\ + (14 - 11.8)^2\ + (11 - 11.8)^2\ + (17 - 11.8)^2\ + (9 - 11.8)^2\ + (7 - 11.8)^2\ + (10 - 11.8)^2\ + (10 - 11.8)^2\ + (15 - 11.8)^2\ + (11 - 11.8)^2\ + \\
& \qquad
(14 - 14.3333)^2\ +\ (15 - 14.3333)^2\ +\ (14 - 14.3333)^2 \\[1em]
&= (-0.8889)^2\ + (-3.8889)^2\ + (0.1111)^2\ + (-3.8889)^2\ + (5.1111)^2\ + (0.1111)^2\ + (0.1111)^2\ + (-0.8889)^2\ + (4.1111)^2\ + \\
& \qquad
(1.375)^2\ + (0.375)^2\ + (-4.625)^2\ + (2.375)^2\ + (1.375)^2\ + (-3.625)^2\ + (2.375)^2\ + (0.375)^2\ + \\
& \qquad
(0.1111)^2\ + (-0.8889)^2\ + (0.1111)^2\ + (-0.8889)^2\ + (2.1111)^2\ + (4.1111)^2\ + (-9.8889)^2\ + (2.1111)^2\ + (3.1111)^2\ + \\
& \qquad
(2.2)^2\ + (2.2)^2\ + (-0.8)^2\ + (5.2)^2\ + (-2.8)^2\ + (-4.8)^2\ + (-1.8)^2\ + (-1.8)^2\ + (3.2)^2\ + (-0.8)^2\ + \\
& \qquad
(-0.3333)^2\ +\ (0.6667)^2\ +\ (-0.3333)^2 \\[1em]
&= (0.7901)\ + (15.1235)\ + (0.0123)\ + (15.1235)\ + (26.1235)\ + (0.0123)\ + (0.0123)\ + (0.7901)\ + (16.9012)\ + \\
& \qquad
(1.8906)\ + (0.1406)\ + (21.3906)\ + (5.6406)\ + (1.8906)\ + (13.1406)\ + (5.6406)\ + (0.1406)\ + \\
& \qquad
(0.0123)\ + (0.7901)\ + (0.0123)\ + (0.7901)\ + (4.4568)\ + (16.9012)\ + (97.7901)\ + (4.4568)\ + (9.679)\ + \\
& \qquad
(4.84)\ + (4.84)\ + (0.64)\ + (27.04)\ + (7.84)\ + (23.04)\ + (3.24)\ + (3.24)\ + (10.24)\ + (0.64)\ + \\
& \qquad
(0.1111)\ +\ (0.4444)\ +\ (0.1111) \\[1em]
&= 74.8889\ + 49.875\ + 134.8889\ + 85.6\ + 0.5556 \\[1em]
&= 345.9194 \\[1em]
\end{align}
$$
From these calculations, the within sum of squares is SSW = 345.9194.
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(11, 8, 12, 8, 17, 12, 12, 11, 16)
treatment2 = c(13, 12, 7, 14, 13, 8, 14, 12)
treatment3 = c(12, 11, 12, 11, 14, 16, 2, 14, 15)
treatment4 = c(14, 14, 11, 17, 9, 7, 10, 10, 15, 11)
treatment5 = c(14, 15, 14)
## Change to Long Format
mmt = c( treatment1, treatment2, treatment3, treatment4, treatment5 )
grp = c( rep("trt1",9), rep("trt2",8), rep("trt3",9), rep("trt4",10), rep("trt5",3) )
## 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(11, 8, 12, 8, 17, 12, 12, 11, 16, 13, 12, 7, 14, 13, 8, 14, 12, 12, 11, 12, 11, 14, 16, 2, 14, 15, 14, 14, 11, 17, 9, 7, 10, 10, 15, 11, 14, 15, 14)
grp = c('trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', 'trt4', '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.