ANOVA (Analysis of Variance) Test in R Programming (original) (raw)

Last Updated : 14 Apr, 2026

ANOVA (Analysis of Variance) is a statistical technique used to analyze the relationship between categorical variables and continuous variables in R. It helps determine whether the means of different groups are significantly different by comparing the variation within groups to the variation between groups. ANOVA is widely used in business, biology, social sciences and experimental research.

anova_

Anova

Understanding Hypotheses in ANOVA

ANOVA is used to test if group means of a continuous variable differ based on categorical grouping.

Assumptions of ANOVA

ANOVA relies on several key assumptions to ensure valid results:

Types of ANOVA

Implementation of ANOVA Test

We perform the ANOVA tests using the mtcars dataset in R and compare the results of one-way and two-way ANOVA.

1. Installing and Loading Required Packages

We install and load the necessary packages for data manipulation and ANOVA.

install.packages("dplyr") library(dplyr)

`

2. Viewing the Dataset

We inspect the first few rows of the dataset to understand the structure.

head(mtcars)

`

**Output:

dataframe

Output

3. Performing One-Way ANOVA

We conduct a one-way ANOVA to examine if the mean displacement (disp) differs across gear levels (gear).

mtcars_aov <- aov(mtcars$disp ~ factor(mtcars$gear)) summary(mtcars_aov)

`

**Output:

dataset

Output

4. Performing Two-Way ANOVA

We conduct a two-way ANOVA to analyze the influence of both gear and am (transmission) on disp.

mtcars_aov2 <- aov(mtcars$disp ~ factor(mtcars$gear) * factor(mtcars$am)) summary(mtcars_aov2)

`

**Output:

dataframe

Output

5. Finding the Best-Fit Model with AIC

We compare both models using AIC to determine the better fit.

install.packages("AICcmodavg") library(AICcmodavg) model.set <- list(mtcars_aov, mtcars_aov2) model.names <- c("mtcars_aov", "mtcars_aov2") aictab(model.set, modnames = model.names)

`

**Output:

data

Output

6. Visualizing the ANOVA Results

We use ggplot2 to visualize the distribution of disp across gear levels.

install.packages("gridExtra") library(gridExtra)

install.packages("ggplot2") library(ggplot2)

plot1 <- ggplot(mtcars, aes(x = factor(gear), y = disp, fill = factor(gear))) + geom_boxplot(color = "black", alpha = 0.7) + labs(title = "One-Way ANOVA", x = "Gear", y = "Displacement") + theme_minimal() + theme(legend.position = "top")

plot2 <- ggplot(mtcars, aes(x = factor(gear), y = disp, fill = factor(am))) + geom_boxplot(color = "black", alpha = 0.7) + labs(title = "Two-Way ANOVA", x = "Gear", y = "Displacement") + theme_minimal() + theme(legend.position = "top")

grid.arrange(plot1, plot2, ncol = 2)

`

**Output:

boxplot

Output