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}^{3}\ (x_{1,j} - -1.3333)^2 + \sum_{j=1}^{8}\ (x_{2,j} - 5.375)^2 + \sum_{j=1}^{9}\ (x_{3,j} - 5.3333)^2 \\[1em]
&= (x_{1,1} - -1.3333)^2\ + (x_{1,2} - -1.3333)^2\ + (x_{1,3} - -1.3333)^2\ + \\
& \qquad
(x_{2,1} - 5.375)^2\ + (x_{2,2} - 5.375)^2\ + (x_{2,3} - 5.375)^2\ + (x_{2,4} - 5.375)^2\ + (x_{2,5} - 5.375)^2\ + (x_{2,6} - 5.375)^2\ + (x_{2,7} - 5.375)^2\ + (x_{2,8} - 5.375)^2\ + \\
& \qquad
(x_{3,1} - 5.3333)^2\ +\ (x_{3,2} - 5.3333)^2\ +\ (x_{3,3} - 5.3333)^2\ +\ (x_{3,4} - 5.3333)^2\ +\ (x_{3,5} - 5.3333)^2\ +\ (x_{3,6} - 5.3333)^2\ +\ (x_{3,7} - 5.3333)^2\ +\ (x_{3,8} - 5.3333)^2\ +\ (x_{3,9} - 5.3333)^2 \\[1em]
&= (0 - -1.3333)^2\ + (-5 - -1.3333)^2\ + (1 - -1.3333)^2\ + \\
& \qquad
(1 - 5.375)^2\ + (2 - 5.375)^2\ + (1 - 5.375)^2\ + (12 - 5.375)^2\ + (4 - 5.375)^2\ + (13 - 5.375)^2\ + (3 - 5.375)^2\ + (7 - 5.375)^2\ + \\
& \qquad
(14 - 5.3333)^2\ +\ (5 - 5.3333)^2\ +\ (3 - 5.3333)^2\ +\ (0 - 5.3333)^2\ +\ (-1 - 5.3333)^2\ +\ (-2 - 5.3333)^2\ +\ (11 - 5.3333)^2\ +\ (10 - 5.3333)^2\ +\ (8 - 5.3333)^2 \\[1em]
&= (1.3333)^2\ + (-3.6667)^2\ + (2.3333)^2\ + \\
& \qquad
(-4.375)^2\ + (-3.375)^2\ + (-4.375)^2\ + (6.625)^2\ + (-1.375)^2\ + (7.625)^2\ + (-2.375)^2\ + (1.625)^2\ + \\
& \qquad
(8.6667)^2\ +\ (-0.3333)^2\ +\ (-2.3333)^2\ +\ (-5.3333)^2\ +\ (-6.3333)^2\ +\ (-7.3333)^2\ +\ (5.6667)^2\ +\ (4.6667)^2\ +\ (2.6667)^2 \\[1em]
&= (1.7778)\ + (13.4444)\ + (5.4444)\ + \\
& \qquad
(19.1406)\ + (11.3906)\ + (19.1406)\ + (43.8906)\ + (1.8906)\ + (58.1406)\ + (5.6406)\ + (2.6406)\ + \\
& \qquad
(75.1111)\ +\ (0.1111)\ +\ (5.4444)\ +\ (28.4444)\ +\ (40.1111)\ +\ (53.7778)\ +\ (32.1111)\ +\ (21.7778)\ +\ (7.1111) \\[1em]
&= 20.6667\ + 161.875\ + 256.8889 \\[1em]
&= 446.5417 \\[1em]
\end{align}
$$
From these calculations, the within sum of squares is SSW = 446.5417.
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(0, -5, 1)
treatment2 = c(1, 2, 1, 12, 4, 13, 3, 7)
treatment3 = c(14, 5, 3, 0, -1, -2, 11, 10, 8)
## Change to Long Format
mmt = c( treatment1, treatment2, treatment3 )
grp = c( rep("trt1",3), rep("trt2",8), rep("trt3",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(0, -5, 1, 1, 2, 1, 12, 4, 13, 3, 7, 14, 5, 3, 0, -1, -2, 11, 10, 8)
grp = c('trt1', 'trt1', 'trt1', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt3', 'trt3', '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.