Regression PDF
Regression PDF
com
Predictive Modelling
using
Linear Regression
Table of Contents
Concept of Regression Analysis
Page 2 of 16
https://leaps.analyttica.com
For example, to describe the relationship between diesel consumption and industrial production, if
it is assumed that “diesel consumption” is the effect of “industrial production”, we can do a
regression analysis to predict value of “diesel consumption” for some specific value of “industrial
production”
To do this, we first try to assume a mathematical relationship between the target and the
predictor(s). The relationship can be a straight line (linear regression) or a polynomial curve
(polynomial regression) or a non-linear relationship (non-linear regression). This can be done
through various ways. The simplest and most popular way is to create a scatter plot of the target
variable and predictor variable. (Refer to Figure 1 and Figure 2)
Once the type of relationship is established, we try to find the most-likely values of the coefficients
in the mathematical formula.
Regression analysis comprises of the entire process of identifying the target and predictors, finding
the relationship, estimating the coefficients, finding the predicted values of target, and finally
evaluating the accuracy of the fitted relationship.
Page 3 of 16
https://leaps.analyttica.com
Let’s say, we want to estimate the credit card spend of the customers in the next quarter. For each
customer, we have their demographic and transaction related data which indicate that the credit
card spend is a factor of age, credit limit and total outstanding balance on their loans. Using this
insight, we can predict future sales of the company based on current and past information.
It indicates the strength of impact of multiple independent variables on a dependent variable and
helps to determine which variables in particular, are most significant predictors of the dependent
variable. Their influence is quantified by the magnitude and sign of the beta estimates, which is
nothing but the extent to which they impact the dependent variable.
It also allows us to compare the effect of variable measures on different scales and can consider
nominal, interval, or categorical variables for analysis.
The simplest form of the equation with one dependent and one independent variable is defined by
the formula:
y = c + b*x,
where
y = estimated dependent score,
c = constant,
x = independent variable.
Page 4 of 16
https://leaps.analyttica.com
Linear Regression
Linear regression is one of the most commonly used predictive modelling techniques. It establishes
a relationship between dependent variable (Y) and one or more independent variables (X) using
a best fit straight line (also known as a regression line).
It is represented by an equation 𝑌 = 𝑎 + 𝑏𝑋 + 𝑒, where a is the intercept, b is the slope of the
line and e is the error term. This equation can be used to predict the value of a target variable based
on given predictor variable(s).
Logistic Regression
Logistic regression is used to explain the relationship between one dependent binary variable and
one or more nominal, ordinal, interval or ratio-level independent variables.
Polynomial Regression
A regression equation is a polynomial regression equation if the power of independent variable is
more than 1. The equation below represents a polynomial equation.
𝑌 = 𝑎 + 𝑏𝑋 + 𝑐𝑋2
In this regression technique, the best fit line is not a straight line. It is rather a curve that fits into the
data points.
Ridge Regression
Ridge regression is suitable for analyzing multiple regression data that suffers from multicollinearity.
When multicollinearity occurs, least squares estimates are unbiased, but their variances are large so
they may be far from the true value. By adding a degree of bias to the regression estimates, ridge
regression reduces the standard errors. It is hoped that the net effect will be to give estimates that
are more reliable.
Page 5 of 16
https://leaps.analyttica.com
Linear Regression
Linear Regression is a predictive modelling technique that establishes a relationship
between dependent variable (Y) and one or more explanatory variables denoted by X, using a best
fit straight line (also known as regression line).
This equation can be used to predict the value of target variable based on given predictor variable(s).
The case of one explanatory variable is called simple linear regression. For more than one
explanatory variables, the process is called multiple linear regression. In this technique, the
dependent variable is continuous, independent variable(s) can be continuous or discrete, and nature
of the regression line is linear.
The following sections discuss in detail, the process of developing and evaluating a regression
model. An important concept to recall at this point, is that of Data Splitting, which requires the data
to be randomly split into Training and Validation datasets. The rationale behind splitting the data is
that the model is built on one dataset (training) and its performance is evaluated on the validation
dataset to evaluate its performance on a new, unknown dataset.
In all following discussions, it is understood that the model building and evaluation process
(determining the best fitting line and estimating the accuracy of the model) is done on the training
dataset, and the model validation is done on the validation dataset.
Page 6 of 16
https://leaps.analyttica.com
Figure 3
Since there can be multiple lines that fit the data, the challenge arises in choosing the one that best
fits. As we already know, the best fit line can be represented as
𝑦̂𝑖 = 𝑏0 + 𝑏1 𝑥𝑖
Where,
When we predict height using the above equation, the predicted value of the prediction wouldn't
be perfectly accurate. It has some "prediction error" (or "residual error"). This can be represented as
𝑒𝑖 = 𝑦𝑖 − 𝑦̂𝑖
A line that fits the data best will be one for which the n (i = 1 to n) prediction errors, one for each
observed data point, are as small as possible in some overall sense.
One way to achieve this goal is to invoke the "least squares criterion," which says to "minimize the
sum of the squared prediction errors."
Page 7 of 16
https://leaps.analyttica.com
Because the deviations are first squared, then added, there is no cancelling out between positive
and negative values.
Because the formulas for b0 and b1 are derived using the least squares criterion, the resulting
equation, 𝑦̂𝑖 = 𝑏0 + 𝑏1 𝑥𝑖 , is often referred to as the "least squares regression line," or simply the
"least squares line." It is also sometimes called the "estimated regression equation."
Page 8 of 16
https://leaps.analyttica.com
Figure 4
An analyst wants to understand what factors (or independent variables) affect credit card sales. Here,
the dependent variable is credit card sales for each customer, and the independent variables are
income, age, current balance, socio-economic status, current spend, last month’s spend, loan
outstanding balance, revolving credit balance, number of existing credit cards and credit limit. In
order to understand what factors affect credit card sales, the analyst needs to build a linear
regression model.
It is important to note that a linear regression cannot be applied to categorical variables, and is not
recommended for ordinal variables, hence, the analyst may also need to check the variable type
before running a model.
In this simulation, the learner is exposed to a sample dataset comprising of telecom customer
accounts and their annual income, age along with their average monthly revenue (dependent
variable). The learner is expected to apply the linear regression model using annual income as the
single predictor variable.
Page 9 of 16
https://leaps.analyttica.com
For an in-depth understanding of all the topics covered in the coming sections, refer to the course
“Fundamentals of Data Analytics” on Analyttica TreasureHunt
(https://leaps.analyttica.com/courses/overview/Fundamentals-of-Data-Analytics). You can also
refer to any of the standard Statistics books for more information on the same.
The null hypothesis here is “The target variable cannot be significantly predicted using the predictor
variable(s)”. To do this we look at the F-statistic and its p-value. Mathematically, the null hypothesis
we test here is “All slope parameters are 0” (note the number of slope parameters will be the same
as the number of independent variables in the model). Hence, if the null hypothesis is accepted (or
not rejected) then it means we cannot predict target variable using the predictor variables and hence
regression is not possible.
Page 10 of 16
https://leaps.analyttica.com
Coefficient of Determination
Next, we look at the R-squared value of the model, which is also called the “Coefficient of
Determination”. This statistic calculates the percentage of variation in target variable explained by
the model. The below illustration captures the explained vs. unexplained variation in data.
Figure 5
R-squared is calculated using the following formula:
𝐸𝑥𝑝𝑙𝑎𝑖𝑛𝑒𝑑 𝑉𝑎𝑟𝑖𝑎𝑛𝑐𝑒 ∑𝑛 (𝑌̂𝑖 − 𝑌̅)2
𝑅2 = 𝑇𝑜𝑡𝑎𝑙 𝑣𝑎𝑟𝑖𝑎𝑛𝑐𝑒 = ∑𝑖=1
𝑛 (𝑌𝑖 − 𝑌 ̅ )2
𝑖=1
R-squared is always between 0 and 100%. As a guideline, the more the R-squared, the better is the
model. The objective is not to maximize the R-squared, since the stability and applicability of the
model are equally important. Next, check the Adjusted R-squared value. Ideally, the R-squared and
adjusted R-squared values need to be in close proximity of each other. If this is not the case, then
the analyst may have over fitted the model and may need to remove the insignificant variables from
the model.
In this simulation, the learner is exposed to a sample dataset capturing telecom customer accounts
and their annual income, age, along with their average monthly revenue (dependent variable). The
dataset also contains predicted values of “average monthly revenue” from a regression model. The
learner is expected to apply the concept of calculation of coefficient of determination.
Page 11 of 16
https://leaps.analyttica.com
Module 2 Simulation 1: Build a Multivariate Linear Regression Model and Evaluate Parameter
Significance
In this simulation, the learner is exposed to a sample dataset capturing the flight status of flights
with their delay in arrival, along with various possible predictor variables like departure delay,
distance, air time, etc. The learner is expected to build a multiple regression model where all the
variables are significant.
Residual Analysis
We can also evaluate a regression model based on various summary statistics on error or residuals.
Some of them are:
• Root Mean Square Error (RMSE): Where we find average of squared residuals as per the given
formula:
n 2
RMSE = 1 ∑(Y − ̂Y )
n i i
i=1
• Mean Absolute Percentage Error (MAPE): We find the average percentage deviation as per
the given formula:
n
ABS(Y − ̂Y )
i i
MAPE = ∑ 1
n Yi
i=1
We also often look at the distribution of absolute percentage deviation across all observations.
Page 12 of 16
https://leaps.analyttica.com
Rank Ordering
Observations are grouped based on predicted values of the target variable. The average of the actual
vs. predicted values of the target variable, across the groups, is observed to see if they move in the
same direction across the groups (increase or decrease). This is called the rank ordering check.
Page 13 of 16
https://leaps.analyttica.com
There are various techniques available to select the best set of variables. Variable reduction
techniques vary depending on the kind of modeling technique used. For linear regression, we often
use any one or more of the following techniques:
• Box-Cox transformations
• Variable multicollinearity through Variance Inflation Factor (VIF)
• Principal Component Analysis
• Stepwise/ Forward/ Backward variable selection technique
In this simulation, the learner is exposed to U. S. Census Bureau data on per capita retail sales along
with some socio-economic variables for the year 1992, of 845 US Standard Metropolitan Statistical
Areas (SMAS). The objective is to predict the “Per Capita Retail Sales” using other socio-economic
variables as possible predictors. The socio-economic variables are not always linearly related and
hence the learner is expected to try various transformations on the variables and try to see which
fits the model better.
Here we need to discuss one more important aspect of regression model fitting. Often, we find one
predictor variable to be exceptionally strong in a regression model compared to other predictors.
Such predictors also contribute to the extremely high accuracy of the model. This is known as the
problem of overfitting.
The main problem of these models is the fact that these models become too dependent on a single
variable. If there is any issue with the value of that specific variable the entire model fails. Sometimes,
the selected variables are actually a part of the target variable.
For Example: Suppose we are trying to fit a regression model to predict “Household Expenditure” of
a household using various predictor variables like “Household Income”, “Household Size”, “City Cost
of Living Index”, etc. Now, note that “Household Income” is expected to have a very high impact on
Page 14 of 16
https://leaps.analyttica.com
the model compared to any other predictor. Also, the model can be very high on efficiency. For a
linear model, the R-square can be as high as 98-99%. On removing the variable from the model, the
R-square will come down to may be 20% or 30%.
Now think about if “Household Income” is a right variable to predict “Household Expenditure”.
Expenditure is actually a part of income. Often, people are reluctant about revealing their actual
income, introducing high levels of impurity in the data. Hence, we should not have included the
variable in the model.
Also, 98% R-square is too high to believe in any real-life scenario. In general, any linear model with
an R-square more that 75% or 80% must be subjected to detailed inspection and checked for
overfitting. Models with an R-square of 40% - 50% are deemed acceptable in most practical cases.
Module 2 Simulation-4: Learn & Apply concepts of Variable Selection & Overfitting
In this simulation, the learner is exposed to a sample dataset capturing flight fares where the
objective is to predict fare between two locations. The learner is expected to select the significant
variable for the model first and then check if there is any problem of overfitting. If found, learner
should remove the requisite variable(s) and iterate through the variable selection process.
Page 15 of 16
https://leaps.analyttica.com
Write to us at
[email protected]
USA Address
Analyttica Datalab Inc.
1007 N. Orange St, Floor-4, Wilmington, Delaware - 19801
Tel: +1 917 300 3289/3325
India Address
Analyttica Datalab Pvt. Ltd.
702, Brigade IRV Centre,2nd Main Rd, Nallurhalli,
Whitefield, Bengaluru - 560066.
Tel : +91 80 4650 7300
Page 16 of 16