ANOVA Conditions

Prof Randi Garcia

2024-09-18

Announcements

Mini Project 1 (MP1)

Review of last class

  • Introduced the motivation behind the Analysis of Variance (ANOVA)
  • Data decomposition
  • Assembly line or CAT scan metaphor

Assembly line code

Calmness = Grand Mean + Animal Effect + Cue Effect + Interaction + Student Effect + Residuals

Leafhoppers

  • Now we start thinking about if those differences in treatment effects are real, or could possibly be due to chance error.
X. control sucrose glucose fructose
2.3 3.6 2.9 2.1
1.7 4.0 2.7 2.3
means 2.0 3.8 2.8 2.2
group effects -0.7 1.1 0.1 -0.5

One-way ANOVA Model

\[Y = f(X) + \epsilon\] The “t-test extension” for our model would be: \[Y = \mu_i + \epsilon\] The One-Way ANOVA model instead uses a treatment effects approach: \[Y = \mu + \alpha_i + \epsilon\] where \(\mu\) is the grand mean, \(\alpha_i\) is the treatment effect (difference from the grand mean for the \(i^{th}\) group), and \(\epsilon\) is the residual.

Analysis of Variance (ANOVA)

Formal ANOVA starts with the simple idea that we can compare our estimate of treatment effect variability to our estimate of chance error variability to measure how large our treatment effect is.  
\[Variability\:in\:Treatment\:E\mathit{f}\mathit{f}ects =\] \[True\:E\mathit{f}\mathit{f}ect\:Di\mathit{f}\mathit{f}erences + Error\] \[Variability\:in\:Residuals = Error\]  
We can construct a comparison, which we will call F:  
\[F = \frac{Variability\:in\:Treatment\:E\mathit{f}\mathit{f}ects}{Variability\:in\:Residuals}=\] \[\frac{True\:E\mathit{f}\mathit{f}ect\:Di\mathit{f}\mathit{f}erences + Error}{Error}\]  
If our null hypothesis, \({H}_{0}: True\:E\mathit{f}\mathit{f}ect\:Di\mathit{f}\mathit{f}erences=0\), is true, then what would we expect the F-ratio to equal?

Sum of Squares (SS)

ANOVA measures variability in treatment effects with the sum of squares (\(SS\)) divided by the number of units of unique information (\(df\)). For the One-Way design:

\[{SS}_{Treatments} = n\sum_{i=1}^{a}(\bar{y}_{i.}-\bar{y}_{..})^{2}\]

\[{SS}_{E} = \sum_{i=1}^{a}\sum_{j=1}^{n}({y}_{ij}-\bar{y}_{i.})^{2}\]

\[{SS}_{Total} = {SS}_{Treatments} + {SS}_{E}\]

where \(n\) is the group size, and \(a\) is the number of treatments.

Degrees of Freedom (df)

The \(df\) for a table equals the number of free numbers, the number of slots in the table you can fill in before the pattern of repetitions and adding to zero tell you what the remaining numbers have to be.

\[{df}_{Treatments}=a-1\]

\[{df}_{E}=N-a\]

Mean Squares (MS)

The ultimate statistic we want to calculate is \(F = \frac{Variability\:in\:Treatment\:E\mathit{f}\mathit{f}ects}{Variability\:in\:Residuals}\).

Variability in treatment effects: \[{MS}_{Treatments}=\frac{{SS}_{Treatments}}{{df}_{Treatments}}\]

Variability in residuals \[{MS}_{E}=\frac{{SS}_{E}}{{df}_{E}}\]

F-ratios and the F-distribution

Finally, the ratio of these two MS’s is called the F ratio. The following quantity is our test statistic for the null hypothesis that there are no treatment effects.

\[F = \frac{{MS}_{Treatments}}{{MS}_{E}}\]

F-ratios and the F-distribution

If the null hypothesis is true, then F is a random variable \(\sim F({df}_{Treatments}, {df}_{E})\). The F-distribution.

qplot(x = rf(500, 3, 4), geom = "density")

Inference Testing in ANOVA

\[{H}_{0}:\alpha_1=\alpha_2=\alpha_3=\alpha_4=0\]

OR, \({H}_{0}:\mu_1=\mu_2=\mu_3=\mu_4\)

\[H_a: Some\:\alpha_i\neq 0\]

Or \({H}_{A}:some\:\mu_i\neq\mu_j\). We can find the p-value for our F calculation with the following code

pf(17.422, 3, 4, lower.tail = FALSE)
[1] 0.009248454

ANOVA Source Table Leafhoppers

mod <- lm(days ~ diet, data = leaf)
anova(mod)
Analysis of Variance Table

Response: days
          Df Sum Sq Mean Sq F value   Pr(>F)   
diet       3   3.92  1.3067  17.422 0.009248 **
Residuals  4   0.30  0.0750                    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

ANOVA for One-Way ANOVA

Model:

\[{y}_{ij}=\mu+{\alpha}_{i}+{e}_{ij}\]

Null hypothesis:

\[{H}_{0}:{\alpha}_{1}={\alpha}_{2}=...={\alpha}_{a}=0\]

ANOVA Source table:

Source SS df MS F
Treatment \(n\sum_{i=1}^{a}(\bar{y}_{i.}-\bar{y}_{..})^{2}\) \(a-1\) \(\frac{{SS}_{Treatments}}{{df}_{Treatments}}\) \(\frac{{MS}_{Treatments}}{{MS}_{E}}\)
Error \(\sum_{i=1}^{a}\sum_{j=1}^{n}({y}_{ij}-\bar{y}_{i.})^{2}\) \(N-a\) \(\frac{{SS}_{E}}{{df}_{E}}\)

Running an ANOVA in R

Four steps (different from the 4 steps in chapter 0!):

  1. Boxplots or some data visualization
  2. Descriptive statistics (n, means, standard deviations)
  3. ANOVA
  4. Check assumptions

Step 1. Boxplots

#Note that boxplots are a bit silly in this example because n is only 2.
qplot(x = diet, y = days, data = leaf, geom = "boxplot")

Step 2. Descriptive Statistics

leaf %>%
  group_by(diet) %>%
  summarize(n = n(),
            m = mean(days),
            sd = sd(days))
# A tibble: 4 × 4
  diet         n     m    sd
  <chr>    <int> <dbl> <dbl>
1 control      2   2   0.424
2 fructose     2   2.2 0.141
3 glucose      2   2.8 0.141
4 sucrose      2   3.8 0.283

Step 3. ANOVA

mod <- lm(days ~ diet, data = leaf)
anova(mod)
Analysis of Variance Table

Response: days
          Df Sum Sq Mean Sq F value   Pr(>F)   
diet       3   3.92  1.3067  17.422 0.009248 **
Residuals  4   0.30  0.0750                    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Write a sentence interpreting the One-Way ANOVA results

There are statistically significant differences in leafhopper survival rates across diets, \(F(3, 4) = 17.42\), \(p = .009\).

OR

Diet has a statistically significant effect on leafhopper survival rates, \(F(3, 4) = 17.42\), \(p = .009\).

Try it!

Run first 3 steps (visualize, descriptive stats, and ANOVA) for the two datasets below. Write a sentence about your conclusion from the ANOVA.

Dataset 1: SandwichAnts

  • Factor: Bread
  • Response: Ants

Dataset 2: Meniscus

  • Factor: Method
  • Response: Displacement
library(Stat2Data)
data("SandwichAnts")
?SandwichAnts

Step 4: ANOVA Conditions

STOP!!…only under certain conditions can we rely on this inference. There were many assumptions built into:

  • how we decomposed the data, and
  • our thoughts about residual errors.

ANOVA Conditions

  • C. Constant effects
  • A. Additive effects
  • S. Same standard deviations
  • I. Independent residuals
  • N. Normally distributed residuals
  • Z. Zero mean residuals

Simulation in R: Assembly Line Metaphor

C. Constant effects

We assume every observation in a similar condition is affected exactly the same. (Gets the same “true score”).

animals_sim <- animals %>%
  mutate(benchmark = mean(calm)) %>%
  group_by(animal) %>%
  mutate(animal_mean = mean(calm),
         aminal_effect = animal_mean - benchmark)

A. Additive effects

We add the effects as we go down the assembly line.

The interaction effect captures the possibility that conditions have non-additive effects, but it is also added to everything else.

calm_sim = benchmark
         + aminal_effect
         + cue_effect
         + interaction_effect
         + student_effect

S. Same standard deviations

The piece of code for adding error is not dependent on which condition the observations is in.

 + rnorm(80, 0, 1.16)

I. Independent residuals

Takes 80 independent draws from a normal distribution.

 + rnorm(80, 0, 1.16)

N. Normally distributed residuals

It’s rnorm(), and not rbinom() or rpois()

 + rnorm(80, 0, 1.16)

Z. Zero mean residuals

The second argument is the mean.

 + rnorm(80, 0, 1.16)

How to check assumptions

  • C. Constant effectsthink about whether it is reasonable.

  • A. Additive effectsthink about whether it is reasonable.

  • S. Same standard deviations – is the biggest SD less than two times as large as the smallest?

  • I. Independent residualsthink about whether it is reasonable.

  • N. Normally distributed residuals – construct a histogram or normal probability plot of residuals.

  • Z. Zero mean residuals – construct a histogram or normal probability plot of residuals.

To check the Residual Assumptions: SINZ

S: calculate descriptive statistics and divide largest SD by smallest.

leaf %>%
  group_by(diet) %>%
  summarize(n = n(),
            m = mean(days),
            sd = sd(days))
# A tibble: 4 × 4
  diet         n     m    sd
  <chr>    <int> <dbl> <dbl>
1 control      2   2   0.424
2 fructose     2   2.2 0.141
3 glucose      2   2.8 0.141
4 sucrose      2   3.8 0.283
0.424/0.141
[1] 3.007092

To check the Residual Assumptions: SINZ

I: Judge for yourself – what do we think about the leafhopper dishes?

To check the Residual Assumptions: SINZ

N: Look at histogram and normal probability plots of residuals.

#normal probability plot of residuals
plot(mod, which = 2)

To check the Residual Assumptions: SINZ

Z: Look at histogram of residuals.

#histogram of residuals
qplot(x = mod$residuals, bins = 4)