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

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 Data Types

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are...

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.

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.

Poisson Distribution in R Programming

Poisson distribution is a type of distribution that deals with the probability distribution of the data values by taking the mean into consideration. Poisson distribution will estimate the probability value for...

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

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.

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.

Decision Tree in R Programming

The decision tree uses the branching method to show every possible output for the specific input. We can draw a decision tree by hand or create it using specialized software...

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.

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.

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.

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

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.

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.

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.

Logistic Regression in R Programming

Logistic Regression is a classification supervised machine learning algorithm in R programming. Logistic Regression can also be termed Binomial Logistic Regression, Binary Logistic Regression, or Logit Model. Logistic Regression is...

4 minutes read.