Tutorial on Linear Regression using R Programming – Learn How to Predict the Data Output

R is widely used for Statistical Analysis and Data Mining Techniques. I have already shown in the previous tutorial How to perform kNN Classification on IRIS Dataset using R Programming. Today I am going to discuss about very popular Technique: Linear Regression. It is an approach for modeling the relationship between a scalar dependent variable y and one or more explanatory variables (or independent variables) denoted X (Source: Wikipedia).

What is Linear Regression?

In Linear Regression two variables are related with the help of an equation such as given below:

Y = ax + b where y is response variable, x is predictor, a and b are constants. Normally Linear Regression is shown with the help of straight line as shown below:


[Image Source – Wikipedia]

Linear Regression using R Programming

To demonstrate How Linear Regression can be applied using R, I am going to consider two case studies: One Case Study will involve analysis of the algorithm on data created by me and other analysis will be on Iris Dataset.

Case Study 1: Predicting Height of the person based on Weight.

Step1: Create the data in R


Step2: Build Model using lm() function

model <- lm(height~weight)

Step3: Predict Weight based on new Height value using model build in the previous step

test <- data.frame(weight=165)
result <- predict(model,test)

You can see that out model predicted the height of the person as 72.83 for input weight of 165.

We can also see the result in a graphical form using plot function as shown below:

plot(height,weight,col = "red",main = "Example of Linear Regression",abline(lm(weight~height)),cex = 1.3,pch = 16,xlab = "Weight",ylab = "Height")


Linear Regression using R - Output1

Case Study 2: Perform Linear Regression on Iris Dataset. To demonstrate the use of Linear Regression in a dataset I am going to consider two variables Petal Length and Petal Width.

Step1: Build Linear Regression Model on the Length and Width Column

model.iris <- lm(Petal.Length~Petal.Width, data=iris)

Step2: Predicting the Length of Petal based on the Width of the Petal

predict(model.iris, newdata=data.frame(Petal.Width=0.5))

Step3: Plot Graph

plot(iris$Petal.Width, iris$Petal.Length,col = "blue",main = "Example of Linear Regression on Iris Data",abline(model.iris),cex = 1.3,pch = 16,xlab = "Petal width",ylab = "Petal Length")


Linear Regression using R - Output2

I hope you like the Tutorial on Linear Regression using R Programming. For any Query please comment below:


  1. Akshay May 10, 2016
    • techno@admin May 11, 2016

Leave a Reply