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}^{6}\ (x_{1,j} - 11)^2 + \sum_{j=1}^{9}\ (x_{2,j} - 8.1111)^2 + \sum_{j=1}^{8}\ (x_{3,j} - 8.25)^2 + \sum_{j=1}^{11}\ (x_{4,j} - 10.3636)^2 + \sum_{j=1}^{7}\ (x_{5,j} - 7.4286)^2 \\[1em]
&= (x_{1,1} - 11)^2\ + (x_{1,2} - 11)^2\ + (x_{1,3} - 11)^2\ + (x_{1,4} - 11)^2\ + (x_{1,5} - 11)^2\ + (x_{1,6} - 11)^2\ + \\
& \qquad
(x_{2,1} - 8.1111)^2\ + (x_{2,2} - 8.1111)^2\ + (x_{2,3} - 8.1111)^2\ + (x_{2,4} - 8.1111)^2\ + (x_{2,5} - 8.1111)^2\ + (x_{2,6} - 8.1111)^2\ + (x_{2,7} - 8.1111)^2\ + (x_{2,8} - 8.1111)^2\ + (x_{2,9} - 8.1111)^2\ + \\
& \qquad
(x_{3,1} - 8.25)^2\ + (x_{3,2} - 8.25)^2\ + (x_{3,3} - 8.25)^2\ + (x_{3,4} - 8.25)^2\ + (x_{3,5} - 8.25)^2\ + (x_{3,6} - 8.25)^2\ + (x_{3,7} - 8.25)^2\ + (x_{3,8} - 8.25)^2\ + \\
& \qquad
(x_{4,1} - 10.3636)^2\ + (x_{4,2} - 10.3636)^2\ + (x_{4,3} - 10.3636)^2\ + (x_{4,4} - 10.3636)^2\ + (x_{4,5} - 10.3636)^2\ + (x_{4,6} - 10.3636)^2\ + (x_{4,7} - 10.3636)^2\ + (x_{4,8} - 10.3636)^2\ + (x_{4,9} - 10.3636)^2\ + (x_{4,10} - 10.3636)^2\ + (x_{4,11} - 10.3636)^2\ + \\
& \qquad
(x_{5,1} - 7.4286)^2\ +\ (x_{5,2} - 7.4286)^2\ +\ (x_{5,3} - 7.4286)^2\ +\ (x_{5,4} - 7.4286)^2\ +\ (x_{5,5} - 7.4286)^2\ +\ (x_{5,6} - 7.4286)^2\ +\ (x_{5,7} - 7.4286)^2 \\[1em]
&= (1 - 11)^2\ + (17 - 11)^2\ + (4 - 11)^2\ + (18 - 11)^2\ + (17 - 11)^2\ + (9 - 11)^2\ + \\
& \qquad
(0 - 8.1111)^2\ + (9 - 8.1111)^2\ + (7 - 8.1111)^2\ + (18 - 8.1111)^2\ + (11 - 8.1111)^2\ + (3 - 8.1111)^2\ + (7 - 8.1111)^2\ + (12 - 8.1111)^2\ + (6 - 8.1111)^2\ + \\
& \qquad
(3 - 8.25)^2\ + (10 - 8.25)^2\ + (15 - 8.25)^2\ + (4 - 8.25)^2\ + (1 - 8.25)^2\ + (11 - 8.25)^2\ + (14 - 8.25)^2\ + (8 - 8.25)^2\ + \\
& \qquad
(10 - 10.3636)^2\ + (10 - 10.3636)^2\ + (15 - 10.3636)^2\ + (11 - 10.3636)^2\ + (20 - 10.3636)^2\ + (6 - 10.3636)^2\ + (13 - 10.3636)^2\ + (10 - 10.3636)^2\ + (8 - 10.3636)^2\ + (5 - 10.3636)^2\ + (6 - 10.3636)^2\ + \\
& \qquad
(5 - 7.4286)^2\ +\ (2 - 7.4286)^2\ +\ (4 - 7.4286)^2\ +\ (7 - 7.4286)^2\ +\ (9 - 7.4286)^2\ +\ (10 - 7.4286)^2\ +\ (15 - 7.4286)^2 \\[1em]
&= (-10)^2\ + (6)^2\ + (-7)^2\ + (7)^2\ + (6)^2\ + (-2)^2\ + \\
& \qquad
(-8.1111)^2\ + (0.8889)^2\ + (-1.1111)^2\ + (9.8889)^2\ + (2.8889)^2\ + (-5.1111)^2\ + (-1.1111)^2\ + (3.8889)^2\ + (-2.1111)^2\ + \\
& \qquad
(-5.25)^2\ + (1.75)^2\ + (6.75)^2\ + (-4.25)^2\ + (-7.25)^2\ + (2.75)^2\ + (5.75)^2\ + (-0.25)^2\ + \\
& \qquad
(-0.3636)^2\ + (-0.3636)^2\ + (4.6364)^2\ + (0.6364)^2\ + (9.6364)^2\ + (-4.3636)^2\ + (2.6364)^2\ + (-0.3636)^2\ + (-2.3636)^2\ + (-5.3636)^2\ + (-4.3636)^2\ + \\
& \qquad
(-2.4286)^2\ +\ (-5.4286)^2\ +\ (-3.4286)^2\ +\ (-0.4286)^2\ +\ (1.5714)^2\ +\ (2.5714)^2\ +\ (7.5714)^2 \\[1em]
&= (100)\ + (36)\ + (49)\ + (49)\ + (36)\ + (4)\ + \\
& \qquad
(65.7901)\ + (0.7901)\ + (1.2346)\ + (97.7901)\ + (8.3457)\ + (26.1235)\ + (1.2346)\ + (15.1235)\ + (4.4568)\ + \\
& \qquad
(27.5625)\ + (3.0625)\ + (45.5625)\ + (18.0625)\ + (52.5625)\ + (7.5625)\ + (33.0625)\ + (0.0625)\ + \\
& \qquad
(0.1322)\ + (0.1322)\ + (21.4959)\ + (0.405)\ + (92.8595)\ + (19.0413)\ + (6.9504)\ + (0.1322)\ + (5.5868)\ + (28.7686)\ + (19.0413)\ + \\
& \qquad
(5.898)\ +\ (29.4694)\ +\ (11.7551)\ +\ (0.1837)\ +\ (2.4694)\ +\ (6.6122)\ +\ (57.3265) \\[1em]
&= 274\ + 220.8889\ + 187.5\ + 194.5455\ + 56.3878 \\[1em]
&= 990.6486 \\[1em]
\end{align}
$$
From these calculations, the within sum of squares is SSW = 990.6486.
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(1, 17, 4, 18, 17, 9)
treatment2 = c(0, 9, 7, 18, 11, 3, 7, 12, 6)
treatment3 = c(3, 10, 15, 4, 1, 11, 14, 8)
treatment4 = c(10, 10, 15, 11, 20, 6, 13, 10, 8, 5, 6)
treatment5 = c(5, 2, 4, 7, 9, 10, 15)
## Change to Long Format
mmt = c( treatment1, treatment2, treatment3, treatment4, treatment5 )
grp = c( rep("trt1",6), rep("trt2",9), rep("trt3",8), rep("trt4",11), 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(1, 17, 4, 18, 17, 9, 0, 9, 7, 18, 11, 3, 7, 12, 6, 3, 10, 15, 4, 1, 11, 14, 8, 10, 10, 15, 11, 20, 6, 13, 10, 8, 5, 6, 5, 2, 4, 7, 9, 10, 15)
grp = c('trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt4', '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.