Simple and Multiple Linear Regression in Python
By: Minesh Barot
Tags: Data Science, Linear Regression, Data Visualization, Python, Predictions

To begin modelling, the first step is to import the required libraries and most importantly the dataset. The drinks dataset has been used for this tutorial which can be found here. The goal is to analyze and predict alcohol consumption using the features present in the dataset.

Now, that the dataset has been loaded it is time to take a look at it to understand the variables, data types and the overall structure. It is important to know the variables present in the dataset, hence, the head() method is applied to check the columns.

One can see that the data set is a mixture of text and numbers, therefore, the dtypes method is applied to make sure that the data types are defined appropriately. In the output, it can be seen that all the numerical columns are either of data type integer or float and the textual values are of type object. Thus, there is no need for converting the data types.

Let’s dig into the relationship between the different types of alcohol per continent to understand the data further. Since the variable continent is categorical, the groupby() method is applied by appending the sum() at the end to find the total distribution of alcohol per continent.

It is seen that beer_servings are the highest and that Europe has the maximum beer_servings. To understand it’s distribution in detail apply the describe() method which will show a statistical summary.

Before modelling Multiple Linear Regression, let’s understand and apply Simple Linear Regression(SLR). This is the most basic form of regression modelling and it assists one to understand the relation between two variables, viz; the dependent/target variable(y) and the independent/predictor variable(x). Following is the equation for SLR: y = bo + b1x, wherein bo is the intercept and b1 is the slope. Python easily calculates these values.

To apply SLR, one independent and one dependent variable have to be selected. Since the goal is to predict alcohol consumption total_litres_of_pure_alcohol is chosen as the target variable. There are three independent variables and for SLR only one independent variable is needed. The solution to this problem is to check the correlation of the three independent variables with the dependent variable. This can be done by using the pairplot() method from the seaborn library.

Since beer_servings and total_litres_of_pure_alcohol show the best correlation, beer_servings is selected as the independent variable.

Modelling Simple Linear Regression:

Firstly, import the linear model from scikit-learn. Secondly, create a Linear Regression object using the constructor. Thirdly, define the predictor and target variable. Fourthly, use the fit() method to fit the model.

To find bo apply the intercept method and to find b1 apply the coef method.

Therefore the Simple Linear Regression Equation is as follows —

total_litres_of_pure_acohol = 1.40 + 0.031*beer_servings

Finally, use the predict() method to predict the total_litres_of_pure_alcohol. In this case, only the first five predicted values have been displayed.

Now that alcohol consumption can be predicted, the company can determine its production level and the quantity that needs to be distributed to avoid wastage and achieve maximum profits. However, the model needs to be evaluated to check its goodness. This can be done either numerically or visually.

Let’s evaluate the model numerically using R squared, which is nothing but a measure of how close the data points fit the regression line. An R squared value lies between 0 to 1. A model is considered good enough if the R squared value nears 1. Simply using the score() method gives the R Squared value.

The R squared value is close to one, hence, it can be said that the model is a good fit. Moreover, R squared also tell the percentage of variation in the dependent variable that is accounted for by its regression on the independent variable. Hence, in this case, it can be said that approximately 70% of the total variation in total_litres_of_pure_alcohol is explained by beer_servings.

Another way to evaluate is by checking the residual plot. To view a residual plot simply apply the residplot() method of searborn and pass in the values.

The residual plot displays that the data points are randomly spread around the x-axis. Thus the model is appropriate. However, if there was curvature in the plot that would mean the model is not a good one.

Modelling Multiple Linear Regression:

Multiple Linear Regression(MLR) is a prediction technique. It provides an explanation to understand the relationship between one continuous dependent/target variable(y) and two or more independent/predictor variables(x). Following is the MLR equation —

y = bo + b1x1 + b2x2 + .. + bnxn, wherein bo is the intercept, b1 is the coefficient of x1, similarly b2 is the coefficient of x2 and so on.

For the model to perform better on unseen or new data the data has been split into training and testing data. To do so import the train_test_split from sklearn. Also define the variables, in this case, we have multiple variables for independent/predictor variables.

z: independent variables

y: dependent variable

z_train, y_train: parts of the available data as the training set

z_test, y_test: parts of available data as the testing set

test_size: percentage of data for testing(30% in this case)

Similar to SLR, create the Linear Regression object using the constructor and fit the model using the fit method. However, the model will be fit on the training set. Additionally, calculate the intercept and coefficients too using the intercept_ and coeff_ methods.

Therefore the Multiple Linear Regression Equation is as follows —

total_litres_of_pure_alcohol = 0.48 + 0.017*wine_servings + 0.018*beer_servings + 0.017 * spirit_servings

Now that the model is set up let’s predict the total_litres_of_pure_alcohol using predict() method on the test subset.

Now let’s evaluate the MLR model on the test subset with the help of R squared which can be calculated using the score() method.

The R squared value is close to one, hence, it can be said that the model is a good fit. In this case, 75% of the variation in total_litres_of_pure_alcohol is explained by the independent variables.

To visually evaluate the model let’s create a distribution plot. A distribution plot counts the predicted value vs the actual values. Use the distplot() method of the seaborn library to create a distribution plot. Pass the actual value(total_litres_of_pure_alcohol) as the parameter and set hist=False since a histogram is not desired. Moreover, the colour and the label can set as well. The predicted value is passed into the second plot which is ‘a’ in this case and ax=ax1 displays the plot on ax1.

It is seen that the predicted values fit closely to the actual values, thus, MLR is a good model.

In conclusion, the prediction for alcohol consumption has been performed using SLR and MLR. Upon evaluation, it is learnt that MLR is the better model as it has a higher R squared value. With the predicted values the company can focus on certain continents and countries to increase its profitability.