Sitemap
Sidharth Sekhar

Sidharth Sekhar

Loves problem solving and critical thinking. Data enthusiast.

Multiple Regression

3 min readAug 13, 2019

More often than not in regression problems, there is more than one independent variable based on which the dependent variable’s value differs. A simple regression model won’t suffice for these kinds of problems hence there is a need to create a model that will take into consideration each of the independent variables. That is exactly what multiple regression does.

D is the number of independent variables

The above mathematical relation is used to establish the relationship between the variables. There are a certain number of assumptions one must verify before using multiple regression as deviations from these assumptions will result in spurious models that won’t predict the right values. The assumptions are,

The relationship between the dependent variable and independent variables should be linear in nature.

There should be no Auto correlation between the residue terms.

There should not be correlation between the independent variables.

The residue terms should be homoscedastic and normally distributed.

The primary assumption i.e. test for linearity can be checked with scatter plot before which we check for normality of the data distributions. Using a distplot in seaborn package.

Press enter or click to view image in full size
Data is not normally distributed.

Log transforming the data

Press enter or click to view image in full size
Distribution after log transformtion.

Then scatterplots are plotted next to check linear relationship.

Press enter or click to view image in full size
Scatterplot showing linear relationship

Next we are going to check for correlation between the 2 independent variables.

The continuous independent variables sqft_living and sqft_living15 are highly correlated.

So we remove sqft_living15 following which we one hot encode all categorical data in the dataset.

Get Sidharth Sekhar’s stories in your inbox

Join Medium for free to get updates from this writer.

Our dataset is now ready to be fed into regression algorithm for predictions.

When given as input to a regression model, it being

Code snippet

The data given to the model had 19 input parameters and for each of them there were weights produced.

Press enter or click to view image in full size

Interpreting Weights

When there is a multiple regression problem, there will be a weight associated with each of the input parameter. It is important how we interpret these weights to understand multiple regression better. The weights are interpreted as, “how much does the dependent variable change for unit change in one independent variable, keeping rest of the features constant.It’s for this reason we don’t take two highly correlated features as input to the model.”

The residual vs fitted values plot. (Also used to check for assumption of regression)

The random spread of the residual vs fitted plot says that there is linear relationship between dependent and independent variable, presence of outliers is less and variance or error terms are equal.

In this work, I used scikitlearn’s train_test_split to split the data into training and testing set. The input parameters were log transformed. Log transformations are important as certain independent variables were skewed. To reduce the skew and to have more linear trends we transform the data.

Press enter or click to view image in full size
Predict method used to give the final output.

The trained model had a R2 score of 72.13 with mean squared error of 223.62

--

--

Sidharth Sekhar
Sidharth Sekhar

Written by Sidharth Sekhar

Loves problem solving and critical thinking. Data enthusiast.