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 of these variables is called the predictor variable, and the other is the response variable.

The predictor variable is estimated by using some statistical experiments. The predictor variable is the one whose value is gathered through experiments. And the Responsible variables are the ones whose value is derived from the predictor variable.

Linear Regression is the most used regression technique over all other regression techniques. Linear Regression creates a predictive model using the relevant data to show trends.

Below is the simple equation of Linear Regression,

y = ax+b


Description of the above parameters, 
y = Responsible Variable
a =Coefficients (Constants)
x = Predictor Variable
b = Coefficients (Constants)

The linear regression graph is plotted as a straight line, as shown below.

LINEAR REGRESSION IN R PROGRAMMING

This is the simplest plot of linear regression. That orange line represents a linear regression. And the black dots represent the data points.

Let's take the example of linear regression to understand the topic better. A very simple example of linear regression can be the height and weight of a student. A student's height is already known to us, and we have to predict a student's weight.

The first step is to gather a sample of observed values of a height and the corresponding weight of the student. Then by using the built-in lm() function R, we will create a relationship model. After that, we will find the coefficients from the above model and create the mathematical equations using all the information.

Once we are done with the equation, we will get the summary of our relationship model to know the average error in the prediction, which can also be termed residuals. And at the end, to predict the weight of a new student, we will use the predict() function.

INPUT DATA:

HEIGHTWEIGHT
15362
16570
17882
13759
12946
17275
16368
15560
14455
14957

CODE:

# Linear regression
# The first step is to input data 
# x is the height of a student
# y is the weight of a student


x <- c(153, 165, 178, 137, 129, 172, 163, 155, 144, 149)
y <- c(62, 70, 82, 59, 46, 75, 68, 60, 55, 57)
  


# Applying the lm() function
relation <- lm(y~x)
print(relation)

When we try to execute the above code in R studio, we get the output as:

OUTPUT:

LINEAR REGRESSION IN R PROGRAMMING

Now, we will get the summary of our relationship mode and use print(summary(relation)).

CODE:

x <- c(153, 165, 178, 137, 129, 172, 163, 155, 144, 149)
y <- c(62, 70, 82, 59, 46, 75, 68, 60, 55, 57)
  


# Applying the lm() function
relation <- lm(y~x)
print(relation)


# Finding Relationship:
print(summary(relation))

OUTPUT:

LINEAR REGRESSION IN R PROGRAMMING

predict() function:

We are using predict function to predict the weight of the new student.

Syntax of predict() function:

predict(object, newdata)


Where, 


object – formula (already created using lm() function)
new data – vector containing new values 

CODE:

# Linear regression
# The first step is to input data 
# x is the height of a student
# y is the weight of a student


x <- c(153, 165, 178, 137, 129, 172, 163, 155, 144, 149)
y <- c(62, 70, 82, 59, 46, 75, 68, 60, 55, 57)
  


# Applying the lm() function
relation <- lm(y~x)
print(relation)


# Here we are finding weight of a student with height 172.
a <- data.frame(x = 172)
result <-  predict(relation,a)
print(result)


When we try to execute the above code in R studio, we get the following result:

OUTPUT:

LINEAR REGRESSION IN R PROGRAMMING

Visualizing the Regression:

The last step which is left to perform is visualizing the regression graphically.

CODE:

# Linear regression
# The first step is to input data 
# x is the height of a student
# y is the weight of a student


x <- c(153, 165, 178, 137, 129, 172, 163, 155, 144, 149)
y <- c(62, 70, 82, 59, 46, 75, 68, 60, 55, 57)




# Applying the lm() function
relation <- lm(y~x)
print(relation)


# Visualizing the graph
# Plot the chart.
plot(y,x,col = "purple",main = "Linear Regression",
     abline(lm(x~y)),cex = 1.3,pch = 16,xlab = "Weight in Kg",ylab = "Height in cm")

When we try to execute the above code in r studio, we get the following result:

OUTPUT:

LINEAR REGRESSION IN R PROGRAMMING

Conclusion:

We can conclude that linear regression is very simple, easy to understand, and easy to fit, yet it is a very powerful model. This article taught us how to perform linear regression model analysis on R. This was all about linear regression in R programming.


Related Topics

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.

R Pie Charts

R programming language provides many numbers of libraries to create charts and graphs. A pie-chart is a representation of data as slices of a circle with different colors representing counts...

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

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.

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.

Clustering in R Programming

The clustering technique is an unsupervised machine learning in R programming. Before discussing clustering techniques, let's have a look at what unsupervised machine learning is?  Unsupervised learning is training the model with...

3 minutes read.

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.

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.

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.

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.

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.

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 JSON File

JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data. JSON file is often used when data is sent from a server to a...

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.

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.

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.

R Excel Files

Microsoft Excel is the spreadsheet program which stores data in the .xls and .xlsx format. R has the facility to read directly from these files using some excel specific packages....

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.

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.

R Tutorial

Introduction to R Programming R is nothing but a programming language, also a free software environment. This software used for predictive analysis, statistical analysis, graphical representation, data modeling, and reporting. R...

9 minutes read.