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)
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)
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 |
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 |
The number of groups (treatments) is a=4
The number of observations within each group (replicates) is n=3
yij denotes the jth observation from the ith group
the physical unit that receives a particular treatment
the physical unit that receives a particular treatment
the physical unit on which measurements are taken
the physical unit that receives a particular treatment
the physical unit on which measurements are taken
These are not always the same!
the physical unit that receives a particular treatment
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?
Question: Is there a difference in growth among the four treatment groups?
H0:μA=μB=μC=μD
Ha: At least one inequality
H0:μA=μB=μC=μD
Ha: At least one inequality
H0:μA=μB=μC=μD
Ha: At least one inequality
We could do this using 6 t-tests
H0:μA=μB=μC=μD
Ha: At least one inequality
We could do this using 6 t-tests
But this would alter the overall (experiment-wise) α level because each individual test has a chance (usually α=0.05) of incorrectly rejecting a true null hypothesis, and this is multiplied when multiple tests are used
yij=β0+βixij+ϵij
ϵij∼normal(0,σ)
yij=β0+βixij+ϵij
ϵij∼normal(0,σ)
yij=β0+βixij+ϵij
ϵij∼normal(0,σ)
What's tricky is how we code xij when it is more than just 0 or 1
Fortunately, what we learned about how R
codes dummy variables will help
data(loblollydata)loblollydata[c(1,4,7,10),]
## Treatment Replicate Height## 1 A 1 11## 4 B 1 9## 7 C 1 7## 10 D 1 5
fit1 <- lm(Height ~ Treatment, data = loblollydata)model.matrix(fit1)[c(1,4,7,10),]
## (Intercept) TreatmentB TreatmentC TreatmentD## 1 1 0 0 0## 4 1 1 0 0## 7 1 0 1 0## 10 1 0 0 1
model.matrix(fit1)[c(1,4,7,10),]
## (Intercept) TreatmentB TreatmentC TreatmentD## 1 1 0 0 0## 4 1 1 0 0## 7 1 0 1 0## 10 1 0 0 1
Remember that the model matrix has one column for every parameter in the model
model.matrix(fit1)[c(1,4,7,10),]
## (Intercept) TreatmentB TreatmentC TreatmentD## 1 1 0 0 0## 4 1 1 0 0## 7 1 0 1 0## 10 1 0 0 1
Remember that the model matrix has one column for every parameter in the model
E[yij]=β0+β1I(B)ij+β2I(C)ij+β3I(D)ij where I(B/C/D)ij are dummy variables (0/1 depending on treatment)
model.matrix(fit1)[c(1,4,7,10),]
## (Intercept) TreatmentB TreatmentC TreatmentD## 1 1 0 0 0## 4 1 1 0 0## 7 1 0 1 0## 10 1 0 0 1
Remember that the model matrix has one column for every parameter in the model
E[yij]=β0+β1I(B)ij+β2I(C)ij+β3I(D)ij where I(B/C/D)ij are dummy variables (0/1 depending on treatment)
broom::tidy(fit1)
## # 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 0.816 -7.35 0.0000801
broom::tidy(fit1)
## # 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 0.816 -7.35 0.0000801
broom::tidy(fit1)
## # 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 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
broom::tidy(fit1)
## # 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 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 μA=μB=μC=μD
summary(fit1)
## ## 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
The F-statistic tests whether all regression coefficients (other than the intercept) are all 0
H0: All regression coefficients are 0
HA: At least one regression coefficient is not 0
The F-statistic tests whether all regression coefficients (other than the intercept) are all 0
H0: All regression coefficients are 0
HA: At least one regression coefficient is not 0
F-statistics are the ratio of sample variances
F=s2As2B
Null hypothesis is that population variances are equal ( σ2A=σ2B )
Always positive (variances cannot be negative)
Usually > 1 (by putting larger variance in the numerator)
The F-statistic tests whether all regression coefficients (other than the intercept) are all 0
H0: All regression coefficients are 0
HA: At least one regression coefficient is not 0
F-statistics are the ratio of sample variances
F=s2As2B
Null hypothesis is that population variances are equal ( σ2A=σ2B )
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?
ˉy.=∑i∑jyija×n
ˉy.=∑i∑jyija×n
ˉyi=∑jyijn
ˉy.=∑i∑jyija×n
ˉyi=∑jyijn
We can now decompose the observations as
yij=ˉy.+(ˉyi−ˉy.)+(yij−ˉyi)
yij=ˉy.+(ˉyi−ˉy.)+(yij−ˉyi)
yij=ˉy.+(ˉyi−ˉy.)+(yij−ˉyi)
yij=μ+αi+ϵij
yij=ˉy.+(ˉyi−ˉy.)+(yij−ˉyi)
yij=μ+αi+ϵij
ϵij∼normal(0,σ2)
yij=μ+αi+ϵij
ϵij∼normal(0,σ2)
yij=μ+αi+ϵij
ϵij∼normal(0,σ2)
μ is the grand mean of the population, estimated by ˉy.
αi is the effect of treatment i, estimated by ˉyi−ˉy.
yij=μ+αi+ϵij
ϵij∼normal(0,σ2)
μ is the grand mean of the population, estimated by ˉy.
αi is the effect of treatment i, estimated by ˉyi−ˉy.
It is the deviation of the group mean from the grand mean
If all αi=0, there is no treatment effect
Thus, we can re-write the null hypothesis H0:α1=α2=...=αa=0
yij=μ+αi+ϵij
ϵij∼normal(0,σ2)
μ is the grand mean of the population, estimated by ˉy.
αi is the effect of treatment i, estimated by ˉyi−ˉy.
It is the deviation of the group mean from the grand mean
If all αi=0, there is no treatment effect
Thus, we can re-write the null hypothesis H0:α1=α2=...=αa=0
ϵij is the residual error, estimated by yij−ˉyi
SSA=n∑i(ˉyi−ˉy.)2
Question: Is there a difference in growth among the four treatment groups?
SSA=n∑i(ˉyi−ˉy.)2
SSW=∑i∑j(yij−ˉyi)2
Question: Is there a difference in growth among the four treatment groups?
SSA=n∑i(ˉyi−ˉy.)2
SSW=∑i∑j(yij−ˉyi)2
SST=SSA+SSW=∑i∑j(yij−ˉy.)2
Question: Is there a difference in growth among the four treatment groups?
MSA=SSAa−1
MSA=SSAa−1
MSW=SSWa(n−1)
F=MSAMSW
F=MSAMSW
F=MSAMSW
F=MSAMSW
F is a ratio measuring the variance among groups to the variance within groups
If there is a large treatment effect, what happens to MSA?
F=MSAMSW
F is a ratio measuring the variance among groups to the variance within groups
If there is a large treatment effect, what happens to MSA?
If there is little residual variation, what happens to MSW?
F=MSAMSW
F is a ratio measuring the variance among groups to the variance within groups
If there is a large treatment effect, what happens to MSA?
If there is little residual variation, what happens to MSW?
Large values of F indicate treatment effects are large relative to residual variation, but can we conclude there is a treatment effect?
F>0
F-distribution is not symmetrical
Shape of distribution depends on an ordered pair of degrees of freedom, dfA and dfB
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
Especially in experimental settings, ANOVA is very commonly used
The linear model approach is increasingly common, especially in observational settings
Especially in experimental settings, ANOVA is very commonly used
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)
Elevation |
|||
---|---|---|---|
Replicate | Low | Medium | High |
1 | 1 | 2 | 4 |
2 | 3 | 0 | 7 |
3 | 0 | 4 | 5 |
4 | 2 | 3 | 5 |
Image courtesy of William H. Majoros via Wikicommons
data(warblerdata)fit.lm <- lm(Count ~ Elevation, data = warblerdata)summary(fit.lm)
## ## 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
data(warblerdata)fit.aov <- aov(Count ~ Elevation, data = warblerdata)summary(fit.aov)
## 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
lm
In case you want more detail about the relationship between lm
and aov
output:
lm()
function also returns residuals (e.g., yi−E[yi])fit.lm$residual
## 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
sum(fit.lm$residuals^2)
## [1] 18.5
sum(fit.lm$residuals^2)/9
## [1] 2.056
lm
In case you want more detail about the relationship between lm
and aov
output:
What about among group variation?
fit.lm$fitted.values
## 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
sum((fit.lm$fitted.values - mean(fit.lm$fitted.values))^2)
## [1] 31.5
sum((fit.lm$fitted.values - mean(fit.lm$fitted.values))^2)/2
## [1] 15.75
lm
In case you want more detail about the relationship between lm
and aov
output:
F-statistic and p-value
MSa <- sum((fit.lm$fitted.values - mean(fit.lm$fitted.values))^2)/2MSe <- sum(fit.lm$residuals^2)/9(F <- MSa/MSe)
## [1] 7.662
(p <- pf(F, 2, 9, lower.tail = FALSE))
## [1] 0.0114
1) Models with categorical variables can be fit using either lm()
or aov()
1) Models with categorical variables can be fit using either lm()
or aov()
2) The test traditionally known as ANOVA is just an extension of the linear model used analyze a binary predictor variable
1) Models with categorical variables can be fit using either lm()
or aov()
2) The test traditionally known as ANOVA is just an extension of the linear model used analyze a binary predictor variable
3) R
codes categorical predictors using dummy variables and lm
coefficients correspond to the difference between the reference level and the other treatment levels
1) Models with categorical variables can be fit using either lm()
or aov()
2) The test traditionally known as ANOVA is just an extension of the linear model used analyze a binary predictor variable
3) R
codes categorical predictors using dummy variables and lm
coefficients correspond to the difference between the reference level and the other treatment levels
4) lm()
or aov()
provide the exact same information, just presented in different ways
1) Models with categorical variables can be fit using either lm()
or aov()
2) The test traditionally known as ANOVA is just an extension of the linear model used analyze a binary predictor variable
3) R
codes categorical predictors using dummy variables and lm
coefficients correspond to the difference between the reference level and the other treatment levels
4) lm()
or aov()
provide the exact same information, just presented in different ways
5) ANOVA output commonly used for manipulative experiments, linear model output for observational studies
1) Models with categorical variables can be fit using either lm()
or aov()
2) The test traditionally known as ANOVA is just an extension of the linear model used analyze a binary predictor variable
3) R
codes categorical predictors using dummy variables and lm
coefficients correspond to the difference between the reference level and the other treatment levels
4) lm()
or aov()
provide the exact same information, just presented in different ways
5) ANOVA output commonly used for manipulative experiments, linear model output for observational studies
6) But how do we tell which groups differ from each other (aside from just the reference level)...
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |