Hide Solution
$$ \begin{align}
\text{SSW} &= \sum_{i=1}^g \sum_{j=1}^{n_i}\ (x_{i,j} - \bar{x})^2 \\[3em]
&= \sum_{i=1}^{3} \sum_{j=1}^{n_i}\ (x_{i,j} - \bar{x})^2 \\[1em]
&= \sum_{j=1}^{3}\ (x_{1,j} - 4.35)^2 + \sum_{j=1}^{8}\ (x_{2,j} - 4.35)^2 + \sum_{j=1}^{9}\ (x_{3,j} - 4.35)^2 \\[1em]
&= (x_{1,1} - 4.35)^2\ + (x_{1,2} - 4.35)^2\ + (x_{1,3} - 4.35)^2\ + \\
& \qquad
(x_{2,1} - 4.35)^2\ + (x_{2,2} - 4.35)^2\ + (x_{2,3} - 4.35)^2\ + (x_{2,4} - 4.35)^2\ + (x_{2,5} - 4.35)^2\ + (x_{2,6} - 4.35)^2\ + (x_{2,7} - 4.35)^2\ + (x_{2,8} - 4.35)^2\ + \\
& \qquad
(x_{3,1} - 4.35)^2\ +\ (x_{3,2} - 4.35)^2\ +\ (x_{3,3} - 4.35)^2\ +\ (x_{3,4} - 4.35)^2\ +\ (x_{3,5} - 4.35)^2\ +\ (x_{3,6} - 4.35)^2\ +\ (x_{3,7} - 4.35)^2\ +\ (x_{3,8} - 4.35)^2\ +\ (x_{3,9} - 4.35)^2 \\[1em]
&= (0 - 4.35)^2\ + (-5 - 4.35)^2\ + (1 - 4.35)^2\ + \\
& \qquad
(1 - 4.35)^2\ + (2 - 4.35)^2\ + (1 - 4.35)^2\ + (12 - 4.35)^2\ + (4 - 4.35)^2\ + (13 - 4.35)^2\ + (3 - 4.35)^2\ + (7 - 4.35)^2\ + \\
& \qquad
(14 - 4.35)^2\ +\ (5 - 4.35)^2\ +\ (3 - 4.35)^2\ +\ (0 - 4.35)^2\ +\ (-1 - 4.35)^2\ +\ (-2 - 4.35)^2\ +\ (11 - 4.35)^2\ +\ (10 - 4.35)^2\ +\ (8 - 4.35)^2 \\[1em]
&= (-4.35)^2\ + (-9.35)^2\ + (-3.35)^2\ + \\
& \qquad
(-3.35)^2\ + (-2.35)^2\ + (-3.35)^2\ + (7.65)^2\ + (-0.35)^2\ + (8.65)^2\ + (-1.35)^2\ + (2.65)^2\ + \\
& \qquad
(9.65)^2\ +\ (0.65)^2\ +\ (-1.35)^2\ +\ (-4.35)^2\ +\ (-5.35)^2\ +\ (-6.35)^2\ +\ (6.65)^2\ +\ (5.65)^2\ +\ (3.65)^2 \\[1em]
&= (18.9225)\ + (87.4225)\ + (11.2225)\ + \\
& \qquad
(11.2225)\ + (5.5225)\ + (11.2225)\ + (58.5225)\ + (0.1225)\ + (74.8225)\ + (1.8225)\ + (7.0225)\ + \\
& \qquad
(93.1225)\ +\ (0.4225)\ +\ (1.8225)\ +\ (18.9225)\ +\ (28.6225)\ +\ (40.3225)\ +\ (44.2225)\ +\ (31.9225)\ +\ (13.3225) \\[1em]
&= 117.5675\ + 170.28\ + 259.38 \\[1em]
&= 560.55 \\[1em]
\end{align}
$$
From these calculations, the total sum of squares is TSS = 560.55. Note that if we had already calculated the between sum of squares (SSB) and the within sum of squares (SSW), then we could have used the relation TSS = SSB + SSW.
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 total sum of squares is the sum of the within and the between sums of squares. To have R do that calculation for you, run:
modSummary = summary(mod)
modSummary[[1]][1,2] + modSummary[[1]][2,2]
Here, the number outputted is the sum of the between and the within sums of squares. 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 sums the between ([1,2]
) and within ([2,2]
) sums of squares to get the total sums of squares.
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, the value of the total sum of squares is the sum o fthe within and the between sums of squares. To have R do that calculation for you, run:
modSummary = summary(mod)
modSummary[[1]][1,2] + modSummary[[1]][2,2]
Here, the number outputted is the sum of the between and the within sums of squares. 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 sums the between ([1,2]
) and within ([2,2]
) sums of squares to get the total sums of squares.