# Analysis of Covariance in R Programming

Analysis of Covariance can also be named ANCOVA. We know that we use the concept of regression analysis for creating models which can explain the effect of the variation in predictor variables on the response variables.

Let’s take an example we have a categorical variable. Categorical variables include Yes/No Questions, True/False or M/F. Using simple regression analysis will give us different results for each value of categorical variables. And we can study the effect of the categorical variable by using it as the predictor variable and comparing the regression line for each level of the categorical variable. This is known as Analysis of Covariance or ANCOVA.

To better understand the topic Analysis of Covariance, we will take one example. Let’s start by using the built-in data set “mtcars”.

EXAMPLE 1:

CODE 1:

The first step is to import the mtcars dataset in RStudio as follows:

# ANALYSIS OF COVARIANCE
# ANCOVA

# The first step is to import the built-in dataset "mtcars".
# And then, we will print the dataset.
data <- mtcars[,c("am","mpg","hp")]
print(data)

# This will print the whole dataset.
# So, by using the head() function, we will only print the starting rows of the dataset.

OUTPUT 1:

CODE 2:

# ANALYSIS OF COVARIANCE
# ANCOVA

# Getting the dataset in RStudio.
data <- mtcars

# Creating the regression model of the mtcars dataset.
data <- aov(mpg~hp*am,data = input)

# Summary function will print the summary of our mtcars dataset.
print(summary(data))

OUTPUT 2:

CODE 3:

# ANALYSIS OF COVARIANCE
# ANCOVA

# Getting the dataset.
input <- mtcars

# Creating the regression models for both the data.
data1 <- aov(mpg~hp*am,data = input)
data2 <- aov(mpg~hp+am,data = input)

# Comparing the two models.
print(anova(data1,data2))

OUTPUT 3:

CONCLUSION:

As we can see, the p-value is more significant than 0.05. We can conclude that the interaction between the horsepower and transmission type is not effective to each other. And mileage per gallon will depend on the horsepower of the car in both modes (Auto transmission mode and Manual transmission mode).

SUMMARY:

Here, we come to the end of this report. In this report, we learned about the analysis of covariance or ANCOVA and how to perform this analysis in RStudio.