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.


print(head(data))

OUTPUT 1:

ANALYSIS OF COVARIANCE IN R PROGRAMMING
ANALYSIS OF COVARIANCE IN R PROGRAMMING

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:

ANALYSIS OF COVARIANCE IN R PROGRAMMING

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:

ANALYSIS OF COVARIANCE IN R PROGRAMMING

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.


Related Topics

R Operators

A symbol that tells the compiler to perform specific mathematical or logical operations is called operator. R language supports mainly 5 different types of operators, which are listed below: Arithmetic...

3 minutes read.

Scatter Plot in R Programming

A Scatter plot is a type of dispersion graph built to represent the various data points of variables. A Scatter plot is also known as a scatter graph or scattergram.  A scatter plot is used...

4 minutes read.

Various types of Charts in R Programming

As we know, R Language is mostly used for analyzing the data and statistics purposes to represent our data graphically & pictorially in R studio. Charts and graphs are plotted in...

4 minutes read.

Linear Regression in R Programming

Linear Regression is an unsupervised machine learning algorithm in R programming. Linear Regression is a type of regression analysis that shows the relationship between two or more variables. And one...

3 minutes read.

Bubble Chart in R Programming

In this article, we will talk about another type of chart in R programming, i.e., the Bubble chart. A bubble chart is a type of data visualization chart that helps display multiple...

4 minutes read.

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...

2 minutes read.

Measures of Central Tendency in R Programming

Central Tendency or CT is one of the features of descriptive statistics. The Central tendency will let us know how the various groups of data are clustered around the central...

4 minutes read.

R Inheritance

One the main concept of object oriented programming is inheritance which allows us to define a new class of existing classes. This means that we can derive new classes from...

5 minutes read.

Normal Distribution in R Programming

In statistics, the normal distribution is a type of probability function. Normal distribution tells the user about the distribution of data values in the dataset. It is a very important...

3 minutes read.

R Variables, Constants and Reserved Words

R Variables Variables are very similar to an open box, where you can put any value of your wish. We can change the values as well. Variables are used to store...

4 minutes read.

How to create Histograms in R

R Histograms A Histogram is the graphical representation of the distribution of numeric data. It takes only one numeric variable as input. The variable is cut into several bars (also called...

2 minutes read.

Binomial Distribution in R Programming

In this article, we will talk about the Binomial distribution in R programming. The binomial distribution is a type of probability distribution. As it is a discrete distribution, it will...

3 minutes read.

Introduction to R Programming

What is R Programming? R is a programming language which provides an environment for software used for statistical analysis, graphics representation and reporting. It possesses an extensive catalog of...

4 minutes read.

R Functions

In programming, a function is a group of instructions that you want to use repeatedly, because of their complexity, are better self-contained in sub-program and called when needed. Hence, we can...

4 minutes read.

R Switch Statement

It is an alternate way to a nested if…else statement. A switch statement permits an expression to be tested against a list of case values. In short, we can say...

1 minute read.

How to make Boxplots in R

R Boxplots Boxplot is a measure of how well the data is distributed in a data set. It is used to give a summary of one or several numeric variables. The...

3 minutes read.

R If Else Statement

If statement It is the most simple decision-making statement. It checks the condition if the condition is true then only it will execute the block of statements written in the ‘if...

2 minutes read.

Pie Charts in R Programming

R programming has various libraries to plot various types of charts and graphs. A pie chart is a circular statistical chart or graph, which is divided into slices to represent the numerical...

4 minutes read.

R Date and Time

R provides very large range of capabilities to deal with times and dates. Generally, dates are internally stored as integers, and depending on the operations we will need, we could...

3 minutes read.

First R Program

First  Hello World R Program As a convention, our first R program will be the “Hello World!” program. We can run our R program either at R command prompt or we...

3 minutes read.