anova() Function in R
The anova() function conducts an ANOVA test, which partitions the total variance observed in a dataset into different components attributed to different sources of variation. These sources can include factors, interactions between factors, and residual error.
Syntax:
anova(model)
model: This parameter specifies the model object to be analyzed, it could be linear model as well as general model not a problem at all
Implement anova() Function in R
Suppose we want to calculate the exam score scored by university student on the basis of number of hours they study.
#make this example reproducible
set.seed(1)
#create dataset
df <- data.frame(hours = runif(50, 5, 15), score=50)
df$score = df$score + df$hours^3/150 + df$hours*runif(50, 1, 2)
#fit full model
full <- lm(score ~ poly(hours,2), data=df)
#fit reduced model
reduced <- lm(score ~ hours, data=df)
#perform lack of fit test using anova()
anova(full, reduced)
Output:
Analysis of Variance Table
Model 1: score ~ poly(hours, 2)
Model 2: score ~ hours
Res.Df RSS Df Sum of Sq F Pr(>F)
1 47 368.48
2 48 451.22 -1 -82.744 10.554 0.002144 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
When to use aov() vs. anova() in R
In R Programming Language aov() stands for analysis of variance. It is used to analyze variance. Variance is a statistical technique to compare means among two or more groups. anova() function is used to perform analysis of variance calculation and hypothesis testing. Together both aov() and anova() are used to analyze variance tests in the R Programming Language.
Table of Content
- aov() Function in R
- Using aov() function for Analysis of Variance.
- anova() Function in R
- Implement anova() Function in R
- Key Differences between aov() and anova()
- When to Use aov() in R
- When to Use anova() in R
Contact Us