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}^{7}\ (x_{1,j} - 7.5714)^2 + \sum_{j=1}^{8}\ (x_{2,j} - 7.5)^2 + \sum_{j=1}^{5}\ (x_{3,j} - 9.2)^2 + \sum_{j=1}^{9}\ (x_{4,j} - 7.7778)^2 \\[1em]
&= (x_{1,1} - 7.5714)^2\ + (x_{1,2} - 7.5714)^2\ + (x_{1,3} - 7.5714)^2\ + (x_{1,4} - 7.5714)^2\ + (x_{1,5} - 7.5714)^2\ + (x_{1,6} - 7.5714)^2\ + (x_{1,7} - 7.5714)^2\ + \\
& \qquad
(x_{2,1} - 7.5)^2\ + (x_{2,2} - 7.5)^2\ + (x_{2,3} - 7.5)^2\ + (x_{2,4} - 7.5)^2\ + (x_{2,5} - 7.5)^2\ + (x_{2,6} - 7.5)^2\ + (x_{2,7} - 7.5)^2\ + (x_{2,8} - 7.5)^2\ + \\
& \qquad
(x_{3,1} - 9.2)^2\ + (x_{3,2} - 9.2)^2\ + (x_{3,3} - 9.2)^2\ + (x_{3,4} - 9.2)^2\ + (x_{3,5} - 9.2)^2\ + \\
& \qquad
(x_{4,1} - 7.7778)^2\ +\ (x_{4,2} - 7.7778)^2\ +\ (x_{4,3} - 7.7778)^2\ +\ (x_{4,4} - 7.7778)^2\ +\ (x_{4,5} - 7.7778)^2\ +\ (x_{4,6} - 7.7778)^2\ +\ (x_{4,7} - 7.7778)^2\ +\ (x_{4,8} - 7.7778)^2\ +\ (x_{4,9} - 7.7778)^2 \\[1em]
&= (6 - 7.5714)^2\ + (7 - 7.5714)^2\ + (9 - 7.5714)^2\ + (7 - 7.5714)^2\ + (9 - 7.5714)^2\ + (5 - 7.5714)^2\ + (10 - 7.5714)^2\ + \\
& \qquad
(8 - 7.5)^2\ + (8 - 7.5)^2\ + (12 - 7.5)^2\ + (6 - 7.5)^2\ + (6 - 7.5)^2\ + (7 - 7.5)^2\ + (4 - 7.5)^2\ + (9 - 7.5)^2\ + \\
& \qquad
(10 - 9.2)^2\ + (9 - 9.2)^2\ + (10 - 9.2)^2\ + (5 - 9.2)^2\ + (12 - 9.2)^2\ + \\
& \qquad
(7 - 7.7778)^2\ +\ (8 - 7.7778)^2\ +\ (9 - 7.7778)^2\ +\ (11 - 7.7778)^2\ +\ (5 - 7.7778)^2\ +\ (4 - 7.7778)^2\ +\ (13 - 7.7778)^2\ +\ (8 - 7.7778)^2\ +\ (5 - 7.7778)^2 \\[1em]
&= (-1.5714)^2\ + (-0.5714)^2\ + (1.4286)^2\ + (-0.5714)^2\ + (1.4286)^2\ + (-2.5714)^2\ + (2.4286)^2\ + \\
& \qquad
(0.5)^2\ + (0.5)^2\ + (4.5)^2\ + (-1.5)^2\ + (-1.5)^2\ + (-0.5)^2\ + (-3.5)^2\ + (1.5)^2\ + \\
& \qquad
(0.8)^2\ + (-0.2)^2\ + (0.8)^2\ + (-4.2)^2\ + (2.8)^2\ + \\
& \qquad
(-0.7778)^2\ +\ (0.2222)^2\ +\ (1.2222)^2\ +\ (3.2222)^2\ +\ (-2.7778)^2\ +\ (-3.7778)^2\ +\ (5.2222)^2\ +\ (0.2222)^2\ +\ (-2.7778)^2 \\[1em]
&= (2.4694)\ + (0.3265)\ + (2.0408)\ + (0.3265)\ + (2.0408)\ + (6.6122)\ + (5.898)\ + \\
& \qquad
(0.25)\ + (0.25)\ + (20.25)\ + (2.25)\ + (2.25)\ + (0.25)\ + (12.25)\ + (2.25)\ + \\
& \qquad
(0.64)\ + (0.04)\ + (0.64)\ + (17.64)\ + (7.84)\ + \\
& \qquad
(0.6049)\ +\ (0.0494)\ +\ (1.4938)\ +\ (10.3827)\ +\ (7.716)\ +\ (14.2716)\ +\ (27.2716)\ +\ (0.0494)\ +\ (7.716) \\[1em]
&= 19.7143\ + 40\ + 26.8\ + 61.8395 \\[1em]
&= 156.0698 \\[1em]
\end{align}
$$
From these calculations, the within sum of squares is SSW = 156.0698.
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(6, 7, 9, 7, 9, 5, 10)
treatment2 = c(8, 8, 12, 6, 6, 7, 4, 9)
treatment3 = c(10, 9, 10, 5, 12)
treatment4 = c(7, 8, 9, 11, 5, 4, 13, 8, 5)
## Change to Long Format
mmt = c( treatment1, treatment2, treatment3, treatment4 )
grp = c( rep("trt1",7), rep("trt2",8), rep("trt3",5), 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(6, 7, 9, 7, 9, 5, 10, 8, 8, 12, 6, 6, 7, 4, 9, 10, 9, 10, 5, 12, 7, 8, 9, 11, 5, 4, 13, 8, 5)
grp = c('trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt1', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', 'trt2', '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.