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}^{9}\ (x_{1,j} - 10.2727)^2 + \sum_{j=1}^{6}\ (x_{2,j} - 10.2727)^2 + \sum_{j=1}^{7}\ (x_{3,j} - 10.2727)^2 \\[1em]
&= (x_{1,1} - 10.2727)^2\ + (x_{1,2} - 10.2727)^2\ + (x_{1,3} - 10.2727)^2\ + (x_{1,4} - 10.2727)^2\ + (x_{1,5} - 10.2727)^2\ + (x_{1,6} - 10.2727)^2\ + (x_{1,7} - 10.2727)^2\ + (x_{1,8} - 10.2727)^2\ + (x_{1,9} - 10.2727)^2\ + \\
& \qquad
(x_{2,1} - 10.2727)^2\ + (x_{2,2} - 10.2727)^2\ + (x_{2,3} - 10.2727)^2\ + (x_{2,4} - 10.2727)^2\ + (x_{2,5} - 10.2727)^2\ + (x_{2,6} - 10.2727)^2\ + \\
& \qquad
(x_{3,1} - 10.2727)^2\ +\ (x_{3,2} - 10.2727)^2\ +\ (x_{3,3} - 10.2727)^2\ +\ (x_{3,4} - 10.2727)^2\ +\ (x_{3,5} - 10.2727)^2\ +\ (x_{3,6} - 10.2727)^2\ +\ (x_{3,7} - 10.2727)^2 \\[1em]
&= (4 - 10.2727)^2\ + (7 - 10.2727)^2\ + (13 - 10.2727)^2\ + (9 - 10.2727)^2\ + (9 - 10.2727)^2\ + (10 - 10.2727)^2\ + (11 - 10.2727)^2\ + (7 - 10.2727)^2\ + (14 - 10.2727)^2\ + \\
& \qquad
(10 - 10.2727)^2\ + (6 - 10.2727)^2\ + (10 - 10.2727)^2\ + (6 - 10.2727)^2\ + (7 - 10.2727)^2\ + (17 - 10.2727)^2\ + \\
& \qquad
(14 - 10.2727)^2\ +\ (7 - 10.2727)^2\ +\ (12 - 10.2727)^2\ +\ (11 - 10.2727)^2\ +\ (18 - 10.2727)^2\ +\ (14 - 10.2727)^2\ +\ (10 - 10.2727)^2 \\[1em]
&= (-6.2727)^2\ + (-3.2727)^2\ + (2.7273)^2\ + (-1.2727)^2\ + (-1.2727)^2\ + (-0.2727)^2\ + (0.7273)^2\ + (-3.2727)^2\ + (3.7273)^2\ + \\
& \qquad
(-0.2727)^2\ + (-4.2727)^2\ + (-0.2727)^2\ + (-4.2727)^2\ + (-3.2727)^2\ + (6.7273)^2\ + \\
& \qquad
(3.7273)^2\ +\ (-3.2727)^2\ +\ (1.7273)^2\ +\ (0.7273)^2\ +\ (7.7273)^2\ +\ (3.7273)^2\ +\ (-0.2727)^2 \\[1em]
&= (39.3471)\ + (10.7107)\ + (7.438)\ + (1.6198)\ + (1.6198)\ + (0.0744)\ + (0.5289)\ + (10.7107)\ + (13.8926)\ + \\
& \qquad
(0.0744)\ + (18.2562)\ + (0.0744)\ + (18.2562)\ + (10.7107)\ + (45.2562)\ + \\
& \qquad
(13.8926)\ +\ (10.7107)\ +\ (2.9835)\ +\ (0.5289)\ +\ (59.7107)\ +\ (13.8926)\ +\ (0.0744) \\[1em]
&= 85.9421\ + 92.6281\ + 101.719 \\[1em]
&= 280.3636 \\[1em]
\end{align}
$$
From these calculations, the total sum of squares is TSS = 280.3636. 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(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 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(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, 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.