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}^{4} \sum_{j=1}^{n_i}\ (x_{i,j} - \bar{x}_i)^2 \\[1em]
&= \sum_{j=1}^{8}\ (x_{1,j} - 12.75)^2 + \sum_{j=1}^{12}\ (x_{2,j} - 13.6667)^2 + \sum_{j=1}^{9}\ (x_{3,j} - 15.4444)^2 + \sum_{j=1}^{9}\ (x_{4,j} - 12.7778)^2 \\[1em]
&= (x_{1,1} - 12.75)^2\ + (x_{1,2} - 12.75)^2\ + (x_{1,3} - 12.75)^2\ + (x_{1,4} - 12.75)^2\ + (x_{1,5} - 12.75)^2\ + (x_{1,6} - 12.75)^2\ + (x_{1,7} - 12.75)^2\ + (x_{1,8} - 12.75)^2\ + \\
& \qquad
(x_{2,1} - 13.6667)^2\ + (x_{2,2} - 13.6667)^2\ + (x_{2,3} - 13.6667)^2\ + (x_{2,4} - 13.6667)^2\ + (x_{2,5} - 13.6667)^2\ + (x_{2,6} - 13.6667)^2\ + (x_{2,7} - 13.6667)^2\ + (x_{2,8} - 13.6667)^2\ + (x_{2,9} - 13.6667)^2\ + (x_{2,10} - 13.6667)^2\ + (x_{2,11} - 13.6667)^2\ + (x_{2,12} - 13.6667)^2\ + \\
& \qquad
(x_{3,1} - 15.4444)^2\ + (x_{3,2} - 15.4444)^2\ + (x_{3,3} - 15.4444)^2\ + (x_{3,4} - 15.4444)^2\ + (x_{3,5} - 15.4444)^2\ + (x_{3,6} - 15.4444)^2\ + (x_{3,7} - 15.4444)^2\ + (x_{3,8} - 15.4444)^2\ + (x_{3,9} - 15.4444)^2\ + \\
& \qquad
(x_{4,1} - 12.7778)^2\ +\ (x_{4,2} - 12.7778)^2\ +\ (x_{4,3} - 12.7778)^2\ +\ (x_{4,4} - 12.7778)^2\ +\ (x_{4,5} - 12.7778)^2\ +\ (x_{4,6} - 12.7778)^2\ +\ (x_{4,7} - 12.7778)^2\ +\ (x_{4,8} - 12.7778)^2\ +\ (x_{4,9} - 12.7778)^2 \\[1em]
&= (15 - 12.75)^2\ + (14 - 12.75)^2\ + (11 - 12.75)^2\ + (13 - 12.75)^2\ + (11 - 12.75)^2\ + (12 - 12.75)^2\ + (16 - 12.75)^2\ + (10 - 12.75)^2\ + \\
& \qquad
(13 - 13.6667)^2\ + (12 - 13.6667)^2\ + (16 - 13.6667)^2\ + (13 - 13.6667)^2\ + (11 - 13.6667)^2\ + (15 - 13.6667)^2\ + (17 - 13.6667)^2\ + (12 - 13.6667)^2\ + (13 - 13.6667)^2\ + (11 - 13.6667)^2\ + (17 - 13.6667)^2\ + (14 - 13.6667)^2\ + \\
& \qquad
(19 - 15.4444)^2\ + (18 - 15.4444)^2\ + (14 - 15.4444)^2\ + (16 - 15.4444)^2\ + (15 - 15.4444)^2\ + (14 - 15.4444)^2\ + (10 - 15.4444)^2\ + (16 - 15.4444)^2\ + (17 - 15.4444)^2\ + \\
& \qquad
(11 - 12.7778)^2\ +\ (16 - 12.7778)^2\ +\ (13 - 12.7778)^2\ +\ (16 - 12.7778)^2\ +\ (14 - 12.7778)^2\ +\ (12 - 12.7778)^2\ +\ (13 - 12.7778)^2\ +\ (10 - 12.7778)^2\ +\ (10 - 12.7778)^2 \\[1em]
&= (2.25)^2\ + (1.25)^2\ + (-1.75)^2\ + (0.25)^2\ + (-1.75)^2\ + (-0.75)^2\ + (3.25)^2\ + (-2.75)^2\ + \\
& \qquad
(-0.6667)^2\ + (-1.6667)^2\ + (2.3333)^2\ + (-0.6667)^2\ + (-2.6667)^2\ + (1.3333)^2\ + (3.3333)^2\ + (-1.6667)^2\ + (-0.6667)^2\ + (-2.6667)^2\ + (3.3333)^2\ + (0.3333)^2\ + \\
& \qquad
(3.5556)^2\ + (2.5556)^2\ + (-1.4444)^2\ + (0.5556)^2\ + (-0.4444)^2\ + (-1.4444)^2\ + (-5.4444)^2\ + (0.5556)^2\ + (1.5556)^2\ + \\
& \qquad
(-1.7778)^2\ +\ (3.2222)^2\ +\ (0.2222)^2\ +\ (3.2222)^2\ +\ (1.2222)^2\ +\ (-0.7778)^2\ +\ (0.2222)^2\ +\ (-2.7778)^2\ +\ (-2.7778)^2 \\[1em]
&= (5.0625)\ + (1.5625)\ + (3.0625)\ + (0.0625)\ + (3.0625)\ + (0.5625)\ + (10.5625)\ + (7.5625)\ + \\
& \qquad
(0.4444)\ + (2.7778)\ + (5.4444)\ + (0.4444)\ + (7.1111)\ + (1.7778)\ + (11.1111)\ + (2.7778)\ + (0.4444)\ + (7.1111)\ + (11.1111)\ + (0.1111)\ + \\
& \qquad
(12.642)\ + (6.5309)\ + (2.0864)\ + (0.3086)\ + (0.1975)\ + (2.0864)\ + (29.642)\ + (0.3086)\ + (2.4198)\ + \\
& \qquad
(3.1605)\ +\ (10.3827)\ +\ (0.0494)\ +\ (10.3827)\ +\ (1.4938)\ +\ (0.6049)\ +\ (0.0494)\ +\ (7.716)\ +\ (7.716) \\[1em]
&= 31.5\ + 50.6667\ + 56.2222\ + 33.8395 \\[1em]
&= 179.9444 \\[1em]
\end{align}
$$
From these calculations, the within sum of squares is SSW = 179.9444.
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(15, 14, 11, 13, 11, 12, 16, 10)
treatment2 = c(13, 12, 16, 13, 11, 15, 17, 12, 13, 11, 17, 14)
treatment3 = c(19, 18, 14, 16, 15, 14, 10, 16, 17)
treatment4 = c(11, 16, 13, 16, 14, 12, 13, 10, 10)
## Change to Long Format
mmt = c( treatment1, treatment2, treatment3, treatment4 )
grp = c( rep("trt1",8), rep("trt2",12), rep("trt3",9), rep("trt4",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(15, 14, 11, 13, 11, 12, 16, 10, 13, 12, 16, 13, 11, 15, 17, 12, 13, 11, 17, 14, 19, 18, 14, 16, 15, 14, 10, 16, 17, 11, 16, 13, 16, 14, 12, 13, 10, 10)
grp = c('trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt2', 'trt2', 'trt2', 'trt2', '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')
## 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.