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?__

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

__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

weight<-c(150,180,140,128,133,152,131) height<-c(62,84,55,52,54,63,53)

Step2: Build Model using lm() function

model <- lm(height~weight) print(model)

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

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

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")

Output:

**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)) 1 2.198528

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")

Output:

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

Hi,

Can you explain first code I hope few mistakes are there. Like in plot function u r passing y,x( which is not defined) instead height and weight is defined. Even in graph how are you showing 10 point I. Graph when you are passing 7 value.

Correct Akshay, By mistake i plot the graph using variables y and x….

Thanks for pointing out the mistake..