Multiple Linear Regression in R [With Graphs & Examples]
Updated on Feb 10, 2025 | 16 min read | 29.0k views
Share:
For working professionals
For fresh graduates
More
Updated on Feb 10, 2025 | 16 min read | 29.0k views
Share:
Table of Contents
As a data scientist, I often find myself engaged in predictive analysis for various projects. Linear regression, encompassing simple and multiple linear regression, serves as a vital statistical tool for establishing relationships between dependent and independent variables. In the field of data science and analytics, R holds immense importance. Specifically, multiple linear regression in R becomes invaluable when dealing with scenarios where a single response variable (Y) exhibits a linear dependence on multiple predictor variables. Mastering this aspect of R enhances my ability to conduct precise predictive modeling in a variety of projects.
Linear regression models are used to show or predict the relationship between a dependent and an independent variable. When there are two or more independent variables used in the regression analysis, the model is not simply linear but a multiple regression model.
Simple linear regression is used for predicting the value of one variable by us.
Check out our data science courses to upskill yourself. A straight line represents the relationship between the two variables with linear regression.
There is a linear relationship between a dependent variable with two or more independent variables in multiple regression. The relationship can also be non-linear, and the dependent and independent variables will not follow a straight line.
Check out our data science courses to upskill yourself.
Pictorial representation of Multiple linear regression model predictions
Linear and non-linear regression are used to track a response using two or more variables. The non-linear regression is created from assumptions from trial and error and is comparatively difficult to execute.
Multiple linear regression is a statistical analysis technique used to predict a variable’s outcome based on two or more variables. It is an extension of linear regression and also known as multiple regression. The variable to be predicted is the dependent variable, and the variables used to predict the value of the dependent variable are known as independent or explanatory variables.
The multiple linear regression enables analysts to determine the variation of the model and each independent variable’s relative contribution. Multiple regression is of two types, linear and non-linear regression.
Multiple linear regression is one of the data mining methods to determine the relations and concealed patterns among the variables in huge. It is a type of regression method and belongs to predictive mining techniques. It helps to determine the relationship and presume the linearity between predictors and targets. But the relationship may not always be linear. So, it is inevitable to discover a statistical technique that fits the data and helps determine unbiased results.
The multiple linear regression in R is an extended version of linear regression that enables you to know the relationship between two or more variables. On the other hand, linear regression determines the relationship between two variables only. Let’s explore more on the multiple linear regression in R.
The multiple regression with three predictor variables (x) predicting variable y is expressed as the following equation:
y = z0 + z1*x1 + z2*x2 + z3*x3
The “z” values represent the regression weights and are the beta coefficients. They are the association between the predictor variable and the outcome.
We have known the brief about multiple regression and the basic formula. However, there are some assumptions of which the multiple linear regression is based on detailed as below:
i. Relationship Between Dependent And Independent Variables
The dependent variable relates linearly with each independent variable. To check the linear relationships, a scatterplot is created and is observed for the linearity. If the scatterplot relationship is non-linear, then a non-linear regression is performed, or the data is transferred using statistical software.
ii. The Independent Variables Are Not Much Correlated
The data should not display multicollinearity, which happens in case the independent variables are highly correlated to each other. This will create problems in fetching out the specific variable contributing to the variance in the dependent variable.
iii. The Residual Variance is Constant
Multiple linear regression assumes that the remaining variables’ error is similar at each point of the linear model. This is known as homoscedasticity. When the data analysis is done, the standard residuals against the predicted values are plotted to determine if the points are properly distributed across independent variables’ values.
iv. Observation Independence
The observations should be of each other, and the residual values should be independent. The Durbin Watson statistic works best for this.
The method shows values from 0 to 4, where a value between 0 and 2 shows positive autocorrelation, and from 2 to 4 shows negative autocorrelation. The midpoint, a value of 2, shows there is no autocorrelation.
Data Science Advanced Certification, 250+ Hiring Partners, 300+ Hours of Learning, 0% EMI
v. Multivariate Normality
Multivariate normality happens with normally distributed residuals. Moreover, it happens when the independent variables are highly correlated. For this assumption, it is observed how the values of residuals are distributed. It can be tested using two methods,
· A histogram showing a superimposed normal curve and
· The Normal Probability Plot method.
vi. Homoscedasticity
This assumption specifies that the error terms’ variance is identical to the independent variables’ values. It features a plot of standardized residuals vs. predicted values. This plot demonstrates whether points are uniformly distributed over all values of the independent variables.
For this assumption, multiple linear regression needs a minimum of two independent variables. They can be ordinal, nominal, or interval/ratio level variables. Regarding the sample size, the regression analysis needs a minimum of 20 cases per independent variable for the analysis.
Firstly, the multiple linear regression in R needs that the relationship between the dependent and independent variables is linear. Scatterplots help to test the linearity assumption.
Secondly, the multiple linear regression analysis states that the errors between predicted and observed values (i.e., the regressions’ residuals) must be normally distributed. You can check this assumption after looking at a Q-Q-Plot or histogram. Normality can be checked with a goodness of fit test (including the Kolmogorov-Smirnov test). Note that this test should be performed on the residuals themselves.
Multiple linear regression is a very important aspect from an analyst’s point of view. Before looking at the details of how to plot multiple linear regression in R, you must know the instances where multiple linear regression is applied. Here are some of the examples where the concept can be applicable:
i. As the value of the dependent variable is correlated to the independent variables, multiple regression is used to predict the expected yield of a crop at certain rainfall, temperature, and fertilizer level.
ii. Multiple linear regression analysis is also used to predict trends and future values. This is particularly useful to predict the price for gold in the six months from now.
iii. In a particular example where the relationship between the distance covered by an UBER driver and the driver’s age and the number of years of experience of the driver is taken out. In this regression, the dependent variable is the distance covered by the UBER driver. The independent variables are the age of the driver and the number of years of experience in driving.
iv. Another example where multiple regressions analysis is used in finding the relation between the GPA of a class of students and the number of hours they study and the students’ height. The dependent variable in this regression is the GPA, and the independent variables are the number of study hours and the heights of the students.
v. The relation between the salary of a group of employees in an organization and the number of years of experience organization the employees’ age can be determined with a regression analysis. The dependent variable for this regression is the salary, and the independent variables are the experience and age of the employees.
Multiple linear regression in R example can be about the selling price of a house. This price can depend on the location’s desirability, the number of bedrooms and bathrooms, the year when the house was constructed, the square footage area of the lot, and many other factors.
Another multiple linear regression in R example can be the height of a child that depends on the height of the mother and the father, nutrition gained, and environmental factors.
Also Read: 6 Types of Regression Models in Machine Learning You Should Know About
There are many ways multiple linear regression can be executed but is commonly done via statistical software. One of the most used software is R which is free, powerful, and available easily. We will first learn the steps to perform the regression with R, followed by an example of a clear understanding.
We will understand how R is implemented when a survey is conducted at a certain number of places by the public health researchers to gather the data on the population who smoke, who travel to the work, and the people with a heart disease.
The following step-by-step guide helps you to know how to plot multiple linear regression in R:
i. Load the heart.data dataset and run the following code
lm<-lm(heart.disease ~ biking + smoking, data = heart.data)
The data set heart. Data calculates the effect of the independent variables biking and smoking on the dependent variable heart disease using ‘lm()’ (the equation for the linear model).
ii. Interpreting Results
use the summary() function to view the results of the model:
summary(heart.disease.lm)
This function puts the most important parameters obtained from the linear model into a table that looks as below:
From this table we can infer:
Row 1 of the coefficients table (Intercept): This is the y-intercept of the regression equation and used to know the estimated intercept to plug in the regression equation and predict the dependent variable values.
heart disease = 15 + (-0.2*biking) + (0.178*smoking) ± e
From the multiple linear regression model output, you can determine the fitted multiple linear regression equation. This equation is useful to make predictions about the mpg value for new observations. You can go through a few metrics as discussed below to evaluate how “good” the multiple regression model in R fits the data:
This metric of multiple regression in R measures the strength of the linear relationship between the response variable and the predictor variables. A multiple R-squared of 1 shows a perfect linear relationship, whereas a multiple R-squared of 0 shows that no linear relationship exists.
Multiple R alternatively denotes the square root of R-squared. It is the variance proportion in the response variable which can be explicated by the predictor variables.
This metric of multiple regression in R calculates the average distance that the observed values fall from the particular regression line.
A model that fulfills the application’s conditions is the minimum requirement. However, you may find different models that fulfil this criterion. So, you may get confused about how to select between various valid models of multiple regression plot in R.
The 3 most common tools to choose a good linear model are:
It is important to note that the first two approaches are suitable for both simple and multiple linear regression; the third one applies to multiple linear regression only.
Here’s a step-by-step explanation of how to improve a multiple linear regression model
The related module explains the stages involved in the decision process of multiple regression plot in R analysis. Based on the plot, the module calculates the regression using dependent and independent variables.
Some Terms Related To Multiple Regression
i. Estimate Column: It is the estimated effect and is also called the regression coefficient or r2 value. The estimates tell that for every one percent increase in biking to work there is an associated 0.2 percent decrease in heart disease, and for every percent increase in smoking there is a .17 percent increase in heart disease.
ii. Std.error: It displays the standard error of the estimate. This is a number that shows variation around the estimates of the regression coefficient.
iii. t Value: It displays the test statistic. It is a t-value from a two-sided t-test.
iv. Pr( > | t | ): It is the p-value which shows the probability of occurrence of t-value.
We should include the estimated effect, the standard estimate error, and the p-value.
In the above example, the significant relationships between the frequency of biking to work and heart disease and the frequency of smoking and heart disease were found to be p < 0.001.
The heart disease frequency is decreased by 0.2% (or ± 0.0014) for every 1% increase in biking. The heart disease frequency is increased by 0.178% (or ± 0.0035) for every 1% increase in smoking.
The effects of multiple independent variables on the dependent variable can be shown in a graph. In this, only one independent variable can be plotted on the x-axis.
Multiple Linear Regression: Graphical Representation
Here, the predicted values of the dependent variable (heart disease) across the observed values for the percentage of people biking to work are plotted.
For the effect of smoking on the independent variable, the predicted values are calculated, keeping smoking constant at the minimum, mean, and maximum rates of smoking.
In this post, I’ve taken you through the intricate world of multiple linear regression, aiming to simplify its complexities. I’ve focused on how we apply this method in R to enhance our prediction capabilities. My goal was to demystify this advanced statistical technique, making it accessible and understandable. By breaking down the steps and illustrating how it aids in refining predictions, I hope to have provided you with valuable insights. This journey through multiple linear regression in R is designed to arm you with the knowledge to apply these concepts effectively in your projects, leading to more accurate and reliable outcomes.
If you’re enthusiastic about advancing your journey in data science and expanding your proficiency in R and other languages, I invite you to enroll Advanced Certification Program in Data Science is tailor-made for working professionals, offering over 300 hours of immersive learning and ongoing mentorship to enhance your career in the dynamic field of data science.
Boost your career with our popular Data Science courses! Learn AI, machine learning, and analytics from industry experts.
Stay ahead in data science with our popular articles! Explore insights on AI, machine learning, and analytics
Master top data science skills like machine learning, Python, and data visualization. Stay ahead in the evolving tech landscape!
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources