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}^{3} \sum_{j=1}^{n_i}\ (x_{i,j} - \bar{x}_i)^2 \\[1em]
&= \sum_{j=1}^{9}\ (x_{1,j} - 9.3333)^2 + \sum_{j=1}^{6}\ (x_{2,j} - 9.3333)^2 + \sum_{j=1}^{7}\ (x_{3,j} - 12.2857)^2 \\[1em]
&= (x_{1,1} - 9.3333)^2\ + (x_{1,2} - 9.3333)^2\ + (x_{1,3} - 9.3333)^2\ + (x_{1,4} - 9.3333)^2\ + (x_{1,5} - 9.3333)^2\ + (x_{1,6} - 9.3333)^2\ + (x_{1,7} - 9.3333)^2\ + (x_{1,8} - 9.3333)^2\ + (x_{1,9} - 9.3333)^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\ + \\
& \qquad
(x_{3,1} - 12.2857)^2\ +\ (x_{3,2} - 12.2857)^2\ +\ (x_{3,3} - 12.2857)^2\ +\ (x_{3,4} - 12.2857)^2\ +\ (x_{3,5} - 12.2857)^2\ +\ (x_{3,6} - 12.2857)^2\ +\ (x_{3,7} - 12.2857)^2 \\[1em]
&= (4 - 9.3333)^2\ + (7 - 9.3333)^2\ + (13 - 9.3333)^2\ + (9 - 9.3333)^2\ + (9 - 9.3333)^2\ + (10 - 9.3333)^2\ + (11 - 9.3333)^2\ + (7 - 9.3333)^2\ + (14 - 9.3333)^2\ + \\
& \qquad
(10 - 9.3333)^2\ + (6 - 9.3333)^2\ + (10 - 9.3333)^2\ + (6 - 9.3333)^2\ + (7 - 9.3333)^2\ + (17 - 9.3333)^2\ + \\
& \qquad
(14 - 12.2857)^2\ +\ (7 - 12.2857)^2\ +\ (12 - 12.2857)^2\ +\ (11 - 12.2857)^2\ +\ (18 - 12.2857)^2\ +\ (14 - 12.2857)^2\ +\ (10 - 12.2857)^2 \\[1em]
&= (-5.3333)^2\ + (-2.3333)^2\ + (3.6667)^2\ + (-0.3333)^2\ + (-0.3333)^2\ + (0.6667)^2\ + (1.6667)^2\ + (-2.3333)^2\ + (4.6667)^2\ + \\
& \qquad
(0.6667)^2\ + (-3.3333)^2\ + (0.6667)^2\ + (-3.3333)^2\ + (-2.3333)^2\ + (7.6667)^2\ + \\
& \qquad
(1.7143)^2\ +\ (-5.2857)^2\ +\ (-0.2857)^2\ +\ (-1.2857)^2\ +\ (5.7143)^2\ +\ (1.7143)^2\ +\ (-2.2857)^2 \\[1em]
&= (28.4444)\ + (5.4444)\ + (13.4444)\ + (0.1111)\ + (0.1111)\ + (0.4444)\ + (2.7778)\ + (5.4444)\ + (21.7778)\ + \\
& \qquad
(0.4444)\ + (11.1111)\ + (0.4444)\ + (11.1111)\ + (5.4444)\ + (58.7778)\ + \\
& \qquad
(2.9388)\ +\ (27.9388)\ +\ (0.0816)\ +\ (1.6531)\ +\ (32.6531)\ +\ (2.9388)\ +\ (5.2245) \\[1em]
&= 78\ + 87.3333\ + 68.2041 \\[1em]
&= 238.7619 \\[1em]
\end{align}
$$
From these calculations, the within sum of squares is SSW = 238.7619.
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(4, 7, 13, 9, 9, 10, 11, 7, 14)
treatment2 = c(10, 6, 10, 6, 7, 17)
treatment3 = c(14, 7, 12, 11, 18, 14, 10)
## Change to Long Format
mmt = c( treatment1, treatment2, treatment3 )
grp = c( rep("trt1",9), rep("trt2",6), rep("trt3",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(4, 7, 13, 9, 9, 10, 11, 7, 14, 10, 6, 10, 6, 7, 17, 14, 7, 12, 11, 18, 14, 10)
grp = c('trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3', 'trt3')
## 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.