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

aov() Function in R

It is a tool in statistics and in R language which is used to perform analysis of variance. It fits a linear model with our data and computes the analysis of the variance. Mostly it is used to test differences in mean values of continuous dependent variables.

Syntax:

aov(formula, data, subset, na.action)
  • formula: This parameter specifies the model to be fitted
  • data: This parameter is an optional data frame containing the variables in the model
  • subset: An optional parameter which is used to denote the subset of the observation.
  • na.action: Use to handle the missing values. It determines what to do if missing values are encountered.

Using aov() function for Analysis of Variance.

Let’s take an example and try to understand how are we going to use aov() for the analysis of variance.

Let’s say we have 3 different types of exercise available and we want to check if these 3 different programs helps in loosing the weight of a person differently. We can do it using one anova testing. Let’s say we recruit 90 people to participate in an experiment in which we randomly assign 30 people to follow either program A, program B, or program C for one month.

R
#make this example reproducible
set.seed(0)

#create data frame
df <- data.frame(program = rep(c("A", "B", "C"), each=30),
                 weight_loss = c(runif(30, 0, 3),
                                 runif(30, 0, 5),
                                 runif(30, 1, 7)))

#fit one-way anova using aov()
fit <- aov(weight_loss ~ program, data=df)

#view results
summary(fit)

Output:

            Df Sum Sq Mean Sq F value   Pr(>F)    
program 2 98.93 49.46 30.83 7.55e-11 ***
Residuals 87 139.57 1.60
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Df: The model’s degrees of freedom.

  • Sum Sq: The sums of squares, which represent the variability that the model is able to account for.
  • Mean Sq: The variance explained by each component is represented by the mean squares.
  • F-value: It is the measure used to compare the mean squares both within and between groups.
  • Pr(>F): The F-statistics p-value, which denotes the factors’ statistical significance.
  • Residuals: Relative deviations from the group mean, are often known as residuals and their summary statistics.

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.

R
#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

Key Differences between aov() and anova()

Here’s a detailed difference between aov() and anova() with respect to different parameters.

Feature

aov()

anova()

Purpose

The aov() functionv fits analysis of variance (ANOVA) models directly to the data

anova() function performs analysis of variance (ANOVA) on model objects

Input Format of Model

Accepts formula-based models (y ~ x1 + x2)

Accepts model objects generated by functions like lm() or glm()

Output Format

Returns an ANOVA table with sources of variation and associated statistics

Returns an ANOVA table comparing models or factors, showing sources of variation and statistics

Usage

It is used for directly conducting ANOVA tests on data

For comparing models or factors using ANOVA tests

Flexibility

Limited to fitting ANOVA models directly to data

Flexibility Limited to fitting ANOVA models directly to data Can compare multiple models or factors, providing more flexibility

Example

aov(response_variable ~ factor1 + factor2, data=my_data)

anova(lm_model1, lm_model2) comparing two linear models

When to Use aov() in R

  1. Use aov() when we want to directly perform an analysis of variance (ANOVA) on our data.
  2. It’s useful when you have a simple experimental design with one or more categorical predictor variables and a continuous response variable.
  3. aov() accepts formula-based models (response_variable ~ factor1 + factor2) directly.
  4. When we have a single model and want to examine the sources of variation and associated statistics, aov() provides a straightforward way to do so.
  5. If our analysis goal is primarily to test for differences in means between groups or factors, aov() is sufficient for conducting basic ANOVA tests.

When to Use anova() in R

  1. Use anova() when you want to compare the fits of multiple models or factors.
  2. It allows you to assess whether adding or removing factors significantly improves the model fit.
  3. When you have fitted several models (e.g., with lm() or glm()), anova() helps in comparing these models to see which one best explains the data.
  4. anova() offers more flexibility as it can handle comparisons between different types of models, not just ANOVA models.
  5. If your analysis requires more advanced statistical comparisons or if you need to assess the significance of interactions between factors, anova() is more suitable

Conclusion

In conclusion, both aov() and anova() functions in R serve important roles in conducting analysis of variance (ANOVA) tests, which are fundamental in statistical analysis. When considering which to use, it’s essential to understand the nuances and specific purposes of each. aov() is ideal for directly analyzing variance within a dataset, especially when examining differences in means across different groups or factors. It operates directly on formula-based models and is well-suited for basic ANOVA testing in simpler experimental designs.



Contact Us