Lab 8 – Nested ANOVA
October 8 & 9, 2017
FANR 6750
Richard Chandler and Bob Cooper
Outline
1 Overview
2 Using aov
3 Using lme
Overview Using aov Using lme 2 / 16
Scenario
We subsample each experimental unit
Overview Using aov Using lme 3 / 16
Scenario
We subsample each experimental unit
For example
• We count larvae at multiple subplots within a plot
• We weigh multiple chicks in a brood
Overview Using aov Using lme 3 / 16
Scenario
We subsample each experimental unit
For example
• We count larvae at multiple subplots within a plot
• We weigh multiple chicks in a brood
We’re interested in treatment effects at the experimental
(whole) unit level, not the subunit level
Overview Using aov Using lme 3 / 16
The additive model
yijk = µ + αi + βij + εijk
Overview Using aov Using lme 4 / 16
The additive model
yijk = µ + αi + βij + εijk
Because we want our inferences to apply to all experimental
units, not just the ones in our sample, βij is random.
Overview Using aov Using lme 4 / 16
The additive model
yijk = µ + αi + βij + εijk
Because we want our inferences to apply to all experimental
units, not just the ones in our sample, βij is random.
Specifically:
βij ∼ Normal(0, σ2
B)
Overview Using aov Using lme 4 / 16
The additive model
yijk = µ + αi + βij + εijk
Because we want our inferences to apply to all experimental
units, not just the ones in our sample, βij is random.
Specifically:
βij ∼ Normal(0, σ2
B)
And as always,
εijk ∼ Normal(0, σ2
)
Overview Using aov Using lme 4 / 16
Hypotheses
Treatment effects
H0 : α1 = · · · = αa = 0
Ha : at least one inequality
Overview Using aov Using lme 5 / 16
Hypotheses
Treatment effects
H0 : α1 = · · · = αa = 0
Ha : at least one inequality
Random variation among experimental units
H0 : σ2
B = 0
Ha : σ2
B > 0
Overview Using aov Using lme 5 / 16
Example data
Import data
gypsyData <- read.csv("gypsyData.csv")
str(gypsyData)
## 'data.frame': 36 obs. of 3 variables:
## $ larvae : num 16 16 15.8 14.2 13.9 14.2 13.5 13.4 14 13.
## $ Treatment: Factor w/ 3 levels "Bt","Control",..: 1 1 1 1 1
## $ Plot : int 1 1 1 1 2 2 2 2 3 3 ...
Overview Using aov Using lme 6 / 16
Example data
Import data
gypsyData <- read.csv("gypsyData.csv")
str(gypsyData)
## 'data.frame': 36 obs. of 3 variables:
## $ larvae : num 16 16 15.8 14.2 13.9 14.2 13.5 13.4 14 13.
## $ Treatment: Factor w/ 3 levels "Bt","Control",..: 1 1 1 1 1
## $ Plot : int 1 1 1 1 2 2 2 2 3 3 ...
Convert Plot to a factor and then cross-tabulate
gypsyData$Plot <- factor(gypsyData$Plot)
table(gypsyData$Treatment, gypsyData$Plot)
##
## 1 2 3 4 5 6 7 8 9
## Bt 4 4 4 0 0 0 0 0 0
## Control 0 0 0 4 4 4 0 0 0
## Dimilin 0 0 0 0 0 0 4 4 4
Overview Using aov Using lme 6 / 16
Incorrect analysis
aov.wrong <- aov(larvae ~ Treatment + Plot,
data=gypsyData)
Overview Using aov Using lme 7 / 16
Incorrect analysis
aov.wrong <- aov(larvae ~ Treatment + Plot,
data=gypsyData)
summary(aov.wrong)
## Df Sum Sq Mean Sq F value Pr(>F)
## Treatment 2 215.39 107.69 208.89 <2e-16 ***
## Plot 6 11.17 1.86 3.61 0.0093 **
## Residuals 27 13.92 0.52
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' '
Overview Using aov Using lme 7 / 16
Incorrect analysis
aov.wrong <- aov(larvae ~ Treatment + Plot,
data=gypsyData)
summary(aov.wrong)
## Df Sum Sq Mean Sq F value Pr(>F)
## Treatment 2 215.39 107.69 208.89 <2e-16 ***
## Plot 6 11.17 1.86 3.61 0.0093 **
## Residuals 27 13.92 0.52
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' '
The denominator degrees-of-freedom are wrong
Overview Using aov Using lme 7 / 16
Correct analysis
aov.correct <- aov(larvae ~ Treatment + Error(Plot),
data=gypsyData)
Overview Using aov Using lme 8 / 16
Correct analysis
aov.correct <- aov(larvae ~ Treatment + Error(Plot),
data=gypsyData)
summary(aov.correct)
##
## Error: Plot
## Df Sum Sq Mean Sq F value Pr(>F)
## Treatment 2 215.39 107.69 57.87 0.00012 ***
## Residuals 6 11.17 1.86
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' '
##
## Error: Within
## Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 27 13.92 0.5156
Overview Using aov Using lme 8 / 16
What happens if we analyze plot-level means?
The aggregate function is similar to tapply but it works on
entire data.frames. Here we get averages for each whole plot.
plotData <- aggregate(formula=larvae ~ Treatment + Plot,
data=gypsyData, FUN=mean)
Overview Using aov Using lme 9 / 16
What happens if we analyze plot-level means?
The aggregate function is similar to tapply but it works on
entire data.frames. Here we get averages for each whole plot.
plotData <- aggregate(formula=larvae ~ Treatment + Plot,
data=gypsyData, FUN=mean)
plotData
## Treatment Plot larvae
## 1 Bt 1 15.50
## 2 Bt 2 13.75
## 3 Bt 3 14.00
## 4 Control 4 18.25
## 5 Control 5 18.75
## 6 Control 6 19.25
## 7 Dimilin 7 12.50
## 8 Dimilin 8 13.50
## 9 Dimilin 9 13.00
Overview Using aov Using lme 9 / 16
F and p values are the same as before
aov.plot <- aov(larvae ~ Treatment, data=plotData)
summary(aov.plot)
## Df Sum Sq Mean Sq F value Pr(>F)
## Treatment 2 53.85 26.924 57.87 0.00012 ***
## Residuals 6 2.79 0.465
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Overview Using aov Using lme 10 / 16
F and p values are the same as before
aov.plot <- aov(larvae ~ Treatment, data=plotData)
summary(aov.plot)
## Df Sum Sq Mean Sq F value Pr(>F)
## Treatment 2 53.85 26.924 57.87 0.00012 ***
## Residuals 6 2.79 0.465
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(aov.correct)
##
## Error: Plot
## Df Sum Sq Mean Sq F value Pr(>F)
## Treatment 2 215.39 107.69 57.87 0.00012 ***
## Residuals 6 11.17 1.86
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Error: Within
## Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 27 13.92 0.5156
Overview Using aov Using lme 10 / 16
Issues
When using using aov with Error term:
• You can’t use TukeyHSD
• You don’t get a direct estimate of σ2
B
• Doesn’t handle unbalanced designs well
• But, you can use model.tables and se.contrast
Overview Using aov Using lme 11 / 16
Issues
When using using aov with Error term:
• You can’t use TukeyHSD
• You don’t get a direct estimate of σ2
B
• Doesn’t handle unbalanced designs well
• But, you can use model.tables and se.contrast
An alternative is to use lme function in nlme package
Overview Using aov Using lme 11 / 16
Issues
When using using aov with Error term:
• You can’t use TukeyHSD
• You don’t get a direct estimate of σ2
B
• Doesn’t handle unbalanced designs well
• But, you can use model.tables and se.contrast
An alternative is to use lme function in nlme package
• Possible to get direct estimates of σ2
B and other variance
parameters
• Handles very complex models and unbalanced designs
• Possible to do multiple comparisons and contrasts using the
the glht function in the multcomp package.
Overview Using aov Using lme 11 / 16
Issues
When using using aov with Error term:
• You can’t use TukeyHSD
• You don’t get a direct estimate of σ2
B
• Doesn’t handle unbalanced designs well
• But, you can use model.tables and se.contrast
An alternative is to use lme function in nlme package
• Possible to get direct estimates of σ2
B and other variance
parameters
• Handles very complex models and unbalanced designs
• Possible to do multiple comparisons and contrasts using the
the glht function in the multcomp package.
• But. . .
• Only works if there random effects
• ANOVA tables aren’t as complete as aov
Overview Using aov Using lme 11 / 16
Using the lme function
library(nlme)
library(multcomp)
lme1 <- lme(larvae ~ Treatment, random=~1|Plot,
data=gypsyData)
Overview Using aov Using lme 12 / 16
Using the lme function
library(nlme)
library(multcomp)
lme1 <- lme(larvae ~ Treatment, random=~1|Plot,
data=gypsyData)
anova(lme1, Terms="Treatment")
## F-test for: Treatment
## numDF denDF F-value p-value
## 1 2 6 57.86567 1e-04
Overview Using aov Using lme 12 / 16
Variance parameter estimates
The first row shows the estimates of σ2
B and σB. The second row shows
the estimates of σ2
and σ
VarCorr(lme1)
## Plot = pdLogChol(1)
## Variance StdDev
## (Intercept) 0.3363889 0.5799904
## Residual 0.5155556 0.7180220
Overview Using aov Using lme 13 / 16
Variance parameter estimates
The first row shows the estimates of σ2
B and σB. The second row shows
the estimates of σ2
and σ
VarCorr(lme1)
## Plot = pdLogChol(1)
## Variance StdDev
## (Intercept) 0.3363889 0.5799904
## Residual 0.5155556 0.7180220
There is more random variation within whole units than among
whole units (after accounting for treatment effects)
Overview Using aov Using lme 13 / 16
Extract the plot-level random effects
These are the βij’s
round(ranef(lme1), 2)
## (Intercept)
## 1 0.78
## 2 -0.48
## 3 -0.30
## 4 -0.36
## 5 0.00
## 6 0.36
## 7 -0.36
## 8 0.36
## 9 0.00
Overview Using aov Using lme 14 / 16
Multiple comparisons
tuk <- glht(lme1, linfct=mcp(Treatment="Tukey"))
Overview Using aov Using lme 15 / 16
Multiple comparisons
tuk <- glht(lme1, linfct=mcp(Treatment="Tukey"))
summary(tuk)
##
## Simultaneous Tests for General Linear Hypotheses
##
## Multiple Comparisons of Means: Tukey Contrasts
##
##
## Fit: lme.formula(fixed = larvae ~ Treatment, data = gypsyData, random = ~1 |
## Plot)
##
## Linear Hypotheses:
## Estimate Std. Error z value Pr(>|z|)
## Control - Bt == 0 4.3333 0.5569 7.781 <0.001 ***
## Dimilin - Bt == 0 -1.4167 0.5569 -2.544 0.0295 *
## Dimilin - Control == 0 -5.7500 0.5569 -10.324 <0.001 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## (Adjusted p values reported -- single-step method)
Overview Using aov Using lme 15 / 16
Assignment
To determine if salinity affects adult fish reproductive performance, a
researcher places one pregnant female in a tank with one of three salinity
levels: low, medium, and high, or a control tank. A week after birth, two
offspring (fry) are measured.
Run a nested ANOVA using aov and lme on the fishData.csv
dataset. Answer the following questions:
(1) What are the null and alternative hypotheses?
(2) Does salinity affect fry growth?
(3) If so, which salinity levels differ?
(4) Is there more random variation among or within experimental units?
Upload your self-contained R script to ELC at least one day before
your next lab
Overview Using aov Using lme 16 / 16

More Related Content

PDF
Model Selection and Multi-model Inference
PDF
Repeated measures analysis in R
PDF
Split-plot Designs
PDF
Factorial designs
PDF
Flights Landing Overrun Project
PPTX
Issta13 workshop on debugging
PPT
PPT
Model Selection and Multi-model Inference
Repeated measures analysis in R
Split-plot Designs
Factorial designs
Flights Landing Overrun Project
Issta13 workshop on debugging

Similar to Nested Designs (20)

PDF
Scilab as a calculator
DOCX
Instrumentation and measurements
PPTX
“Python” or “CPython” is written in C/C+
PDF
PVS-Studio team is about to produce a technical breakthrough, but for now let...
PDF
Numerical analysis using Scilab: Error analysis and propagation
PPSX
White Box testing by Pankaj Thakur, NITTTR Chandigarh
PPTX
RxJava 2 Reactive extensions for the JVM
PPT
Verilog Lecture2 thhts
PPT
Verilog Lecture3 hust 2014
PPTX
Task_Verilohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhg.pptx
PDF
MATLAB Basics-Part1
PDF
Numerical analysis using Scilab: Numerical stability and conditioning
DOCX
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
PDF
Perl_Part2
PDF
Chapter 18,19
PDF
Blocking lab
PPTX
Introduction to flowchart
PDF
T11_effects_of_peripheral_stim
PDF
T11_effects_of_peripheral_stim
Scilab as a calculator
Instrumentation and measurements
“Python” or “CPython” is written in C/C+
PVS-Studio team is about to produce a technical breakthrough, but for now let...
Numerical analysis using Scilab: Error analysis and propagation
White Box testing by Pankaj Thakur, NITTTR Chandigarh
RxJava 2 Reactive extensions for the JVM
Verilog Lecture2 thhts
Verilog Lecture3 hust 2014
Task_Verilohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhg.pptx
MATLAB Basics-Part1
Numerical analysis using Scilab: Numerical stability and conditioning
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
Perl_Part2
Chapter 18,19
Blocking lab
Introduction to flowchart
T11_effects_of_peripheral_stim
T11_effects_of_peripheral_stim
Ad

More from richardchandler (12)

PDF
Introduction to Generalized Linear Models
PDF
Introduction to statistical modeling in R
PDF
ANCOVA in R
PDF
Assumptions of ANOVA
PDF
Lab on contrasts, estimation, and power
PDF
One-way ANOVA
PDF
t-tests in R - Lab slides for UGA course FANR 6750
PDF
Introduction to R - Lab slides for UGA course FANR 6750
PDF
Hierarchichal species distributions model and Maxent
PDF
Slides from ESA 2015
PDF
The role of spatial models in applied ecological research
PDF
2014 ISEC slides
Introduction to Generalized Linear Models
Introduction to statistical modeling in R
ANCOVA in R
Assumptions of ANOVA
Lab on contrasts, estimation, and power
One-way ANOVA
t-tests in R - Lab slides for UGA course FANR 6750
Introduction to R - Lab slides for UGA course FANR 6750
Hierarchichal species distributions model and Maxent
Slides from ESA 2015
The role of spatial models in applied ecological research
2014 ISEC slides
Ad

Recently uploaded (20)

PPT
Cell Structure Description and Functions
PDF
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
PDF
Packaging materials of fruits and vegetables
PPTX
AP CHEM 1.2 Mass spectroscopy of elements
PDF
Communicating Health Policies to Diverse Populations (www.kiu.ac.ug)
PPTX
ELISA(Enzyme linked immunosorbent assay)
PPTX
GREEN FIELDS SCHOOL PPT ON HOLIDAY HOMEWORK
PDF
Social preventive and pharmacy. Pdf
PDF
CuO Nps photocatalysts 15156456551564161
PPTX
congenital heart diseases of burao university.pptx
PDF
Integrative Oncology: Merging Conventional and Alternative Approaches (www.k...
PPTX
Introduction to Immunology (Unit-1).pptx
PPTX
diabetes and its complications nephropathy neuropathy
PPTX
Platelet disorders - thrombocytopenia.pptx
PPTX
Understanding the Circulatory System……..
PPTX
LIPID & AMINO ACID METABOLISM UNIT-III, B PHARM II SEMESTER
PPTX
bone as a tissue presentation micky.pptx
PPTX
Presentation1 INTRODUCTION TO ENZYMES.pptx
PDF
Science Form five needed shit SCIENEce so
PDF
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
Cell Structure Description and Functions
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...
Packaging materials of fruits and vegetables
AP CHEM 1.2 Mass spectroscopy of elements
Communicating Health Policies to Diverse Populations (www.kiu.ac.ug)
ELISA(Enzyme linked immunosorbent assay)
GREEN FIELDS SCHOOL PPT ON HOLIDAY HOMEWORK
Social preventive and pharmacy. Pdf
CuO Nps photocatalysts 15156456551564161
congenital heart diseases of burao university.pptx
Integrative Oncology: Merging Conventional and Alternative Approaches (www.k...
Introduction to Immunology (Unit-1).pptx
diabetes and its complications nephropathy neuropathy
Platelet disorders - thrombocytopenia.pptx
Understanding the Circulatory System……..
LIPID & AMINO ACID METABOLISM UNIT-III, B PHARM II SEMESTER
bone as a tissue presentation micky.pptx
Presentation1 INTRODUCTION TO ENZYMES.pptx
Science Form five needed shit SCIENEce so
Worlds Next Door: A Candidate Giant Planet Imaged in the Habitable Zone of ↵ ...

Nested Designs

  • 1. Lab 8 – Nested ANOVA October 8 & 9, 2017 FANR 6750 Richard Chandler and Bob Cooper
  • 2. Outline 1 Overview 2 Using aov 3 Using lme Overview Using aov Using lme 2 / 16
  • 3. Scenario We subsample each experimental unit Overview Using aov Using lme 3 / 16
  • 4. Scenario We subsample each experimental unit For example • We count larvae at multiple subplots within a plot • We weigh multiple chicks in a brood Overview Using aov Using lme 3 / 16
  • 5. Scenario We subsample each experimental unit For example • We count larvae at multiple subplots within a plot • We weigh multiple chicks in a brood We’re interested in treatment effects at the experimental (whole) unit level, not the subunit level Overview Using aov Using lme 3 / 16
  • 6. The additive model yijk = µ + αi + βij + εijk Overview Using aov Using lme 4 / 16
  • 7. The additive model yijk = µ + αi + βij + εijk Because we want our inferences to apply to all experimental units, not just the ones in our sample, βij is random. Overview Using aov Using lme 4 / 16
  • 8. The additive model yijk = µ + αi + βij + εijk Because we want our inferences to apply to all experimental units, not just the ones in our sample, βij is random. Specifically: βij ∼ Normal(0, σ2 B) Overview Using aov Using lme 4 / 16
  • 9. The additive model yijk = µ + αi + βij + εijk Because we want our inferences to apply to all experimental units, not just the ones in our sample, βij is random. Specifically: βij ∼ Normal(0, σ2 B) And as always, εijk ∼ Normal(0, σ2 ) Overview Using aov Using lme 4 / 16
  • 10. Hypotheses Treatment effects H0 : α1 = · · · = αa = 0 Ha : at least one inequality Overview Using aov Using lme 5 / 16
  • 11. Hypotheses Treatment effects H0 : α1 = · · · = αa = 0 Ha : at least one inequality Random variation among experimental units H0 : σ2 B = 0 Ha : σ2 B > 0 Overview Using aov Using lme 5 / 16
  • 12. Example data Import data gypsyData <- read.csv("gypsyData.csv") str(gypsyData) ## 'data.frame': 36 obs. of 3 variables: ## $ larvae : num 16 16 15.8 14.2 13.9 14.2 13.5 13.4 14 13. ## $ Treatment: Factor w/ 3 levels "Bt","Control",..: 1 1 1 1 1 ## $ Plot : int 1 1 1 1 2 2 2 2 3 3 ... Overview Using aov Using lme 6 / 16
  • 13. Example data Import data gypsyData <- read.csv("gypsyData.csv") str(gypsyData) ## 'data.frame': 36 obs. of 3 variables: ## $ larvae : num 16 16 15.8 14.2 13.9 14.2 13.5 13.4 14 13. ## $ Treatment: Factor w/ 3 levels "Bt","Control",..: 1 1 1 1 1 ## $ Plot : int 1 1 1 1 2 2 2 2 3 3 ... Convert Plot to a factor and then cross-tabulate gypsyData$Plot <- factor(gypsyData$Plot) table(gypsyData$Treatment, gypsyData$Plot) ## ## 1 2 3 4 5 6 7 8 9 ## Bt 4 4 4 0 0 0 0 0 0 ## Control 0 0 0 4 4 4 0 0 0 ## Dimilin 0 0 0 0 0 0 4 4 4 Overview Using aov Using lme 6 / 16
  • 14. Incorrect analysis aov.wrong <- aov(larvae ~ Treatment + Plot, data=gypsyData) Overview Using aov Using lme 7 / 16
  • 15. Incorrect analysis aov.wrong <- aov(larvae ~ Treatment + Plot, data=gypsyData) summary(aov.wrong) ## Df Sum Sq Mean Sq F value Pr(>F) ## Treatment 2 215.39 107.69 208.89 <2e-16 *** ## Plot 6 11.17 1.86 3.61 0.0093 ** ## Residuals 27 13.92 0.52 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' Overview Using aov Using lme 7 / 16
  • 16. Incorrect analysis aov.wrong <- aov(larvae ~ Treatment + Plot, data=gypsyData) summary(aov.wrong) ## Df Sum Sq Mean Sq F value Pr(>F) ## Treatment 2 215.39 107.69 208.89 <2e-16 *** ## Plot 6 11.17 1.86 3.61 0.0093 ** ## Residuals 27 13.92 0.52 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' The denominator degrees-of-freedom are wrong Overview Using aov Using lme 7 / 16
  • 17. Correct analysis aov.correct <- aov(larvae ~ Treatment + Error(Plot), data=gypsyData) Overview Using aov Using lme 8 / 16
  • 18. Correct analysis aov.correct <- aov(larvae ~ Treatment + Error(Plot), data=gypsyData) summary(aov.correct) ## ## Error: Plot ## Df Sum Sq Mean Sq F value Pr(>F) ## Treatment 2 215.39 107.69 57.87 0.00012 *** ## Residuals 6 11.17 1.86 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' ## ## Error: Within ## Df Sum Sq Mean Sq F value Pr(>F) ## Residuals 27 13.92 0.5156 Overview Using aov Using lme 8 / 16
  • 19. What happens if we analyze plot-level means? The aggregate function is similar to tapply but it works on entire data.frames. Here we get averages for each whole plot. plotData <- aggregate(formula=larvae ~ Treatment + Plot, data=gypsyData, FUN=mean) Overview Using aov Using lme 9 / 16
  • 20. What happens if we analyze plot-level means? The aggregate function is similar to tapply but it works on entire data.frames. Here we get averages for each whole plot. plotData <- aggregate(formula=larvae ~ Treatment + Plot, data=gypsyData, FUN=mean) plotData ## Treatment Plot larvae ## 1 Bt 1 15.50 ## 2 Bt 2 13.75 ## 3 Bt 3 14.00 ## 4 Control 4 18.25 ## 5 Control 5 18.75 ## 6 Control 6 19.25 ## 7 Dimilin 7 12.50 ## 8 Dimilin 8 13.50 ## 9 Dimilin 9 13.00 Overview Using aov Using lme 9 / 16
  • 21. F and p values are the same as before aov.plot <- aov(larvae ~ Treatment, data=plotData) summary(aov.plot) ## Df Sum Sq Mean Sq F value Pr(>F) ## Treatment 2 53.85 26.924 57.87 0.00012 *** ## Residuals 6 2.79 0.465 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Overview Using aov Using lme 10 / 16
  • 22. F and p values are the same as before aov.plot <- aov(larvae ~ Treatment, data=plotData) summary(aov.plot) ## Df Sum Sq Mean Sq F value Pr(>F) ## Treatment 2 53.85 26.924 57.87 0.00012 *** ## Residuals 6 2.79 0.465 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 summary(aov.correct) ## ## Error: Plot ## Df Sum Sq Mean Sq F value Pr(>F) ## Treatment 2 215.39 107.69 57.87 0.00012 *** ## Residuals 6 11.17 1.86 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Error: Within ## Df Sum Sq Mean Sq F value Pr(>F) ## Residuals 27 13.92 0.5156 Overview Using aov Using lme 10 / 16
  • 23. Issues When using using aov with Error term: • You can’t use TukeyHSD • You don’t get a direct estimate of σ2 B • Doesn’t handle unbalanced designs well • But, you can use model.tables and se.contrast Overview Using aov Using lme 11 / 16
  • 24. Issues When using using aov with Error term: • You can’t use TukeyHSD • You don’t get a direct estimate of σ2 B • Doesn’t handle unbalanced designs well • But, you can use model.tables and se.contrast An alternative is to use lme function in nlme package Overview Using aov Using lme 11 / 16
  • 25. Issues When using using aov with Error term: • You can’t use TukeyHSD • You don’t get a direct estimate of σ2 B • Doesn’t handle unbalanced designs well • But, you can use model.tables and se.contrast An alternative is to use lme function in nlme package • Possible to get direct estimates of σ2 B and other variance parameters • Handles very complex models and unbalanced designs • Possible to do multiple comparisons and contrasts using the the glht function in the multcomp package. Overview Using aov Using lme 11 / 16
  • 26. Issues When using using aov with Error term: • You can’t use TukeyHSD • You don’t get a direct estimate of σ2 B • Doesn’t handle unbalanced designs well • But, you can use model.tables and se.contrast An alternative is to use lme function in nlme package • Possible to get direct estimates of σ2 B and other variance parameters • Handles very complex models and unbalanced designs • Possible to do multiple comparisons and contrasts using the the glht function in the multcomp package. • But. . . • Only works if there random effects • ANOVA tables aren’t as complete as aov Overview Using aov Using lme 11 / 16
  • 27. Using the lme function library(nlme) library(multcomp) lme1 <- lme(larvae ~ Treatment, random=~1|Plot, data=gypsyData) Overview Using aov Using lme 12 / 16
  • 28. Using the lme function library(nlme) library(multcomp) lme1 <- lme(larvae ~ Treatment, random=~1|Plot, data=gypsyData) anova(lme1, Terms="Treatment") ## F-test for: Treatment ## numDF denDF F-value p-value ## 1 2 6 57.86567 1e-04 Overview Using aov Using lme 12 / 16
  • 29. Variance parameter estimates The first row shows the estimates of σ2 B and σB. The second row shows the estimates of σ2 and σ VarCorr(lme1) ## Plot = pdLogChol(1) ## Variance StdDev ## (Intercept) 0.3363889 0.5799904 ## Residual 0.5155556 0.7180220 Overview Using aov Using lme 13 / 16
  • 30. Variance parameter estimates The first row shows the estimates of σ2 B and σB. The second row shows the estimates of σ2 and σ VarCorr(lme1) ## Plot = pdLogChol(1) ## Variance StdDev ## (Intercept) 0.3363889 0.5799904 ## Residual 0.5155556 0.7180220 There is more random variation within whole units than among whole units (after accounting for treatment effects) Overview Using aov Using lme 13 / 16
  • 31. Extract the plot-level random effects These are the βij’s round(ranef(lme1), 2) ## (Intercept) ## 1 0.78 ## 2 -0.48 ## 3 -0.30 ## 4 -0.36 ## 5 0.00 ## 6 0.36 ## 7 -0.36 ## 8 0.36 ## 9 0.00 Overview Using aov Using lme 14 / 16
  • 32. Multiple comparisons tuk <- glht(lme1, linfct=mcp(Treatment="Tukey")) Overview Using aov Using lme 15 / 16
  • 33. Multiple comparisons tuk <- glht(lme1, linfct=mcp(Treatment="Tukey")) summary(tuk) ## ## Simultaneous Tests for General Linear Hypotheses ## ## Multiple Comparisons of Means: Tukey Contrasts ## ## ## Fit: lme.formula(fixed = larvae ~ Treatment, data = gypsyData, random = ~1 | ## Plot) ## ## Linear Hypotheses: ## Estimate Std. Error z value Pr(>|z|) ## Control - Bt == 0 4.3333 0.5569 7.781 <0.001 *** ## Dimilin - Bt == 0 -1.4167 0.5569 -2.544 0.0295 * ## Dimilin - Control == 0 -5.7500 0.5569 -10.324 <0.001 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## (Adjusted p values reported -- single-step method) Overview Using aov Using lme 15 / 16
  • 34. Assignment To determine if salinity affects adult fish reproductive performance, a researcher places one pregnant female in a tank with one of three salinity levels: low, medium, and high, or a control tank. A week after birth, two offspring (fry) are measured. Run a nested ANOVA using aov and lme on the fishData.csv dataset. Answer the following questions: (1) What are the null and alternative hypotheses? (2) Does salinity affect fry growth? (3) If so, which salinity levels differ? (4) Is there more random variation among or within experimental units? Upload your self-contained R script to ELC at least one day before your next lab Overview Using aov Using lme 16 / 16