How to Perform Multiple Linear Regression in R

Spread the love

Multiple linear regression is an extension of simple linear regression that allows for the prediction of a dependent variable based on multiple independent variables. This technique forms the bedrock of many statistical modeling and analysis undertakings. In this extensive guide, we’ll explore the steps to perform and interpret multiple linear regression in R.

1. Understanding Multiple Linear Regression

Multiple linear regression attempts to model the relationship between two or more independent variables and a response by fitting a linear equation to observed data. The steps to perform multiple linear regression are almost identical to those of simple linear regression. The main difference lies in the number of independent variables.

2. Setting Up the R Environment

Ensure you have R and optionally RStudio installed.

Step 1: Install Required Packages


Step 2: Load Necessary Libraries


3. Performing Multiple Linear Regression

We will continue using the mtcars dataset in R for illustration.

Step 1: Data Exploration

Get a feel for the data:


We’ll predict mpg (miles per gallon) using wt (weight of the car), hp (horsepower), and drat (rear axle ratio) as predictors.

Step 2: Model Fitting

Fit the multiple regression model:

model_multi <- lm(mpg ~ wt + hp + drat, data = mtcars)

The summary() function offers detailed insights, from coefficients to R-squared values and significance levels.

4. Model Interpretation

  • Coefficients: Represent how much the mean of the dependent variable changes given a one-unit shift in the independent variable.
  • R-squared: Explains the proportion of variance in the dependent variable that’s explained by independent variables. A value closer to 1 indicates a better model fit.
  • Adjusted R-squared: Provides a more accurate representation by considering the number of predictors in the model.
  • p-value: Indicates the significance of each coefficient. A low p-value (typically ≤ 0.05) suggests that the predictor is significant.

5. Checking Assumptions of Multiple Linear Regression

Several critical assumptions must hold true for linear regression models:

1. Linearity: The relationship between predictors and the response variable should be linear. Residual plots can be handy here:


2. Independence of Residuals: Residuals should be independent. This is a crucial assumption, especially for time series data.

3. Homoscedasticity: The residuals’ variances should remain constant across the data. You can visually inspect this with residual plots.

4. Normality of Residuals: Residuals should follow a normal distribution. Q-Q plots can be useful:

qqPlot(model_multi, main="Q-Q Plot")

5. No Multicollinearity: Predictors shouldn’t be highly correlated. The Variance Inflation Factor (VIF) can help diagnose this:


A VIF value greater than 5-10 suggests problematic multicollinearity.

6. Refining the Model

If the model violates assumptions or isn’t satisfactory, consider:

  • Variable Transformation: Log or square root transformations can sometimes help linearize relationships.
  • Adding Interaction Terms: If the effect of one variable depends on another.
  • Variable Selection: Consider techniques like forward selection, backward elimination, or stepwise regression to select meaningful variables.

7. Making Predictions

To predict new data:

new_data <- data.frame(wt = c(2.5, 3.5), hp = c(100, 150), drat = c(3.0, 3.5))
predictions <- predict(model_multi, new_data)

8. Model Validation

Cross-validation is a technique to validate the model’s performance on unseen data. This ensures that our model isn’t overfitting the training data.

trainIndex <- createDataPartition(mtcars$mpg, p = 0.7, list = FALSE)
mtcarsTrain <- mtcars[trainIndex,]
mtcarsTest <- mtcars[-trainIndex,]

model_multi_train <- lm(mpg ~ wt + hp + drat, data = mtcarsTrain)
predictions_test <- predict(model_multi_train, mtcarsTest)

RMSE = sqrt(mean((predictions_test - mtcarsTest$mpg)^2))

9. Conclusion

Multiple linear regression forms the cornerstone of many statistical endeavors. It allows for the modeling of complex relationships between a response and multiple predictors. By using R, a versatile tool for statistical analysis, practitioners can develop, refine, and interpret multiple regression models with ease. Always ensure to validate your model and check its assumptions to maintain its reliability and validity.

Posted in RTagged

Leave a Reply