Tag Archives: Regression

ByHariharan Rajendran

Linear Regression with R – Part 2

Check the Linear regression introduction and how to use excel for linear regression.

In this post, let me explain how to use R programming for linear regression.

I am taking the same dataset in R to produce the linear regression.

Run the below codes.

Year<- c(2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010)

SalesAmount<-c(1450,1486,1560,1200,1205,1100,1150,1750,1650,1500,1900)

#linear regression

result<-lm(SalesAmount~Year)

#display coefficient

result

# new value for year 2011

year2011 <- data.frame(Year=2011)

year2011

#predict the sales amount.

predict (result,year2011)

ByHariharan Rajendran

Linear Regression

Before starting with linear regression, let us try to understand the machine learning concepts.

As of today, machine learning is one of the trending technologies as the applications are started to consume real-time data and deliver machine learning services.

As part of machine learning, we have three different types of machine learning models available.

  1. Supervised Learning
  2. Unsupervised Learning
  3. Reinforcement learning

In Supervised Learning, the system tries to learn from the previous examples that are given.

Under supervised learning, we have classification and regression models.

One of the common and simple techniques for predicting a continuous variable is called linear regression.

It assumes a linear relationship between the outcome and the predictor variables.

The linear regression equation can be written as y = b0 + b*x + e, where:

  1. b0 is the intercept,
  2. b is the regression weight or coefficient associated with the predictor variable x.
  3. e is the residual error

This post covers the linear regression on excel and R studio.

Linear Regression on Excel

Follow the below steps to perform the linear regression in excel

Step 1: Consider you have year wise sales amount. You are in the situation to predict the sales amount for the year 2011.

Step 2: Create a scatter plot like below with Year and Sales Amount

Step 3: Right-click on the line and choose trendline.

Step 4: Display the equation on the trendline by selecting the checkbox on the Format trendline properties.

Step 5: Predict the value using the generated formula for existing values.

Step 6: Predict for the year 2011.

As you can see the values are not 100% correct, we need to adjust the con-efficient and reduce the r2 values to get the perfect result.

1