ANOVA (Stats)

We also studied this from a different angle in the Math Methods in Biology course: ANOVA (Maths)

Variance decomposition

Law of Total Variance

var[Y]⏟total variance=E[var[Y|X]]⏟within groups variance+var[E[Y|X]]⏟between groups variance.

Where:

  • Y is a vector of observations, e.g. trait values
  • X is a vector of indicators telling which group each individual measurement comes from.
  • This only holds if the residual variance is independent from the variance in covariates - if there is correlation there are some problems, but we won't worry about this for now

Proof: Wikipedia

Since this relation is only true if both come from the same population, we can determine how much the left side differs from the right side and this can tell us how likely it is that our two samples came from the same population.

Example

We will return from our example from last week (comparing whitefish populations in the Baltic Sea).

The following math term is a way of saying that the expectation of Yi depends on which group it is in.

E[Yi|Xi]={μ1 if Xi=0 (Population 1)μ2 if Xi=1 (Population 2).

Since for this example we are pretending we don't know what the true population means are, we can calculate the sample means from both groups and assign these to a vector of the same length:

mu = c() mu[X==0] = mean(Y[X==0]) # this is the same as mean(y) 
mu[X==1] = mean(Y[X==1]) # this is the same as mean(z)

Components

Total variance

Since both samples are the same size (n), the size of the entire vector is 2n. So the formula for the total variance is:

var[Y]=12n∑i=12n(Yi−Y¯)2

Within Groups Variance

This is equal to the average of the variances within each group. So we add the variance from population 1 and population 2, then divide by two (the number of groups):

E[var[y|x]]=12(var[Yi|Xi=0]]+var[Yi|Xi=1]])

Now, doing some algebra, given the formulas for true variance from the maths class, we can simplify this to:

E[var[y|x]]=12n∑i=12n(Yi−E[Yi|Xi])2

(remember that E[Yi|Xi] can be either μ1 or μ2 depending on Xi). In R, we simply do var(Y - mu). (where mu is our vector of sample means)

Between Groups Variance

The mean of the group means is μ¯=12(μ1+μ2). We use this for the between groups variance calculation:

var[E[y|x]]=12((μ1−μ¯)2+(μ2−μ¯)2)

This can be simplified to:

var[E[y|x]]=var[μ−μ¯]

And in R, simply var(mu-mean(Y))

Proportion of explained variance

Now that we have decomposed the variance into components, we can analyze the proportion of variance explained by the different groups:

variance explained=between group variancetotal variance=var[E[Y[X]]var[Y]

Note that with some algebra you can show that this is actually the same as the multiple r-squared. We can show this in R by comparing doing this calculation manually with the R-squared value from a linear model.

F-statistic

F-statistic
Ok, this is how this ANOVA analysis works with two populations. But the point of this class was to examine more than two. We use the F-statistic to do this, using the Residual sum of squares.

The F-statistic is the quotient of the between groups variance with the within groups variance:

F=between group variancewithin groups variance

In statistical software, the F test used is a bit different and looks more like:

F=∑i=1kn(μi−Y¯)2⏞bg sum of squares/(k−1)∑i=1kn(Yi−μi)2⏟residual sum of squares/k(n−1)=k(n−1)×[between groups variance](k−1)×[within group variance]

Where k representes the number of groups
This term with k is the degrees of freedom