Difference between revisions of "SMHS ANOVA"

From SOCR
Jump to: navigation, search
(Two-way ANOVA)
m (See Also)
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
==[[SMHS| Scientific Methods for Health Sciences]] - Analysis of Variance (ANOVA) ==
+
==[[SMHS|Scientific Methods for Health Sciences]] - Analysis of Variance (ANOVA)==
 
 
  
 
===Overview===
 
===Overview===
[[EBook#Chapter_XI:_Analysis_of_Variance_.28ANOVA.29|Analysis of Variance (ANOVA)]] is a method that is commonly applied to analyze differences between group means. In ANOVA, we divide the observed variance into components attributed to different sources of variation. It is a widely used statistical technique that provides a statistical test of whether or not the means of several groups are equal; ANOVA can be thought of as a generalized t-test for more than 2 groups. If there are only 2 groups, ANOVA results coincide with the corresponding results of a 2-sample independent t-test. Here, we introduce ANOVA, both one-way and two-way, and provide examples.
+
[[EBook#Chapter_XI:_Analysis_of_Variance_.28ANOVA.29|Analysis of Variance (ANOVA)]] is a statistical method used to test differences between two or more group means. While the [[AP_Statistics_Curriculum_2007_Infer_2Means_Dep|t-test]] is limited to comparing two groups, ANOVA generalizes this to multiple groups, partitioning the total observed variance into components attributable to different sources (e.g., between‑group and within‑group variation). This chapter introduces one‑way and two‑way ANOVA, discusses underlying assumptions, provides step‑by‑step calculations, and illustrates applications with real data and R examples.
  
 
===Motivation===
 
===Motivation===
In the previous two-sample inference, we applied a t-test to compare two independent group means. What if we want to compare more than 2 independent samples? In this case, we will need to decompose the entire variation into components that allow us to analyze the variance of the entire dataset. Suppose 5 varieties of a particular crop are tested for further study. A field was divided into 20 plots, with each variety planted in 4 plots. The measurements are shown in the table below:
+
Suppose a plant biologist wants to compare the yield of five different varieties of wheat. Each variety is planted in four randomly assigned plots, yielding 20 plots total. The yields (in bushels/acre) are:
  
 
<center>
 
<center>
{| class="wikitable" style="text-align:center; width:35%" border="1"
+
{| class="wikitable" style="text-align:center; width:60%" border="1"
 
|-
 
|-
|A ||B|| C|| D|| E
+
! Variety A !! Variety B !! Variety C !! Variety D !! Variety E
 
|-
 
|-
|26.2|| 29.2|| 29.1 ||21.3|| 20.1
+
| 26.2 || 29.2 || 29.1 || 21.3 || 20.1
 
|-
 
|-
|24.3|| 28.1 ||30.8 ||22.4|| 19.3
+
| 24.3 || 28.1 || 30.8 || 22.4 || 19.3
 
|-
 
|-
|21.8 ||27.3|| 33.9|| 24.3 ||19.9
+
| 21.8 || 27.3 || 33.9 || 24.3 || 19.9
|-
 
|28.1|| 31.2|| 32.8|| 21.8|| 22.1
 
 
|-
 
|-
 +
| 28.1 || 31.2 || 32.8 || 21.8 || 22.1
 
|}
 
|}
 
</center>
 
</center>
  
 +
If we denote the population means of the five varieties by <math>\mu_1, \mu_2, \mu_3, \mu_4, \mu_5</math>, we could perform <math>\binom{5}{2}=10</math> pairwise t‑tests. However, this approach inflates the Type I error rate and lacks a single overall test. ANOVA provides an integrated framework that simultaneously tests
 +
<math>H_0: \mu_1 = \mu_2 = \mu_3 = \mu_4 = \mu_5</math> vs. <math>H_a:</math> at least one mean differs,
 +
while controlling the family‑wise error rate.
 +
 +
===Theory===
 +
 +
====One‑Way ANOVA====
 +
One‑way ANOVA compares the means of <math>k</math> independent groups under the assumption that the populations are normally distributed with equal variances.
 +
 +
*Notation:
 +
 +
* <math>y_{ij}</math> = <math>j</math>‑th observation in group <math>i</math> (<math>i=1,\dots,k</math>; <math>j=1,\dots,n_i</math>).
 +
* <math>n_i</math> = number of observations in group <math>i.</math>
 +
* <math>n = \sum_{i=1}^k n_i</math> = total sample size.
 +
* Group mean: <math>\bar{y}_{i.} = \frac{1}{n_i}\sum_{j=1}^{n_i} y_{ij}.</math>
 +
* Grand mean: <math>\bar{y}_{..} = \frac{1}{n}\sum_{i=1}^k \sum_{j=1}^{n_i} y_{ij}.</math>
 +
 +
*Variance decomposition:
 +
* Total sum of squares: <math>SS_{total} = \sum_{i=1}^k \sum_{j=1}^{n_i} (y_{ij} - \bar{y}_{..})^2</math> with <math>df_{total}=n-1.</math>
 +
* Between‑groups sum of squares: <math>SS_{between} = \sum_{i=1}^k n_i (\bar{y}_{i.} - \bar{y}_{..})^2</math> with <math>df_{between}=k-1.</math>
 +
* Within‑groups (error) sum of squares: <math>SS_{within} = \sum_{i=1}^k \sum_{j=1}^{n_i} (y_{ij} - \bar{y}_{i.})^2</math> with <math>df_{within}=n-k.</math>
 +
 +
The fundamental identity is:
 +
<math>SS_{total} = SS_{between} + SS_{within}</math>, and likewise <math>df_{total} = df_{between} + df_{within}.</math>
 +
 +
*Mean squares and F‑statistic:
 +
* <math>MS_{between} = \frac{SS_{between}}{df_{between}}</math>
 +
* <math>MS_{within} = \frac{SS_{within}}{df_{within}}</math>
 +
* Test statistic: <math>F = \frac{MS_{between}}{MS_{within}}.</math>
 +
 +
Under <math>H_0</math> (all population means equal), <math>F</math> follows an F‑distribution with <math>(k-1,\; n-k)</math> degrees of freedom. A large <math>F</math> value suggests that the between‑group variation is substantial relative to within‑group variation, providing evidence against <math>H_0</math>.
 +
 +
*ANOVA table (general form):
 
<center>
 
<center>
{| class="wikitable" style="text-align:center; width:35%" border="1"
+
{| class="wikitable" style="text-align:center; width:70%" border="1"
|-
 
|A|| 26.2,24.3,21.8,28.1
 
 
|-
 
|-
|B|| 29.2,28.1,27.3,31.2
+
! Source of Variation !! Degrees of Freedom (df) !! Sum of Squares (SS) !! Mean Square (MS) !! F‑statistic !! P‑value
 
|-
 
|-
|C|| 29.1,30.8,33.9,32.8
+
| Between Groups || <math>k-1</math> || <math>SS_{between}</math> || <math>MS_{between}</math> || <math>F = \frac{MS_{between}}{MS_{within}}</math> || <math>P(F_{k-1,\; n-k} > F_{\text{obs}})</math>
 
|-
 
|-
|D|| 21.3,22.4,24.3,21.8
+
| Within Groups (Error) || <math>n-k</math> || <math>SS_{within}</math> || <math>MS_{within}</math> || ||
|-
 
|E|| 20.1,19.3,19.9,22.1
 
 
|-
 
|-
 +
| Total || <math>n-1</math> || <math>SS_{total}</math> || || ||
 
|}
 
|}
 
</center>
 
</center>
  
Using ANOVA, the data are regarded as random samples from 5 populations. Suppose the population means are denoted as $\mu_{1},\mu_{2},\mu_{3},\mu_{4},$ and $\mu_{5}$, and the population standard deviations are denoted as $\sigma_{1},\sigma_{2},\sigma_{3},\sigma_{4},$ and $\sigma_{5}$. One method would be to apply $\binom{5}{2}=10$ separate t-tests and compare all independent pairs of groups. However, in this case, ANOVA would be much easier and more powerful.
+
*Assumptions and diagnostics:
 +
# '''Independence''': Observations are independent within and across groups.
 +
# '''Normality''': The residuals ( <math>e_{ij} = y_{ij} - \bar{y}_{i.}</math> ) should be approximately normally distributed for each group. This can be checked with Q‑Q plots, Shapiro‑Wilk, or Kolmogorov‑Smirnov tests.
 +
# '''Homoscedasticity''' (equal variances): Group populations should have the same variance <math>\sigma^2</math>. Formal tests (Levene’s, Bartlett’s) or visual inspection of residual‑vs‑fitted plots can be used.
 +
 
 +
If assumptions are violated, consider transformations (log, square‑root) or non‑parametric alternatives (Kruskal‑Wallis test).
  
===Theory===
+
*Effect size – Eta‑squared and partial Eta‑squared:
 +
* <math>\eta^2 = \frac{SS_{between}}{SS_{total}}</math> (proportion of total variance explained by group differences).
 +
* Partial <math>\eta^2 = \frac{SS_{between}}{SS_{between} + SS_{within}}</math>.
 +
Interpretation: 0.01 = small, 0.06 = medium, 0.14 = large effect (Cohen, 1988).
  
====One-way ANOVA====
+
*Post‑hoc comparisons: If the overall <math>F</math>-test is significant, we conduct pairwise comparisons to identify which groups differ. Common methods (with correction for multiple testing) include:
One-way ANOVA expands our inference methods to study and compare k independent samples. In this case, we will be decomposing the entire variation in the data into independent components.
+
* Tukey’s HSD (Honest Significant Difference)
 +
* Bonferroni correction
 +
* Scheffé’s method
  
*Notation: $y_{ij}$ is the $j^{th}$ measurement from group $i$; $k$ is the number of groups; $n_{i}$ is the number of observations in group $i$; $n$ is the total number of observations and $n=n_{1}+n_{2}+⋯+n_{k}$. The group mean for group $i$ is $\bar y_{i}$=$\frac{\sum_{j=1}^{n_{i}} y_{ij}} {n_{i}}$, and the grand mean is $\bar y =\bar y_{..}=$ $\frac{\sum_{i=1}^{k}\sum_{j=1}^{n}_{i}y_{ij}}{n}$
+
====Two‑Way ANOVA====
 +
Two‑way ANOVA extends the idea to two categorical factors (A and B), allowing us to test:
 +
# Main effect of factor A.
 +
# Main effect of factor B.
 +
# Interaction effect between A and B (whether the effect of one factor depends on the level of the other).
  
*Difference between means (i.e., compare each group mean to the grand mean).
+
*Model:
** (Total) The total variance is calculated as the total sum of squares (SST) divided by the total degrees of freedom (df(total)).  $SST=\sum_{i=1}^{k}\sum_{j=1}^{n_i}(y_{ij}-\bar y_{..})^{2}$ and $df(total)=n-1$.
+
<math>y_{ijk} = \mu + \alpha_i + \beta_j + (\alpha\beta)_{ij} + \varepsilon_{ijk}</math>
** (Between/Treatment) The difference between each group mean and the grand mean: $SST(between)$=$\sum_{i=1}^{k} {n_{i} (\bar y_{i.}-\bar y_{..})^2}$, degrees of freedom $df(between)=k-1$;
+
where:
** (Within/Error) Sum square due to error (combination of the variations within each group): $SSE(Error)=\sum_{i=1}^{k} n_{i}(\bar y_{i.}- \bar y_{..})^2$, degrees of freedom $df(within)=n-k$.  
+
* <math>\mu</math> = grand mean.
** ANOVA variance decomposition yields
+
* <math>\alpha_i</math> = effect of level <math>i</math> of factor A (<math>i=1,\dots,a</math>).
$$\sum_{i=1}^{k} {\sum_{j=1}^{n_{i}} {(y_{ij}- \bar y_{..})^2 }} = \sum_{i=1}^{k} {n_{i} (\bar y_{i.}-\bar y_{..})^2} + \sum_{i=1}^{k} {\sum_{j=1}^{n_i} {(y_{ij}-\bar y_{i.})^2}},$$
+
* <math>\beta_j</math> = effect of level <math>j</math> of factor B (<math>j=1,\dots,b</math>).
:: that is $SST(total)$=$SST(between)$+$SSE(within)$ and $df(total)$=$df(between)$+$df(within).$
+
* <math>(\alpha\beta)_{ij}</math> = interaction effect.
 +
* <math>\varepsilon_{ijk} \sim N(0,\sigma^2)</math> (error), with <math>k=1,\dots,r</math> replicates per cell.
  
*Calculations:
+
*Sum of squares decomposition:
<center>
+
<math>SS_{total} = SS_A + SS_B + SS_{AB} + SS_{error}</math>
{| class="wikitable" style="text-align:center; width:50%" border="1"
+
with degrees of freedom:
|-
+
<math>df_{total} = N-1,\; df_A = a-1,\; df_B = b-1,\; df_{AB} = (a-1)(b-1),\; df_{error} = N - ab</math>,
| Variance Source || Degrees of Freedom (df) || Sum of Squares (SS) || Mean Sum  of Squares (MS) || F-Statistics || [http://socr.ucla.edu/htmls/SOCR_Distributions.html P-value]
+
where <math>N = a \times b \times r</math> is the total number of observations.
|-
 
| Treatment Effect (Between Group) || k-1 || <math>\sum_{i=1}^{k}{n_i(\bar{y}_{i,.}-\bar{y})^2}</math> || <math>MST(Between)={SST(Between)\over df(Between)}</math> || <math>F_o = {MST(Between)\over MSE(Within)}</math> || <math>P(F_{(df(Between), df(Within))} > F_o)</math>
 
|-
 
| Error (Within Group) || n-k || <math>\sum_{i=1}^{k}{\sum_{j=1}^{n_i}{(y_{i,j}-\bar{y}_{i,.})^2}}</math> || <math>MSE(Within)={SSE(Within)\over df(Within)}</math> ||  || [http://socr.ucla.edu/Applets.dir/Normal_T_Chi2_F_Tables.htm F-Distribution Calculator]
 
|-
 
| Total || n-1 || <math>\sum_{i=1}^{k}{\sum_{j=1}^{n_i}{(y_{i,j} - \bar{y})^2}}</math> ||  ||  || [[SOCR_EduMaterials_AnalysisActivities_ANOVA_1 | ANOVA Activity]]
 
|}
 
</center>
 
  
* ANOVA hypotheses (general form): $H_{\sigma}:\mu_{1}=\mu_{2}==\mu_{k}$; $H_{a}:\mu_{i}≠\mu_{j}$ for some $i≠j$.  The test statistics: $F_{0}=\frac{MST(between)}{MSE(within)}$ , if $F_{0}$ is large, then there is a lot between group variation, relative to the within group variation. Therefore, the discrepancies between the group means are large compared to the variability within the groups (error). That is large $F_{0}$ provides strong evidence against $H_{0}$.
+
*Hypotheses:
 +
# <math>H_0^{(A)}: \alpha_1 = \alpha_2 = \dots = \alpha_a = 0</math> (no main effect of A).
 +
# <math>H_0^{(B)}: \beta_1 = \beta_2 = \dots = \beta_b = 0</math> (no main effect of B).
 +
# <math>H_0^{(AB)}: (\alpha\beta)_{ij} = 0</math> for all <math>i,j</math> (no interaction).
  
*Examples: given the following data from a hands-on study.
+
*ANOVA table for two‑way design (balanced, with replication):
 
<center>
 
<center>
{| class="wikitable" style="text-align:center; width:35%" border="1"
+
{| class="wikitable" style="text-align:center; width:75%" border="1"
|-
 
| || colspan=3|Groups
 
|-
 
|Index|| A|| B|| C
 
 
|-
 
|-
|1 || 0|| 1|| 4
+
! Source !! df !! SS !! MS !! F !! P‑value
 
|-
 
|-
|2|| 1|| 0|| 5
+
| Factor A || <math>a-1</math> || <math>SS_A</math> || <math>MS_A = \frac{SS_A}{df_A}</math> || <math>F_A = \frac{MS_A}{MS_{error}}</math> || …
 
|-
 
|-
|3|| ||2||
+
| Factor B || <math>b-1</math> || <math>SS_B</math> || <math>MS_B = \frac{SS_B}{df_B}</math> || <math>F_B = \frac{MS_B}{MS_{error}}</math> || …
 
|-
 
|-
|$n_{i}$|| 2|| 3|| 2
+
| Interaction A×B || <math>(a-1)(b-1)</math> || <math>SS_{AB}</math> || <math>MS_{AB} = \frac{SS_{AB}}{df_{AB}}</math> || <math>F_{AB} = \frac{MS_{AB}}{MS_{error}}</math> ||
 
|-
 
|-
| $s$ ||1|| 3|| 9
+
| Error || <math>N-ab</math> || <math>SS_{error}</math> || <math>MS_{error} = \frac{SS_{error}}{df_{error}}</math> || ||
|-
 
|$\bar y_{l}$|| 0.5|| 1|| 4.5
 
 
|-
 
|-
 +
| Total || <math>N-1</math> || <math>SS_{total}</math> || || ||
 
|}
 
|}
 
</center>
 
</center>
  
Using this data, we have the following ANOVA table:
+
*Assumptions: same as one‑way ANOVA (independence, normality, homoscedasticity). Additionally, the interaction model assumes that the effects are additive unless the interaction term is included.
<center>
+
 
{| class="wikitable" style="text-align:center; width:50%" border="1"
+
*Interactions: If the interaction is significant, the main effects cannot be interpreted separately. Simple‑effects analysis (comparing levels of one factor at fixed levels of the other) is then appropriate.
|-
+
 
| Variance Source || Degrees of Freedom (df) || Sum  of Squares (SS) || Mean Sum  of Squares (MS) || F-Statistics || [http://socr.ucla.edu/htmls/dist/Fisher_Distribution.html P-value]
+
===Applications===
|-
+
 
| Treatment Effect (Between Group) || 3-1 || <math>\sum_{i=1}^{k}{n_i(\bar{y}_{i,.}-\bar{y})^2}=19.86</math> || <math>{SST(Between)\over df(Between)}={19.86\over 2}</math> || <math>F_o = {MST(Between)\over MSE(Within)}=13.24</math> || <math>P(F_{(df(Between), df(Within))} > F_o)=0.017</math>
+
====Example 1: One‑Way ANOVA with R====
|-
+
A clinical trial tests three new analgesics (Drug A, B, C) against a placebo for pain relief (score 0‑10, lower is better). Data:
| Error (Within Group) || 7-3 || <math>\sum_{i=1}^{k}{\sum_{j=1}^{n_i}{(y_{i,j}-\bar{y}_{i,.})^2}}=3</math> || <math>{SSE(Within)\over df(Within)}={3\over 4}</math> || || [http://socr.ucla.edu/Applets.dir/Normal_T_Chi2_F_Tables.htm F-Distribution Calculator]
+
 
|-
+
<pre>
| Total || 7-1 || <math>\sum_{i=1}^{k}{\sum_{j=1}^{n_i}{(y_{i,j} - \bar{y})^2}}=22.86</math> ||  ||  || [[SOCR_EduMaterials_AnalysisActivities_ANOVA_1 | Anova Activity]]
+
Pain = c(5,6,4,5,7, 3,4,5,4,3, 2,3,4,3,2, 6,7,6,5,7)
|}
+
Group = factor(rep(c("Placebo","DrugA","DrugB","DrugC"), each=5))
</center>
+
</pre>
 +
 
 +
'''Step 1: Exploratory data analysis'''
 +
<pre>
 +
boxplot(Pain ~ Group, col="lightblue", main="Pain Score by Treatment",  
 +
        xlab="Treatment", ylab="Pain Score")
 +
</pre>
 +
 
 +
'''Step 2: Fit ANOVA model'''
 +
<pre>
 +
fit <- aov(Pain ~ Group)
 +
summary(fit)
 +
</pre>
 +
 
 +
Output:
 +
 
 +
<pre>
 +
            Df Sum Sq Mean Sq F value  Pr(>F)   
 +
Group        3 40.00  13.333  20.00 1.25e-05 ***
 +
Residuals  16  10.67  0.667                   
 +
---
 +
Significant at <0.001
 +
</pre>
 +
 
 +
'''Step 3: Check assumptions'''
  
Based on the ANOVA table above, we can reject the null hypothesis at $\alpha=0.05.$
+
<pre>
 +
# Normality of residuals
 +
shapiro.test(residuals(fit))
  
*ANOVA conditions: valid if (1) design conditions: all groups of observations represent random samples from their population respectively. Plus, all the observations within each group are independent of each other; (2) population conditions: the k population distributions must be approximately normal. If sample size is large, the normality condition is less crucial. Plus, the standard deviations of all populations are equal, which can be slightly relaxed when $0.5≤\frac{\sigma_{i}}{\sigma_{j}}≤2,$ for all $i$ and $j$, none of the population variance is twice larger than any of the other ones.
+
# Homogeneity of variances (Levene’s test)
 +
library(car)
 +
leveneTest(Pain ~ Group)
 +
</pre>
  
====Two-way ANOVA====
+
'''Step 4: Post‑hoc comparisons (Tukey)'''
Two-way ANOVA decomposes the variance of a dataset into independent (orthogonal) components when we have two grouping factors.
 
Notations first: two-way model: $y_{ijk}=\mu+\tau_{i}+\beta_{j}+γ_{ij}+\varepsilon_{ijk},$ for all $1≤i≤a,1≤j≤b$ and $1≤k≤r.$ The measurement $y_{ijk}$ represents A-factor level $i$, and B-factor level $j$, observation-index $k$ -- the number of replications; $a_{i}$ is the number of A-factor observations at level $i,a=a_{1}+⋯+a_{I}$; $b_{j}$ is the number of B-factor observations at level $j$, $b=b_{1}+⋯+b_{J}$; $N$ is the total number of observations and $N=a\times b\times r$. Here $\mu$ is the overall mean response, $\tau_{i}$ is the effect due to the $i^{th}$ level of factor A, $\beta_{j}$ is the effect due to the $j^{th}$ level of factor B, and $\gamma_{ij}$ is the effect due to any interaction between the $i^{th}$ level of factor A and $j^{th}$ level of factor B. The mean for A-factor group mean at level $I$ and B-factor at level $j$ is $\bar{y}_{ij.}=\frac{\sum_{k=1}^{r} {y_{ijk}}} {r},$ the grand mean is $\bar {y} =\bar{y}_{...} = \frac{\sum_{k=1}^{r} {\sum_{i=1}^{a} {\sum_{j=1}^{b} {y_{ijk}}}}} {n}$, and we have we have:
 
$$SST(total)=SS(A)+SS(B)+SS(AB)+SSE,$$
 
where:
 
$$ SST(total)=\sum_{i=1}^{a} {\sum_{j=1}^{b} {\sum_{k=1}^{r} {(y_{ijk}-\bar{y}_{...})^2}}},$$
 
  
$$SS(A)=r\times b \times \sum_{i=1}^{a} {(\bar{y}_{i..}-\bar{y}_{...})^2},$$
+
<pre>
 +
TukeyHSD(fit, conf.level=0.95)
 +
plot(TukeyHSD(fit))
 +
</pre>
  
$$SS(B)=r\times a \times \sum_{j=1}^{b} {(\bar{y}_{.j.}-\bar{y}_{...})^2},$$
+
'''Step 5: Effect size'''
  
$$SS(AB) =r\times \sum_{i=1}^{a} {\sum_{j=1}^{b} {(\bar{y}_{ij.}-\bar{y}_{i..}-\bar{y}_{.j.}-\bar{y}_{...})^2}},$$
+
<pre>
 +
eta_squared <- summary(fit)[[1]][1,2] / sum(summary(fit)[[1]][,2])
 +
eta_squared
 +
</pre>
  
$$SSE= \sum_{i=1}^{a} {\sum_{j=1}^{b} {\sum_{k=1}^{r}{(y_{ijk}-\bar{y}_{ij.})^2}}}.$$
+
==== Example 2: Two‑Way ANOVA with Interaction====
 +
A study examines how gender (M/F) and exercise regimen (None, Light, Heavy) affect cholesterol level (mg/dL). Data are balanced with 5 subjects per cell.
  
*Hypotheses:  
+
'''R analysis:'''
**Null hypotheses: (1) the population means of the first factor are equal, which is like the one-way ANOVA for the row factor; (2) the population means of the second factor are equal, which is like the one-way ANOVA for the column factor; (3) there is no interaction between the two factors, which is similar to performing a [[AP_Statistics_Curriculum_2007_Contingency_Indep|test for independence with contingency tables]].
 
**Factors: factor A and factor B are independent variables in a two-way ANOVA.
 
**Treatment groups: formed by making all possible combinations of two factors. For example, if the factor A has 3 levels and factor B has 5 levels, then there will be $3\times 5=15$ different treatment groups.
 
**Main effect: involves the dependent variable one at a time. The interaction is ignored for this part.
 
**Interaction effect: the effect that one factor has on the other factor. The degree of freedom is the product of the two degrees of freedom of each factor.
 
  
*Calculations: It is assumed that main effect A has $a$ levels (and $df(A) = a-1$), main effect B has $b$ levels (and ($df(B) = b-1$), $r$ is the sample size of each treatment, and $N = a\times b\times r$ is the total sample size. Notice the overall degree of freedom is once again one less than the total sample size.
+
<pre>
 +
chol <- c(220,210,230,215,225, 200,195,205,210,200,
 +
          190,185,195,190,180, 210,205,215,200,210,
 +
          180,175,185,180,170, 170,165,175,170,160)
 +
gender <- factor(rep(rep(c("M","F"), each=5), 3))
 +
exercise <- factor(rep(c("None","Light","Heavy"), each=10))
  
<center>
+
# Two‑way ANOVA with interaction
{| class="wikitable" style="text-align:center; width:50%" border="1"
+
fit2 <- aov(chol ~ gender * exercise)
|-
+
summary(fit2)
| Variance Source || Degrees of Freedom (df) || Sum  of Squares (SS) || Mean Sum  of Squares (MS) || F-Statistics || [http://socr.umich.edu/html/dist/ P-value]
 
|-
 
| Main Effect A || df(A)=a-1 || <math>SS(A)=r\times b\times\sum_{i=1}^{a}{(\bar{y}_{i,.,.}-\bar{y})^2}</math> || <math>{SS(A)\over df(A)}</math> || <math>F_o = {MS(A)\over MSE}</math> || <math>P(F_{(df(A), df(E))} > F_o)</math>
 
|-
 
| Main Effect B || df(B)=b-1 || <math>SS(B)=r\times a\times\sum_{j=1}^{b}{(\bar{y}_{., j,.}-\bar{y})^2}</math> || <math>{SS(B)\over df(B)}</math> || <math>F_o = {MS(B)\over MSE}</math> || <math>P(F_{(df(B), df(E))} > F_o)</math>
 
|-
 
| A vs.B Interaction || df(AB)=(a-1)(b-1) || <math>SS(AB)=r\times \sum_{i=1}^{a}{\sum_{j=1}^{b}{((\bar{y}_{i, j,.}-\bar{y}_{i, .,.})+(\bar{y}_{., j,.}-\bar{y}))^2}}</math> || <math>{SS(AB)\over df(AB)}</math> || <math>F_o = {MS(AB)\over MSE}</math> || <math>P(F_{(df(AB), df(E))} > F_o)</math>
 
|-
 
| Error || <math>N-a\times b</math> || <math>SSE=\sum_{k=1}^r{\sum_{i=1}^{a}{\sum_{j=1}^{b}{(\bar{y}_{i, j,k}-\bar{y}_{i, j,.})^2}}}</math> || <math>{SSE\over df(Error)}</math> ||  ||
 
|-
 
| Total || N-1 || <math>SST=\sum_{k=1}^r{\sum_{i=1}^{a}{\sum_{j=1}^{b}{(\bar{y}_{i, j,k}-\bar{y}_{., .,.})^2}}}</math> ||  ||  || [[SOCR_EduMaterials_AnalysisActivities_ANOVA_2 | ANOVA Activity]]
 
|}
 
</center>
 
  
* Two-way ANOVA is valid if:
+
# Check interaction visually
:: (1) the population from which the samples were obtained are normally or approximately normally distributed;
+
interaction.plot(exercise, gender, chol, type="b", col=c("blue","red"),
:: (2) the samples are independent;
+
                pch=c(16,18), main="Interaction Plot: Cholesterol")
:: (3) the variances of the populations are equal;
+
</pre>
:: (4) the groups have the same sample size.
 
  
* Studying the residuals: When the ANOVA assumptions hold, the residuals should be approximately normally distributed. Otherwise, there may be outliers in the data, one or more groups may come from non-normal distributions, or the linear mode is just not able to capture the data complexity. Normality testing of the residuals gives an indication of whether the sample populations are roughly normal, but may not indicate the underlying cause of this non-normality. Normality tests include:
+
If the interaction is not significant, we may refit the model without it:
** [[SOCR_EduMaterials_AnalysisActivities_KolmogorovSmirnoff|Kolmogorov-Smirnov test]] and [http://socr.umich.edu/html/dist Anderson-Darling test] are based on the estimate of the empirical CDF.
 
** [[AP_Statistics_Curriculum_2007_MultiVar_ANOVA#Wilk.E2.80.99s_.5C.28.5CLambda.5C.29 | Shapiro-Wilk’s test]] is a correlation based test applicable for $n < 50$.
 
** Graphical methods can convey the patterns and residual however they may be subjective.
 
  
* Examples
+
<pre>
** [[AP_Statistics_Curriculum_2007_ANOVA_2Way | Clinical example  of knee pain study]]
+
fit2_additive <- aov(chol ~ gender + exercise)
** [[SOCR_Activity_ANOVA_SnailsSexualDimorphism| Snails Sexual Dimorphism activity]]
+
summary(fit2_additive)
 +
</pre>
  
===Applications===
+
===Software===
* [[SOCR_EduMaterials_Activities_BoxAndWhiskerChart|This activity]] presents the Box and Whisker Chart, which is often used in exploratory data analyses. It demonstrates the range, standard deviation, mean and quartiles of the values and is especially useful in comparing statistical data. This article illustrated the implementation of the chart in SOCR with comprehensive introduction. It also included the application of this method in different areas.
 
  
* [[SOCR_EduMaterials_AnalysisActivities_ANOVA_2|The SOCR Two-Way ANOVA Java Applet]] includes examples of two-way analysis of variance using SOCR tools. It illustrated the application of two-way ANOVA with examples applied in the SOCR. It also expanded the two-way ANOVA in softwares like R and SAS.
+
====R Commands for ANOVA====
 +
* One‑way ANOVA: aov(y ~ group, data)
 +
* Two‑way ANOVA (with interaction): aov(y ~ factor1 * factor2, data)
 +
* Check assumptions:
 +
  - Normality: shapiro.test(residuals(model))
 +
  - Homogeneity of variances: leveneTest(y ~ group, data)
 +
* Post‑hoc tests: TukeyHSD(model), pairwise.t.test(y, group, p.adjust="bonferroni")
 +
* Effect size: library(effectsize); eta_squared(model)
  
* [[SOCR_Activity_ANOVA_SnailsSexualDimorphism| Ther SOCR Snails Sexual Dimorphism Activity]] shows an application of ANOVA. This activity recreates part of the design of a classification method for the Cocholotoma septemspirale snail. By observing multiple traits of the shells, the original researchers were able to decide on a series of dimorphisms (difference in forms) between male and female snails. This article presents a comprehensive illustration of the example.
+
====Complete R Example (One‑Way)====
  
===Software ===
+
<pre>
* [http://socr.umich.edu/html/ana/ SOCR Analyses Java Applets]
+
# Simulated data: three diets (A, B, C) and weight loss (lbs)
* [[SOCR_EduMaterials_AnalysisActivities_ANOVA_1 | One-Way ANOVA Activity]] and [[SOCR_Activity_ANOVA_SnailsSexualDimorphism| SOCR 2-way ANOVA Activity]]
+
set.seed(123)
* R:
+
n <- 20
:: Generic R ANOVA
+
diet <- factor(sample(rep(c("A","B","C"), c(7,7,6))))
# fit a  model
+
weight_loss <- c(rnorm(7,5,1.2), rnorm(7,7,1.2), rnorm(6,4,1.2))
# one-way ANOVA with completely randomized design
+
data <- data.frame(diet, weight_loss)
fit <- aov(y ~ A, data = mydata)
 
# randomized block design (B as the blocking factor)
 
fit <- aov(y ~ A + B, data = mydata)
 
# two-way factorial design
 
fit <- aov(y ~ A + B + A*B, data = mydata)
 
# to check out the model fitted with type I ANOVA table
 
summary(fit)
 
# type III SS and F test
 
drop1(fit, ~., test=’F’)
 
  
:: Example R ANOVA: Suppose an FDA study plans to test four alternative pain relief medications for migraine headache. For the experiment 36 volunteers agreed to participate in the study and they were randomly assigned to four groups corresponding to the four treatments. The subjects are instructed to take the pain killers during their next migraine headache episode and report their pain on a scale of 1 (no pain) to 10 (worst pain) one hour after taking the drug.
+
# ANOVA
# the 4x9=36 reported pain scores
+
model <- aov(weight_loss ~ diet, data=data)
pain = c(4, 5, 4, 3, 2, 4, 3, 4, 4, 6, 8, 4, 5, 4, 6, 5, 8, 6, 6, 7, 6, 6, 7, 5, 6, 5, 5, 7, 8, 7, 5, 8, 6, 6, 4, 9)
+
summary(model)
drug = c(rep("A",9), rep("B",9), rep("C",9), rep("D",9)) # make the group labels
 
migraine = data.frame(pain,drug) # make a frame from the data
 
migraine # print the data
 
plot(pain ~ drug, data=migraine) # EDA: plot the box-and-whisker plots for the 4 groups separately
 
  
aov_res = aov(pain ~ drug, data=migraine) # run the ANOVA
+
# Diagnostic plots
summary(aov_res) # show summary results
+
par(mfrow=c(2,2))
 +
plot(model)
 +
par(mfrow=c(1,1))
  
:: The ANOVA F-test addresses the question whether or not there are significant differences in the $k$ population means. It does not directly provide information about how and which group means may differ. When rejecting $H_0$ in ANOVA, additional tests may be required to determine what groups may be different in their mean response.
+
# Post‑hoc (if significant)
 +
if(summary(model)[[1]][1,5] < 0.05) {
 +
  TukeyHSD(model)
 +
}
  
# compute the pairwise comparisons between group means (how many of these are there in this case?)
+
# Effect size
# to correct for multiple testing, we can specify a specific "p.adjust.methods" correction method
+
library(effectsize)
# (e.g., “Bonferroni”, "holm", "hochberg", "hommel", "BH", "BY", "fdr", "none")
+
eta_squared(model)
pairwise.t.test(pain, drug, p.adjust="bonferroni")
+
</pre>
 
# alternatively, the Tukey's method (Honest Significance Test) may be used to create a set of
 
# confidence intervals on the differences between means with the specified family-wise error rate.
 
TukeyHSD(aov_res, conf.level =0.95)
 
  
 
===Problems===
 
===Problems===
* Tom was shopping for a ping pong table that could be taken apart quickly and easily. For some reason, the salesman happened to have a table of the assembly times (sec) for the three tables. Using ANOVA, do you think there is a difference in the average time of assembly for the three brands of ping pong tables?
+
 
 +
====Problem 1 (One‑Way ANOVA)====
 +
A manufacturer compares the assembly times (seconds) of three brands of ping‑pong tables.
 
<center>
 
<center>
{| class="wikitable" style="text-align:center;" border="1"
+
{| class="wikitable" style="text-align:center; width:60%" border="1"
|-
 
! Assembly_time_(sec)||Brand
 
|-
 
| 93.0||1
 
|-
 
| 67.0||1
 
|-
 
| 77.0||1
 
|-
 
| 92.0||1
 
|-
 
| 97.0||1
 
|-
 
| 62.0||1
 
|-
 
| 136.0||2
 
|-
 
| 120.0||2
 
|-
 
| 115.0||2
 
|-
 
| 104.0||2
 
|-
 
| 115.0||2
 
|-
 
| 121.0||2
 
|-
 
| 102.0||2
 
|-
 
| 130.0||2
 
|-
 
| 198.0||3
 
 
|-
 
|-
| 217.0||3
+
! Assembly Time (sec) !! Brand
 
|-
 
|-
| 209.0||3
+
| 93, 67, 77, 92, 97, 62 || 1
 
|-
 
|-
| 221.0||3
+
| 136, 120, 115, 104, 115, 121, 102, 130 || 2
 
|-
 
|-
| 190.0||3
+
| 198, 217, 209, 221, 190 || 3
 
|}
 
|}
 
</center>
 
</center>
  
: (a) We can say that there is no reason to reject the null that the average assembly times are the same
+
At <math>\alpha=0.05</math>, test whether the average assembly times differ across brands.
: (b) We should reject the null that the average assembly times are the same
 
  
* Based on the data in the previous problem, what is the value for R square:
+
* Solution outline:
: (a) 0.342
 
: (b) 0.143
 
: (c) 0.832
 
: (d) 0.943
 
  
* Tom is curious to see if two-door vehicles drive faster on average than four-door vehicles. He parks behind a bush so as not to be seen, and records the car type and the speed reading. Here are the results (1 means two-door, and 2 means four-door):
+
1. State hypotheses: <math>H_0: \mu_1 = \mu_2 = \mu_3</math> vs. <math>H_a</math>: at least one mean differs.
<center>
+
 
{| class="wikitable" style="text-align:center;" border="1"
+
2. Check assumptions (normality, equal variance).
|-
 
! Speed_(MPH)||Vehicle_Type
 
|-
 
| 45||2
 
|-
 
| 45||2
 
|-
 
| 40||2
 
|-
 
| 69||1
 
|-
 
| 72||1
 
|-
 
| 40||1
 
|-
 
| 75||2
 
|-
 
| 19||2
 
|-
 
| 62||1
 
|-
 
| 43||2
 
|-
 
| 75||1
 
|-
 
| 42||2
 
|-
 
| 58||1
 
|-
 
| 58||1
 
|-
 
| 47||2
 
|-
 
| 48||2
 
|-
 
| 49||2
 
|-
 
| 45||2
 
|-
 
| 54||2
 
|}
 
</center>
 
  
: At the 1% significance level, should we reject the null hypothesis that that average speed is the same for both types of vehicles?
+
3. Compute ANOVA table.
: (a) Yes, we should reject the null hypothesis.
 
: (b) No, we should not reject the null hypothesis.
 
: (c) There is not enough information.
 
  
*  Based on data above, what is the value for R square?
+
4. Make decision based on F‑test.
: (a) 0.432
 
: (b) 0.983
 
: (c) 0.308
 
: (d) 0.231
 
  
*  In a two-way ANOVA test, which of the following is not the typical null hypothesizes?
+
5. If significant, perform post‑hoc comparisons.
: (a) The population means of the first factor are equal.
 
: (b) The population means of the first and second factor are equal.
 
: (c) The population means of the second factor are equal.
 
: (d) There is no interaction between the two factors.
 
  
*  Suppose that two factors, A and B, is thought to affect the top speed of a car. We will use two-way ANOVA analysis. Are the population means of factor A equal?
+
====Problem 2 (Two‑Way ANOVA)====
 +
A researcher investigates how gender (M, F) and education level (High School, Bachelor, Graduate) affect starting salary. Data (in thousands):
 
<center>
 
<center>
{| class="wikitable" style="text-align:center;" border="1"
+
{| class="wikitable" style="text-align:center; width:70%" border="1"
 
|-
 
|-
! Top_Speed||A||B
+
! Gender !! Education !! Salaries
 
|-
 
|-
| 93.0||1||1
+
| M || High School || 45, 48, 50
 
|-
 
|-
| 136.0||1||2
+
| M || Bachelor || 65, 68, 70
 
|-
 
|-
| 198.0||1||3
+
| M || Graduate || 85, 88, 90
 
|-
 
|-
| 88.0||2||1
+
| F || High School || 42, 44, 46
 
|-
 
|-
| 148.0||2||2
+
| F || Bachelor || 60, 62, 64
 
|-
 
|-
| 279.0||2||3
+
| F || Graduate || 80, 82, 84
 
|}
 
|}
 
</center>
 
</center>
: (a) Yes, they are equal.
 
: (b) No, they are not equal.
 
  
* Use the data above and apply the two-way ANOVA analysis, are the population means of factor B equal?
+
Test for main effects and interaction. If the interaction is significant, conduct simple‑effects analysis.
: (a) Yes, they are equal.
+
 
: (b) No, they are not equal.
+
===References===
  
* Use data from table above and apply the two-way ANOVA analysis, is there an interaction effect between the two factors
+
* [https://sda.statisticalcomputing.org/learning See the SOCR SDA app ANOVA Learning Module]
: (a) Yes, they are equal.
+
* [http://wiki.stat.ucla.edu/socr/index.php/Probability_and_statistics_EBook#Chapter_XI:_Analysis_of_Variance_.28ANOVA.29 SOCR ANOVA Chapter]
: (b) No, they are not equal.
+
* [https://en.wikipedia.org/wiki/Analysis_of_variance ANOVA – Wikipedia]
 +
* Cohen, J. (1988). Statistical Power Analysis for the Behavioral Sciences (2nd ed.). Hillsdale, NJ: Lawrence Erlbaum.
 +
* Kutner, M.H., Nachtsheim, C.J., Neter, J., & Li, W. (2005). Applied Linear Statistical Models (5th ed.). McGraw‑Hill.
  
=== References===
+
===See Also===
* [http://wiki.stat.ucla.edu/socr/index.php/Probability_and_statistics_EBook#Chapter_XI:_Analysis_of_Variance_.28ANOVA.29  SOCR]
+
* [[AP_Statistics_Curriculum_2007_ANOVA_1Way | AP Statistics: One‑Way ANOVA]]
* [http://en.wikipedia.org/wiki/Analysis_of_variance  ANOVA Wikipedia]
+
* [[AP_Statistics_Curriculum_2007_ANOVA_2Way | AP Statistics: Two‑Way ANOVA]]
 +
* [[SOCR_EduMaterials_AnalysisActivities_ANOVA_1 | SOCR One‑Way ANOVA Activity]]
 +
* [[SOCR_EduMaterials_AnalysisActivities_ANOVA_2 | SOCR Two‑Way ANOVA Activity]]
  
  
 
<hr>
 
<hr>
* SOCR Home page: http://www.socr.umich.edu
+
* SOCR Home page: https://socr.umich.edu
  
{{translate|pageName=http://wiki.socr.umich.edu/index.php?title=SMHS_ANOVA}}
+
{{translate|pageName=https://wiki.socr.umich.edu/index.php?title=SMHS_ANOVA}}

Latest revision as of 17:18, 10 February 2026

Scientific Methods for Health Sciences - Analysis of Variance (ANOVA)

Overview

Analysis of Variance (ANOVA) is a statistical method used to test differences between two or more group means. While the t-test is limited to comparing two groups, ANOVA generalizes this to multiple groups, partitioning the total observed variance into components attributable to different sources (e.g., between‑group and within‑group variation). This chapter introduces one‑way and two‑way ANOVA, discusses underlying assumptions, provides step‑by‑step calculations, and illustrates applications with real data and R examples.

Motivation

Suppose a plant biologist wants to compare the yield of five different varieties of wheat. Each variety is planted in four randomly assigned plots, yielding 20 plots total. The yields (in bushels/acre) are:

Variety A Variety B Variety C Variety D Variety E
26.2 29.2 29.1 21.3 20.1
24.3 28.1 30.8 22.4 19.3
21.8 27.3 33.9 24.3 19.9
28.1 31.2 32.8 21.8 22.1

If we denote the population means of the five varieties by \(\mu_1, \mu_2, \mu_3, \mu_4, \mu_5\), we could perform \(\binom{5}{2}=10\) pairwise t‑tests. However, this approach inflates the Type I error rate and lacks a single overall test. ANOVA provides an integrated framework that simultaneously tests \(H_0: \mu_1 = \mu_2 = \mu_3 = \mu_4 = \mu_5\) vs. \(H_a:\) at least one mean differs, while controlling the family‑wise error rate.

Theory

One‑Way ANOVA

One‑way ANOVA compares the means of \(k\) independent groups under the assumption that the populations are normally distributed with equal variances.

  • Notation:
  • \(y_{ij}\) = \(j\)‑th observation in group \(i\) (\(i=1,\dots,k\); \(j=1,\dots,n_i\)).
  • \(n_i\) = number of observations in group \(i.\)
  • \(n = \sum_{i=1}^k n_i\) = total sample size.
  • Group mean\[\bar{y}_{i.} = \frac{1}{n_i}\sum_{j=1}^{n_i} y_{ij}.\]
  • Grand mean\[\bar{y}_{..} = \frac{1}{n}\sum_{i=1}^k \sum_{j=1}^{n_i} y_{ij}.\]
  • Variance decomposition:
  • Total sum of squares\[SS_{total} = \sum_{i=1}^k \sum_{j=1}^{n_i} (y_{ij} - \bar{y}_{..})^2\] with \(df_{total}=n-1.\)
  • Between‑groups sum of squares\[SS_{between} = \sum_{i=1}^k n_i (\bar{y}_{i.} - \bar{y}_{..})^2\] with \(df_{between}=k-1.\)
  • Within‑groups (error) sum of squares\[SS_{within} = \sum_{i=1}^k \sum_{j=1}^{n_i} (y_{ij} - \bar{y}_{i.})^2\] with \(df_{within}=n-k.\)

The fundamental identity is\[SS_{total} = SS_{between} + SS_{within}\], and likewise \(df_{total} = df_{between} + df_{within}.\)

  • Mean squares and F‑statistic:
  • \(MS_{between} = \frac{SS_{between}}{df_{between}}\)
  • \(MS_{within} = \frac{SS_{within}}{df_{within}}\)
  • Test statistic\[F = \frac{MS_{between}}{MS_{within}}.\]

Under \(H_0\) (all population means equal), \(F\) follows an F‑distribution with \((k-1,\; n-k)\) degrees of freedom. A large \(F\) value suggests that the between‑group variation is substantial relative to within‑group variation, providing evidence against \(H_0\).

  • ANOVA table (general form):
Source of Variation Degrees of Freedom (df) Sum of Squares (SS) Mean Square (MS) F‑statistic P‑value
Between Groups \(k-1\) \(SS_{between}\) \(MS_{between}\) \(F = \frac{MS_{between}}{MS_{within}}\) \(P(F_{k-1,\; n-k} > F_{\text{obs}})\)
Within Groups (Error) \(n-k\) \(SS_{within}\) \(MS_{within}\)
Total \(n-1\) \(SS_{total}\)
  • Assumptions and diagnostics:
  1. Independence: Observations are independent within and across groups.
  2. Normality: The residuals ( \(e_{ij} = y_{ij} - \bar{y}_{i.}\) ) should be approximately normally distributed for each group. This can be checked with Q‑Q plots, Shapiro‑Wilk, or Kolmogorov‑Smirnov tests.
  3. Homoscedasticity (equal variances): Group populations should have the same variance \(\sigma^2\). Formal tests (Levene’s, Bartlett’s) or visual inspection of residual‑vs‑fitted plots can be used.

If assumptions are violated, consider transformations (log, square‑root) or non‑parametric alternatives (Kruskal‑Wallis test).

  • Effect size – Eta‑squared and partial Eta‑squared:
  • \(\eta^2 = \frac{SS_{between}}{SS_{total}}\) (proportion of total variance explained by group differences).
  • Partial \(\eta^2 = \frac{SS_{between}}{SS_{between} + SS_{within}}\).

Interpretation: 0.01 = small, 0.06 = medium, 0.14 = large effect (Cohen, 1988).

  • Post‑hoc comparisons: If the overall \(F\)-test is significant, we conduct pairwise comparisons to identify which groups differ. Common methods (with correction for multiple testing) include:
  • Tukey’s HSD (Honest Significant Difference)
  • Bonferroni correction
  • Scheffé’s method

Two‑Way ANOVA

Two‑way ANOVA extends the idea to two categorical factors (A and B), allowing us to test:

  1. Main effect of factor A.
  2. Main effect of factor B.
  3. Interaction effect between A and B (whether the effect of one factor depends on the level of the other).
  • Model\[y_{ijk} = \mu + \alpha_i + \beta_j + (\alpha\beta)_{ij} + \varepsilon_{ijk}\]

where:

  • \(\mu\) = grand mean.
  • \(\alpha_i\) = effect of level \(i\) of factor A (\(i=1,\dots,a\)).
  • \(\beta_j\) = effect of level \(j\) of factor B (\(j=1,\dots,b\)).
  • \((\alpha\beta)_{ij}\) = interaction effect.
  • \(\varepsilon_{ijk} \sim N(0,\sigma^2)\) (error), with \(k=1,\dots,r\) replicates per cell.
  • Sum of squares decomposition\[SS_{total} = SS_A + SS_B + SS_{AB} + SS_{error}\]

with degrees of freedom\[df_{total} = N-1,\; df_A = a-1,\; df_B = b-1,\; df_{AB} = (a-1)(b-1),\; df_{error} = N - ab\], where \(N = a \times b \times r\) is the total number of observations.

  • Hypotheses:
  1. \(H_0^{(A)}: \alpha_1 = \alpha_2 = \dots = \alpha_a = 0\) (no main effect of A).
  2. \(H_0^{(B)}: \beta_1 = \beta_2 = \dots = \beta_b = 0\) (no main effect of B).
  3. \(H_0^{(AB)}: (\alpha\beta)_{ij} = 0\) for all \(i,j\) (no interaction).
  • ANOVA table for two‑way design (balanced, with replication):
Source df SS MS F P‑value
Factor A \(a-1\) \(SS_A\) \(MS_A = \frac{SS_A}{df_A}\) \(F_A = \frac{MS_A}{MS_{error}}\)
Factor B \(b-1\) \(SS_B\) \(MS_B = \frac{SS_B}{df_B}\) \(F_B = \frac{MS_B}{MS_{error}}\)
Interaction A×B \((a-1)(b-1)\) \(SS_{AB}\) \(MS_{AB} = \frac{SS_{AB}}{df_{AB}}\) \(F_{AB} = \frac{MS_{AB}}{MS_{error}}\)
Error \(N-ab\) \(SS_{error}\) \(MS_{error} = \frac{SS_{error}}{df_{error}}\)
Total \(N-1\) \(SS_{total}\)
  • Assumptions: same as one‑way ANOVA (independence, normality, homoscedasticity). Additionally, the interaction model assumes that the effects are additive unless the interaction term is included.
  • Interactions: If the interaction is significant, the main effects cannot be interpreted separately. Simple‑effects analysis (comparing levels of one factor at fixed levels of the other) is then appropriate.

Applications

Example 1: One‑Way ANOVA with R

A clinical trial tests three new analgesics (Drug A, B, C) against a placebo for pain relief (score 0‑10, lower is better). Data:

Pain = c(5,6,4,5,7, 3,4,5,4,3, 2,3,4,3,2, 6,7,6,5,7)
Group = factor(rep(c("Placebo","DrugA","DrugB","DrugC"), each=5))

Step 1: Exploratory data analysis

boxplot(Pain ~ Group, col="lightblue", main="Pain Score by Treatment", 
        xlab="Treatment", ylab="Pain Score")

Step 2: Fit ANOVA model

fit <- aov(Pain ~ Group)
summary(fit)

Output:

            Df Sum Sq Mean Sq F value   Pr(>F)    
Group        3  40.00  13.333   20.00 1.25e-05 ***
Residuals   16  10.67   0.667                     
---
Significant at <0.001

Step 3: Check assumptions

# Normality of residuals
shapiro.test(residuals(fit))

# Homogeneity of variances (Levene’s test)
library(car)
leveneTest(Pain ~ Group)

Step 4: Post‑hoc comparisons (Tukey)

TukeyHSD(fit, conf.level=0.95)
plot(TukeyHSD(fit))

Step 5: Effect size

eta_squared <- summary(fit)[[1]][1,2] / sum(summary(fit)[[1]][,2])
eta_squared

Example 2: Two‑Way ANOVA with Interaction

A study examines how gender (M/F) and exercise regimen (None, Light, Heavy) affect cholesterol level (mg/dL). Data are balanced with 5 subjects per cell.

R analysis:

chol <- c(220,210,230,215,225, 200,195,205,210,200, 
          190,185,195,190,180, 210,205,215,200,210,
          180,175,185,180,170, 170,165,175,170,160)
gender <- factor(rep(rep(c("M","F"), each=5), 3))
exercise <- factor(rep(c("None","Light","Heavy"), each=10))

# Two‑way ANOVA with interaction
fit2 <- aov(chol ~ gender * exercise)
summary(fit2)

# Check interaction visually
interaction.plot(exercise, gender, chol, type="b", col=c("blue","red"), 
                 pch=c(16,18), main="Interaction Plot: Cholesterol")

If the interaction is not significant, we may refit the model without it:

fit2_additive <- aov(chol ~ gender + exercise)
summary(fit2_additive)

Software

R Commands for ANOVA

  • One‑way ANOVA: aov(y ~ group, data)
  • Two‑way ANOVA (with interaction): aov(y ~ factor1 * factor2, data)
  • Check assumptions:
 - Normality: shapiro.test(residuals(model))
 - Homogeneity of variances: leveneTest(y ~ group, data)
  • Post‑hoc tests: TukeyHSD(model), pairwise.t.test(y, group, p.adjust="bonferroni")
  • Effect size: library(effectsize); eta_squared(model)

Complete R Example (One‑Way)

# Simulated data: three diets (A, B, C) and weight loss (lbs)
set.seed(123)
n <- 20
diet <- factor(sample(rep(c("A","B","C"), c(7,7,6))))
weight_loss <- c(rnorm(7,5,1.2), rnorm(7,7,1.2), rnorm(6,4,1.2))
data <- data.frame(diet, weight_loss)

# ANOVA
model <- aov(weight_loss ~ diet, data=data)
summary(model)

# Diagnostic plots
par(mfrow=c(2,2))
plot(model)
par(mfrow=c(1,1))

# Post‑hoc (if significant)
if(summary(model)[[1]][1,5] < 0.05) {
  TukeyHSD(model)
}

# Effect size
library(effectsize)
eta_squared(model)

Problems

Problem 1 (One‑Way ANOVA)

A manufacturer compares the assembly times (seconds) of three brands of ping‑pong tables.

Assembly Time (sec) Brand
93, 67, 77, 92, 97, 62 1
136, 120, 115, 104, 115, 121, 102, 130 2
198, 217, 209, 221, 190 3

At \(\alpha=0.05\), test whether the average assembly times differ across brands.

  • Solution outline:

1. State hypotheses\[H_0: \mu_1 = \mu_2 = \mu_3\] vs. \(H_a\): at least one mean differs.

2. Check assumptions (normality, equal variance).

3. Compute ANOVA table.

4. Make decision based on F‑test.

5. If significant, perform post‑hoc comparisons.

Problem 2 (Two‑Way ANOVA)

A researcher investigates how gender (M, F) and education level (High School, Bachelor, Graduate) affect starting salary. Data (in thousands):

Gender Education Salaries
M High School 45, 48, 50
M Bachelor 65, 68, 70
M Graduate 85, 88, 90
F High School 42, 44, 46
F Bachelor 60, 62, 64
F Graduate 80, 82, 84

Test for main effects and interaction. If the interaction is significant, conduct simple‑effects analysis.

References

See Also





Translate this page:

(default)
Uk flag.gif

Deutsch
De flag.gif

Español
Es flag.gif

Français
Fr flag.gif

Italiano
It flag.gif

Português
Pt flag.gif

日本語
Jp flag.gif

България
Bg flag.gif

الامارات العربية المتحدة
Ae flag.gif

Suomi
Fi flag.gif

इस भाषा में
In flag.gif

Norge
No flag.png

한국어
Kr flag.gif

中文
Cn flag.gif

繁体中文
Cn flag.gif

Русский
Ru flag.gif

Nederlands
Nl flag.gif

Ελληνικά
Gr flag.gif

Hrvatska
Hr flag.gif

Česká republika
Cz flag.gif

Danmark
Dk flag.gif

Polska
Pl flag.png

România
Ro flag.png

Sverige
Se flag.gif