| Replicate | A | B | C | D |
|---|---|---|---|---|
| 1 | 11 | 9 | 7 | 5 |
| 2 | 10 | 8 | 6 | 4 |
| 3 | 9 | 7 | 5 | 3 |
FANR 6750
Fall 2026
In lecture 4, we learned about fitting models with categorical predictors with 1 (measurement error) or 2 (frog) levels
Linear models with these categorical predictors correspond to one-sample or two-sample t-tests
Binary categorical predictors are quite common in ecological studies (e.g., male vs female, juvenile vs adult, disturbed vs undisturbed)
However, you will often encounter categorical predictors with >2 levels
Foresters are studying the effect of 4 different fertilizers (treatments) on the growth of loblolly pine, which are grown on 3 plots (replicates) receiving each treatment. Data are average height per plot after 5 years:
Treatment
|
||||
|---|---|---|---|---|
| Replicate | A | B | C | D |
| 1 | 11 | 9 | 7 | 5 |
| 2 | 10 | 8 | 6 | 4 |
| 3 | 9 | 7 | 5 | 3 |
Notation
The number of groups (treatments) is \(a=4\)
The number of observations within each group (replicates) is \(n=3\)
\(y_{ij}\) denotes the \(j\)th observation from the \(i\)th group
What counts as an observation?
Experimental unit
the physical unit that receives a particular treatment
Observational unit
the physical unit on which measurements are taken
These are not always the same!
Examples
Agricultural fields given different fertilizer, crop yield measured
Rats given different diets, disease state measured
Microcosm given different predator abundance, tadpole growth measured
Question: Is there a difference in growth among the four treatment groups?

Hypotheses:
\(\large H_0 : \mu_A = \mu_B = \mu_C = \mu_D\)
\(\large H_a :\) At least one inequality
How should we test the null?
Fortunately, extending the previous linear model to include additional predictor levels is “straightforward”
\[\large y_{ij} = \beta_0 + \beta_ix_{ij} + \epsilon_{ij}\]
\[\large \epsilon_{ij} \sim normal(0, \sigma)\]
What’s tricky is how we code \(\large x_{ij}\) when it is more than just 0 or 1
Fortunately, what we learned about how R codes dummy variables will help
View a subset (rows 1, 4, 7, & 10) of the data:
Treatment Replicate Height
1 A 1 11
4 B 1 9
7 C 1 7
10 D 1 5
View the corresponding rows of the model matrix2
(Intercept) TreatmentB TreatmentC TreatmentD
1 1 0 0 0
4 1 1 0 0
7 1 0 1 0
10 1 0 0 1
The model matrix has one row for each observation and one column for every parameter in the model
Multiplying the model matrix by the parameter matrix gives the expected value of each observation3
\[\large E[y_{ij}] = \beta_0 + \beta_1 I(B)_{ij} + \beta_2 I(C)_{ij} + \beta_3 I(D)_{ij}\]
where \(I(B/C/D)_{ij}\) are dummy variables (0/1 depending on treatment)
# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 10.0 0.577 17.3 0.000000126
2 TreatmentB -2.00 0.816 -2.45 0.0400
3 TreatmentC -4.00 0.816 -4.90 0.00120
4 TreatmentD -6.00 0.816 -7.35 0.0000801
Be sure you understand what each parameter means!
t-statistics and p-values are calculated and interpreted in the same way as the frog example
But wait, the null hypothesis was \(\mu_A = \mu_B = \mu_C = \mu_D\)
Call:
lm(formula = Height ~ Treatment, data = loblollydata)
Residuals:
Min 1Q Median 3Q Max
-1 -1 0 1 1
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 10.000 0.577 17.32 1.3e-07 ***
TreatmentB -2.000 0.816 -2.45 0.0400 *
TreatmentC -4.000 0.816 -4.90 0.0012 **
TreatmentD -6.000 0.816 -7.35 8.0e-05 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 1 on 8 degrees of freedom
Multiple R-squared: 0.882, Adjusted R-squared: 0.838
F-statistic: 20 on 3 and 8 DF, p-value: 0.000449
Notice the last line of this output. Where did this p-value come from and what does it mean?
The F-statistic tests whether all regression coefficients (other than the intercept) are all 0
\(H_0\): All regression coefficients are 0
\(H_A\): At least one regression coefficient is not 0
F-statistics are the ratio of sample variances
\[F = \frac{s^2_A}{s^2_B}\]
Null hypothesis is that population variances are equal ( \(\sigma^2_A = \sigma^2_B\) )
Always positive (variances cannot be negative)
Usually > 1 (by putting larger variance in the numerator)
Where do the variances come from in the linear model and what do they tell us about differences among groups?
To understand why the test is based on variance, it is helpful to consider several types of means:
\[\large \bar{y}. = \frac{\sum_i \sum_jy_{ij}}{a \times n}\]

To understand why the test is based on variance, it is helpful to consider several types of means:
\[\large \bar{y}. = \frac{\sum_i \sum_jy_{ij}}{a \times n}\]
\[\large \bar{y}_i = \frac{\sum_j y_{ij}}{n}\]

To understand why the test is based on variance, it is helpful to consider several types of means:
\[\large \bar{y}. = \frac{\sum_i \sum_jy_{ij}}{a \times n}\]
\[\large \bar{y}_i = \frac{\sum_j y_{ij}}{n}\]
We can now decompose the observations as4:
\[y_{ij} = \color{#446E9B}{\bar{y}.} + \color{#D47500}{(\bar{y}_i - \bar{y}.)} + \color{#3CB521}{(y_{ij} - \bar{y}_i)}\]

The decomposition:
\[\large y_{ij} = \color{#446E9B}{\bar{y}.} + \color{#D47500}{(\bar{y}_i - \bar{y}.)} + \color{#3CB521}{(y_{ij} - \bar{y}_i)}\]
The additive model:
\[\large y_{ij} = \color{#446E9B}{\mu} + \color{#D47500}{\alpha_i} + \color{#3CB521}{\epsilon_{ij}}\]
where
\[\large \epsilon_{ij} \sim normal(0, \sigma^2)\]
\[\large y_{ij} = \mu + \alpha_i + \epsilon_{ij}\]
\[\large \epsilon_{ij} \sim normal(0, \sigma^2)\]
Notes:
\(\mu\) is the grand mean of the population, estimated by \(\bar{y}.\)
\(\alpha_i\) is the effect of treatment i, estimated by \(\bar{y}_i - \bar{y}.\)
It is the deviation of the group mean from the grand mean
If all \(\alpha_i = 0\), there is no treatment effect
Thus, we can re-write the null hypothesis \(H_0 : \alpha_1 = \alpha_2=... =\alpha_a = 0\)
\(\epsilon_{ij}\) is the residual error, estimated by \(y_{ij} - \bar{y}_i\)
Note that this is just a re-arrangement of the linear model
Back to the F-test. Where to the variances for the test come from and how do they related to the null hypothesis?
To answer those questions, we need to quantify where the variation in our data comes from
To do that, we calculate sums of squares
First, we calculate the sums of squares among, which quantifies variation among treatment groups5
\[\large SS_A = n \sum_i (\bar{y}_i - \bar{y}.)^2\]
Question: Is there a difference in growth among the four treatment groups?
Variation among groups
\[\large SS_A = n \sum_i (\bar{y}_i - \bar{y}.)^2\]
Next, we calculate the sums of squares within, which quantifies variation within treatment groups
\[\large SS_W = \sum_i \sum_j (y_{ij} - \bar{y}_i)^2\]
Question: Is there a difference in growth among the four treatment groups?
Variation among groups
\[\large SS_A = n \sum_i (\bar{y}_i - \bar{y}.)^2\]
Variation within groups
\[\large SS_W = \sum_i \sum_j (y_{ij} - \bar{y}_i)^2\]
Note that we can also calculate the total variation in our observations6
\[\large SS_T = \sum_i \sum_j (y_{ij} - \bar{y}.)^2= SS_A + SS_W\]
Question: Is there a difference in growth among the four treatment groups?
The F-test is based on variances. Sums of squares quantify variation but they are not technically variances.
To covert the sums of squares to variances, divide by the degrees of freedom:7
Mean squares among
\[\large MS_A = \frac{SS_A}{a-1}\]
Mean squares within
\[\large MS_W = \frac{SS_W}{a(n-1)}\]
From the mean squares, we can finally calculate the F-statistic:
\[\large F = \frac{MS_A}{MS_W}\]
Note:
F is a ratio measuring the variance among groups to the variance within groups
If there is a large treatment effect, what happens to \(MS_A\)?
If there is little residual variation, what happens to \(MS_W\)?
Large values of \(F\) indicate treatment effects are large relative to residual variation, but can we conclude there is a treatment effect?
If the null hypothesis is true (\(\sigma^2_A = \sigma^2_B\)), the ratio of sample variances follows an F-distribution
Properties:
\(F > 0\)
\(F\)-distribution is not symmetrical
Shape of distribution depends on an ordered pair of degrees of freedom, \(df_A\) and \(df_B\)

Just like the t-distribution, we can use the F-distribution to calculate p-values and test the null hypothesis that the variances are equal
Using the F-statistic to test whether there is a treatment effect is commonly referred to as Analysis of Variance (ANOVA)
Especially in experimental settings, ANOVA is very commonly used
When treated as an ANOVA, the model is usually presented in a table providing the degrees of freedom, sums of squares, mean squares, F-statistic, and p-value
The linear model approach is increasingly common, especially in observational settings
From a practical standpoint, the linear model and ANOVA approaches provide exactly the same information, just presented in different ways (just like the linear model vs t-test we saw previously)
Suppose we are interested in the effect of elevation on the abundance of Canada Warblers

Elevation
|
|||
|---|---|---|---|
| Replicate | Low | Medium | High |
| 1 | 1 | 2 | 4 |
| 2 | 3 | 0 | 7 |
| 3 | 0 | 4 | 5 |
| 4 | 2 | 3 | 5 |
Call:
lm(formula = Count ~ Elevation, data = warblerdata)
Residuals:
Min 1Q Median 3Q Max
-2.250 -0.688 -0.250 0.938 1.750
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.250 0.717 7.32 4.5e-05 ***
ElevationLow -3.750 1.014 -3.70 0.0049 **
ElevationMedium -3.000 1.014 -2.96 0.0160 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 1.43 on 9 degrees of freedom
Multiple R-squared: 0.63, Adjusted R-squared: 0.548
F-statistic: 7.66 on 2 and 9 DF, p-value: 0.0114
Df Sum Sq Mean Sq F value Pr(>F)
Elevation 2 31.5 15.75 7.66 0.011 *
Residuals 9 18.5 2.06
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Note:
The row named for the treatment variable (e.g., Elevation) is the among group variation
The row named Residuals is the within group variation
The degrees of freedom, F-statistic, and p-value are exactly the same as returned by lm()8
lmIn case you want more detail about the relationship between lm and aov output:
lm() function also returns residuals (e.g., \(y_i - E[y_i]\)) 1 2 3 4 5 6 7 8 9 10 11 12
-0.50 1.50 -1.50 0.50 -0.25 -2.25 1.75 0.75 -1.25 1.75 -0.25 -0.25
lmIn case you want more detail about the relationship between lm and aov output:
What about among group variation?
1 2 3 4 5 6 7 8 9 10 11 12
1.50 1.50 1.50 1.50 2.25 2.25 2.25 2.25 5.25 5.25 5.25 5.25
lmIn case you want more detail about the relationship between lm and aov output:
F-statistic and p-value9
Models with categorical variables can be fit using either lm() or aov()
The test traditionally known as ANOVA is just a linear model, slightly modified from the one used analyze binary predictor variables
R codes categorical predictors using dummy variables and lm coefficients correspond to the difference between the reference level and the other treatment levels
lm() or aov() provide the exact same information, just presented in different ways
ANOVA output is commonly used for manipulative experiments, linear model output for observational studies10
But how do we tell which groups differ from each other (aside from just the reference level)…
Next time: Multiple comparisons
Reading: Fieberg chp. 3.9 and 3.12
We will discuss this issue in detail in the next lecture
Sometimes referred to as the design matrix
For a model with \(b\) parameters and \(n\) observations, the model matrix \(X\) is a \(n \times b\) matrix. The parameters themselves form a \(b \times 1\) matrix denoted \(B\). Using matrix multiplication, \(X * B\) returns a \(n \times 1\) matrix with each value corresponding to \(E[y_i]\) for each observation
Notice that this is just an algebraic manipulation of the linear model. We’ve re-written the equation but it returns the same value as the previous equation we used
Note what this equation is doing: We take the difference between each group mean and the grand mean and square them so there are no negative values (essentially, the length of the orange bars on the previous graph regardless of direction). Then we add them together to get the total length of those lines. Multiplying by \(n\) is basically a way to account for how many observations this variation in based on. The equation here assumes a balanced design (same \(n\) for each group), though \(SS_A\) can be calculated for unbalanced designs
And, importantly, that \(SS_A\) and \(SS_W\) partition all of the variation into two categories: variation due to differences among treatments (\(SS_A\)) and variation not due to treatments \(SS_W\)
Degrees of freedom are measures of how much information we have, specially how many independent pieces of information we have to estimate parameters. Generally, degrees of freedom are equal to the number of observations minus the number of parameters we need to estimate from those observations. Another way to view degrees of freedom. Imagine you know the mean of 10 observations is \(\bar{y}\). Based on this information, you have no way of knowing the value of the fist 9 observations - they are free to be any value. But once you know the value of observations 1-9, the value of observation 10 is also known - it is not free to vary. So to estimate the mean from \(n=10\), you have \(df = 9\)
Which makes sense, because they are from the exact same model, just presented in two different ways
The pf() function calculates the area under an F-distribution with \(df1\) and \(df2\) degrees of freedom. lower.tail = FALSE returns only the area under the upper tail, which is what we want since we testing whether \(MS_A\) is greater than \(MS_W\)
This is just convention. Since they are the same model, you could use either one regardless of the type of experiment