Two-Way ANOVA
library(data.table)

1 Introduction

So this is an overly pedantic explanation of two-way ANOVA that describes how to do the math both by hand and using R. This will use the CO2 dataset which shows the amount of co2 absorbed by the same species of grass that originated from either Mississippi or Quebec and were measured after being either chilled overnight or unchilled.

co2 <- data.table(CO2)
head(co2)
##    Plant   Type  Treatment conc uptake
## 1:   Qn1 Quebec nonchilled   95   16.0
## 2:   Qn1 Quebec nonchilled  175   30.4
## 3:   Qn1 Quebec nonchilled  250   34.8
## 4:   Qn1 Quebec nonchilled  350   37.2
## 5:   Qn1 Quebec nonchilled  500   35.3
## 6:   Qn1 Quebec nonchilled  675   39.2

The dataset measures co2 uptake at different levels of ambient co2 concentrations so I’ll just average those for each plant.


co2[,.(meanUptake = mean(uptake)), by = .(Plant,Type,Treatment)]
##     Plant        Type  Treatment meanUptake
##  1:   Qn1      Quebec nonchilled   33.22857
##  2:   Qn2      Quebec nonchilled   35.15714
##  3:   Qn3      Quebec nonchilled   37.61429
##  4:   Qc1      Quebec    chilled   29.97143
##  5:   Qc2      Quebec    chilled   32.70000
##  6:   Qc3      Quebec    chilled   32.58571
##  7:   Mn1 Mississippi nonchilled   26.40000
##  8:   Mn2 Mississippi nonchilled   27.34286
##  9:   Mn3 Mississippi nonchilled   24.11429
## 10:   Mc1 Mississippi    chilled   18.00000
## 11:   Mc2 Mississippi    chilled   12.14286
## 12:   Mc3 Mississippi    chilled   17.30000


So there are three plants that received each treatment and come from each location for a total of 12 plants.


The table looks like this:

Mississippi Quebec
Chilled 18, 12.143, 17.3 29.971, 32.7, 32.586
Nonchilled 26.4, 27.343, 24.114 33.229, 35.157, 37.614


2 Hypthoses


Now we state our hypotheses. There are three sets of hypotheses:

\(H_0: \text{Temperature (treatment) doesn't affect co2 uptake}\)

\(H_1: \text{Temperature (treatment) does affect co2 uptake}\)


\(H_0: \text{Plant origin (type) doesn't affect co2 uptake}\)

\(H_1: \text{Plant origin (type) does affect co2 uptake}\)


\(H_0: \text{Plant origin (type) and temperature (treatment) don't affect co2 uptake}\)

\(H_1: \text{Plant origin (type) and temperature (treatment) affect co2 uptake}\)


3 ANOVA Table


We have to fill in this ANOVA table to find out if the F-test value is greater or less than the C.V.


d.f. CV SS MS F
Treatment (A) a-1 \(SS_A\) \(SS_A/(d.f.A)\) \(MS_A/MS_{Within}\)
Type (B) b-1 \(SS_B\) \(SS_{B}/(d.f.B)\) \(MS_B/MS_{Within}\)
Interaction(AxB) (a-1)(b-1) \(SS_{AxB}\) \(SS_{(AxB)}/(d.f.AxB)\) \(MS_{(AxB)}/MS_{Within}\)
Within (error) ab(n-1) \(SS_{Within}\) \(SS_W/d.f.W\)
Total abr-1 \(SS_{Total}\)


4 Degrees of Freedom

To find the critical values we use the F-Distribution table which requires that we have the following:

The Degrees of Freedom for each factor:


\[\begin{align} \text{Factor Treatment: } d.f.N & = (a - 1) = (2-1) = 1 \ & \\ \text{Factor Type: } d.f.N & = (b - 1) = (2-1) = 1 \ & \\ \text{Interaction (A x B) : } d.f.N & = (a - 1)(b - 1) = (2-1)(2-1) = 1 \\ & \\ \text{and the within } & \text{group Degrees of Freedom: }\\ & \\ \text{Within (error): } d.f.D & = ab(n-1) = 2 \cdot 2(3-1) = 8 \end{align}\]

Where:

  • \(a\) is the number of treatment categories (chilled and nonchilled).
  • \(b\) is the number of type categories (Quebec and Mississippi).
  • \(n\) is the number of data values in each group.

Also:

  • d.f.N stands for Degrees of Freedom Numerator
  • d.f.D stands for Degrees of Freedom Denominator

5 Critical Values

Based on those numbers and the F-Distribution Table the critical values are:

  • \(CV_{treatment} = 5.3177\)
  • \(CV_{type} = 5.3177\)
  • \(CV_{type \times treatment} = 5.3177\)

6 Find Sum of Squares and Mean Squares


First we have to find the means for each group, then the means of the rows and columns.


Mississippi Quebec Row Mean
Chilled 18, 12.143, 17.3 (Mean = 15.814) 29.971, 32.7, 32.586 (Mean = 31.752) 23.783
Nonchilled 26.4, 27.343, 24.114 (Mean = 25.952) 33.229, 35.157, 37.614 (Mean = 35.333) 30.643
Column Mean 20.883 33.543 27.213


In the following equations:

  • r = replicants, or the number of values in each group
  • b = the number of type categories (Quebec and Mississippi)
  • a = the number of treatment categories (chilled and nonchilled)
  • \(\bar{X}_{GM}\) = Grand Mean, the mean of all values in the table


6.1 Sum and Mean Squares A

\[\begin{align} SS_{A} = SS_{treatment} & = rb \sum_{i=1}^A (\bar{X}_{i } - \bar{X}_{GM})^2 \\ & & \\ SS_{treatment} & = 3 \times 2 \times[(23.783 - 27.213)^2 + (30.642 - 27.213)^2] = 141.138 \\ & & \\ \text{We're just taking each row mean,} & \text{ subtracting it from the grand mean, squaring each of them, and then adding the result together.} \ & & \\ \text{Then we multiply that result by t} & \text{he number of samples in each group and by the number of }\textbf{ type}\text{ categories.} \\ & & \\ MS_{treatment} & = SS_{treatment} \mathbin{/} df_{treatment} = SS_{treatment} \mathbin{/} (a-1) \\ & & \\ MS_{treatment} & = 141.138 \mathbin{/} 1 = 141.138 \end{align}\]


6.2 Sum and Mean Squares B


\[\begin{align} SS_{B} = SS_{type} & = ra \sum_{j=1}^B (\bar{X}_{j} - \bar{X}_{GM})^2 \\ & & \\ SS_{type} & = 3 \times 2 \times[(20.883 - 27.213)^2 + (33.543 - 27.213)^2] = 480.827 \\ & & \\ \text{Again, pretty much the same as} & \text{ above, we're just taking each column mean, subtracting from the row mean, square the result and} \ & & \\ \text{add the two together. Then mul} & \text{tiply that result by the number of values in each group and the number of} \textbf{ treatment}\text{ categories.} \\ & & \\ MS_{type} & = SS_{type} \mathbin{/} df_{type} = SS_{type} \mathbin{/} (b-1) \\ & & \\ MS_{type} & = 480.827 \mathbin{/} (2-1) = 480.827 \end{align}\]


6.3 Sum and Mean Squares Interaction


\[\begin{align} SS_{(A \times B)} & = r \sum_{j=1}^b \sum_{i=1}^a (\bar{X}_{ij} - \bar{X}_{i} - \bar{X}_{j} + \bar{X}_{GM})^2 \\ & & \\ SS_{(A \times B)} & = 3 \times [(15.814 - 20.883 - 23.783 + 27.213)^2 + (31.752 - 33.543 - 23.783 + 27.213)^2 + \ & & \\ & \hspace{1cm} (25.952 - 20.883 - 30.643 + 27.213)^2 + (35.333 - 33.543 - 30.643 + 27.213)^2] \\ & & \\ SS_{(A \times B)} & = 32.246 \\ & & \\ \text{So in this case we're doing the} & \text{ sum of the square of the group means minus the row and column means plus the grand mean.} \\ & & \\ MS_{(A \times B)} & = SS_{(A \times B)} \mathbin{/} df_{(A \times B)} \\ & & \\ MS_{(A \times B)} & = 32.246 \mathbin{/} = 32.246 \end{align}\]


6.4 Sum and Mean Square Within

\[\begin{align} SS_W & = \sum_{k=1}^r \sum_{j=1}^B \sum_{i=1}^A (X_{ijk} - \bar{X}_{ij})^2 \\ & & \\ SS_W & = (18 - 15.814)^2 + (12.143 - 15.814)^2 + (17.3 - 15.814)^2 + (29.971 - 31.752)^2 + \ & & \\ & \hspace{.5cm} _{\cdot \space \cdot \space \cdot \space \cdot \space \cdot} \ & & \\ & \hspace{.5cm} + (37.614 - 35.333)^2 \\ & & \\ SS_W & = 40.404 \\ & & \\ \text{Here were taking each} & \text{ value in each group, subtracting from the mean of the group and squaring the result.} \\ & & \\ MS_W & = SS_W \mathbin{/} df_{within} = SS_W \mathbin{/} ab(n-1) \\ & & \\ MS_W & = 40.404 \mathbin{/} 8 = 5.050 \end{align}\]


7 F-Test


\[\begin{align} F_A = & \frac{MS_A}{MS_W} = \frac{141.138}{5.050} = 27.948 \\ & & \\ F_B = & \frac{MS_B}{MS_W} =\frac{480.827}{5.050} = 95.213 \\ & & \\ F_{(A \times B)} = & \frac{MS_{(A \times B)}}{MS_W} = \frac{32.246}{5.050} = 6.385 \end{align}\]


8 ANOVA Table

Now we can build our ANOVA table:


d.f. CV SS MS F
Treatment (A) 1 5.3177 141.138 141.138 27.948
Type (B) 1 5.3177 480.827 480.827 95.213
Interaction(AxB) 1 5.3177 32.246 32.246 6.385
Within (error) 8 40.404 5.050
Total 11 694.615


So based on that we can see that all three factors cross the \(\alpha = 0.05\) threshold. Doing the operation in R confirms this.


9 Do It In R

co2 <- co2[,.(meanUptake = mean(uptake)), by = .(Plant,Type,Treatment)]
head(co2)
##    Plant   Type  Treatment meanUptake
## 1:   Qn1 Quebec nonchilled   33.22857
## 2:   Qn2 Quebec nonchilled   35.15714
## 3:   Qn3 Quebec nonchilled   37.61429
## 4:   Qc1 Quebec    chilled   29.97143
## 5:   Qc2 Quebec    chilled   32.70000
## 6:   Qc3 Quebec    chilled   32.58571
co2.model = aov(meanUptake ~ Type + Treatment + Type:Treatment, data = co2)
co2.model
## Call:
##    aov(formula = meanUptake ~ Type + Treatment + Type:Treatment, 
##     data = co2)
## 
## Terms:
##                     Type Treatment Type:Treatment Residuals
## Sum of Squares  480.7906  141.1592        32.2471   40.4045
## Deg. of Freedom        1         1              1         8
## 
## Residual standard error: 2.247345
## Estimated effects may be unbalanced
anova(co2.model)
## Analysis of Variance Table
## 
## Response: meanUptake
##                Df Sum Sq Mean Sq F value    Pr(>F)    
## Type            1 480.79  480.79 95.1955  1.02e-05 ***
## Treatment       1 141.16  141.16 27.9492 0.0007402 ***
## Type:Treatment  1  32.25   32.25  6.3849 0.0354301 *  
## Residuals       8  40.40    5.05                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1


Yeah, that’s much faster.


10 F-Distribution Table \(\alpha\) = 0.05


/ dfN = 1 2 3 4 5 6 7 8 9 10 12 15 20 24 30 40 60 120
dfD=1 161.4476 199.5000 215.7073 224.5832 230.1619 233.9860 236.7684 238.8827 240.5433 241.8817 243.9060 245.9499 248.0131 249.0518 250.0951 251.1432 252.1957 253.2529 254.3144
2 18.5128 19.0000 19.1643 19.2468 19.2964 19.3295 19.3532 19.3710 19.3848 19.3959 19.4125 19.4291 19.4458 19.4541 19.4624 19.4707 19.4791 19.4874 19.4957
3 10.1280 9.5521 9.2766 9.1172 9.0135 8.9406 8.8867 8.8452 8.8123 8.7855 8.7446 8.7029 8.6602 8.6385 8.6166 8.5944 8.5720 8.5494 8.5264
4 7.7086 6.9443 6.5914 6.3882 6.2561 6.1631 6.0942 6.0410 5.9988 5.9644 5.9117 5.8578 5.8025 5.7744 5.7459 5.7170 5.6877 5.6581 5.6281
5 6.6079 5.7861 5.4095 5.1922 5.0503 4.9503 4.8759 4.8183 4.7725 4.7351 4.6777 4.6188 4.5581 4.5272 4.4957 4.4638 4.4314 4.3985 4.3650
6 5.9874 5.1433 4.7571 4.5337 4.3874 4.2839 4.2067 4.1468 4.0990 4.0600 3.9999 3.9381 3.8742 3.8415 3.8082 3.7743 3.7398 3.7047 3.6689
7 5.5914 4.7374 4.3468 4.1203 3.9715 3.8660 3.7870 3.7257 3.6767 3.6365 3.5747 3.5107 3.4445 3.4105 3.3758 3.3404 3.3043 3.2674 3.2298
8 5.3177 4.4590 4.0662 3.8379 3.6875 3.5806 3.5005 3.4381 3.3881 3.3472 3.2839 3.2184 3.1503 3.1152 3.0794 3.0428 3.0053 2.9669 2.9276
9 5.1174 4.2565 3.8625 3.6331 3.4817 3.3738 3.2927 3.2296 3.1789 3.1373 3.0729 3.0061 2.9365 2.9005 2.8637 2.8259 2.7872 2.7475 2.7067
10 4.9646 4.1028 3.7083 3.4780 3.3258 3.2172 3.1355 3.0717 3.0204 2.9782 2.9130 2.8450 2.7740 2.7372 2.6996 2.6609 2.6211 2.5801 2.5379
11 4.8443 3.9823 3.5874 3.3567 3.2039 3.0946 3.0123 2.9480 2.8962 2.8536 2.7876 2.7186 2.6464 2.6090 2.5705 2.5309 2.4901 2.4480 2.4045
12 4.7472 3.8853 3.4903 3.2592 3.1059 2.9961 2.9134 2.8486 2.7964 2.7534 2.6866 2.6169 2.5436 2.5055 2.4663 2.4259 2.3842 2.3410 2.2962
13 4.6672 3.8056 3.4105 3.1791 3.0254 2.9153 2.8321 2.7669 2.7144 2.6710 2.6037 2.5331 2.4589 2.4202 2.3803 2.3392 2.2966 2.2524 2.2064
14 4.6001 3.7389 3.3439 3.1122 2.9582 2.8477 2.7642 2.6987 2.6458 2.6022 2.5342 2.4630 2.3879 2.3487 2.3082 2.2664 2.2229 2.1778 2.1307
15 4.5431 3.6823 3.2874 3.0556 2.9013 2.7905 2.7066 2.6408 2.5876 2.5437 2.4753 2.4034 2.3275 2.2878 2.2468 2.2043 2.1601 2.1141 2.0658
16 4.4940 3.6337 3.2389 3.0069 2.8524 2.7413 2.6572 2.5911 2.5377 2.4935 2.4247 2.3522 2.2756 2.2354 2.1938 2.1507 2.1058 2.0589 2.0096
17 4.4513 3.5915 3.1968 2.9647 2.8100 2.6987 2.6143 2.5480 2.4943 2.4499 2.3807 2.3077 2.2304 2.1898 2.1477 2.1040 2.0584 2.0107 1.9604
18 4.4139 3.5546 3.1599 2.9277 2.7729 2.6613 2.5767 2.5102 2.4563 2.4117 2.3421 2.2686 2.1906 2.1497 2.1071 2.0629 2.0166 1.9681 1.9168
19 4.3807 3.5219 3.1274 2.8951 2.7401 2.6283 2.5435 2.4768 2.4227 2.3779 2.3080 2.2341 2.1555 2.1141 2.0712 2.0264 1.9795 1.9302 1.8780
20 4.3512 3.4928 3.0984 2.8661 2.7109 2.5990 2.5140 2.4471 2.3928 2.3479 2.2776 2.2033 2.1242 2.0825 2.0391 1.9938 1.9464 1.8963 1.8432
21 4.3248 3.4668 3.0725 2.8401 2.6848 2.5727 2.4876 2.4205 2.3660 2.3210 2.2504 2.1757 2.0960 2.0540 2.0102 1.9645 1.9165 1.8657 1.8117
22 4.3009 3.4434 3.0491 2.8167 2.6613 2.5491 2.4638 2.3965 2.3419 2.2967 2.2258 2.1508 2.0707 2.0283 1.9842 1.9380 1.8894 1.8380 1.7831
23 4.2793 3.4221 3.0280 2.7955 2.6400 2.5277 2.4422 2.3748 2.3201 2.2747 2.2036 2.1282 2.0476 2.0050 1.9605 1.9139 1.8648 1.8128 1.7570
24 4.2597 3.4028 3.0088 2.7763 2.6207 2.5082 2.4226 2.3551 2.3002 2.2547 2.1834 2.1077 2.0267 1.9838 1.9390 1.8920 1.8424 1.7896 1.7330
25 4.2417 3.3852 2.9912 2.7587 2.6030 2.4904 2.4047 2.3371 2.2821 2.2365 2.1649 2.0889 2.0075 1.9643 1.9192 1.8718 1.8217 1.7684 1.7110
26 4.2252 3.3690 2.9752 2.7426 2.5868 2.4741 2.3883 2.3205 2.2655 2.2197 2.1479 2.0716 1.9898 1.9464 1.9010 1.8533 1.8027 1.7488 1.6906
27 4.2100 3.3541 2.9604 2.7278 2.5719 2.4591 2.3732 2.3053 2.2501 2.2043 2.1323 2.0558 1.9736 1.9299 1.8842 1.8361 1.7851 1.7306 1.6717
28 4.1960 3.3404 2.9467 2.7141 2.5581 2.4453 2.3593 2.2913 2.2360 2.1900 2.1179 2.0411 1.9586 1.9147 1.8687 1.8203 1.7689 1.7138 1.6541
29 4.1830 3.3277 2.9340 2.7014 2.5454 2.4324 2.3463 2.2783 2.2229 2.1768 2.1045 2.0275 1.9446 1.9005 1.8543 1.8055 1.7537 1.6981 1.6376
30 4.1709 3.3158 2.9223 2.6896 2.5336 2.4205 2.3343 2.2662 2.2107 2.1646 2.0921 2.0148 1.9317 1.8874 1.8409 1.7918 1.7396 1.6835 1.6223
40 4.0847 3.2317 2.8387 2.6060 2.4495 2.3359 2.2490 2.1802 2.1240 2.0772 2.0035 1.9245 1.8389 1.7929 1.7444 1.6928 1.6373 1.5766 1.5089
60 4.0012 3.1504 2.7581 2.5252 2.3683 2.2541 2.1665 2.0970 2.0401 1.9926 1.9174 1.8364 1.7480 1.7001 1.6491 1.5943 1.5343 1.4673 1.3893
120 3.9201 3.0718 2.6802 2.4472 2.2899 2.1750 2.0868 2.0164 1.9588 1.9105 1.8337 1.7505 1.6587 1.6084 1.5543 1.4952 1.4290 1.3519 1.2539
3.8415 2.9957 2.6049 2.3719 2.2141 2.0986 2.0096 1.9384 1.8799 1.8307 1.7522 1.6664 1.5705 1.5173 1.4591 1.3940 1.3180 1.2214 1.0000