Multiple Linear Regression in MATLAB: A Comprehensive Guide

Multiple linear regression is a powerful statistical technique used to model the relationship between multiple independent variables and a dependent variable. MATLAB provides robust tools and functions to perform multiple linear regression analysis efficiently. This tutorial will guide you through the process of implementing multiple linear regression using MATLAB, including loading and preprocessing data, building the regression model, interpreting results, and making predictions.
Prerequisites
To follow along with this tutorial, you should have a basic understanding of MATLAB programming and familiarity with linear regression concepts. Interested readers can refer to the tutorials on MATLAB mentioned at the end of this tutorial to understand the basics of MATLAB programming language.
Dataset
MATLAB provides several built-in datasets that you can use for multiple linear regression analysis. One such dataset is the “carsmall” dataset, which contains information about various car models.
Interested readers can read the related tutorials on simple linear regression from the link mentioned below:
Here’s a step-by-step guide to performing multiple linear regression on the “carsmall” dataset in MATLAB:
Load the “carsmall” dataset
The load carsmall command loads the built-in "carsmall" dataset into the MATLAB workspace. This dataset contains information about various car models.
load carsmall
Create predictors and response variables
Now, we create a matrix X to store the independent variables (predictors) and a vector Y to store the dependent variable (response).
X = [Weight, Horsepower, MPG]; Y = MPGCity;
In this step, we create a matrix X to store the independent variables or predictors, which include the car's weight, horsepower, and MPG (miles per gallon). We also create a vector Y to store the dependent variable or response, which represents the MPG in the city.
Split the dataset into training and testing sets
To evaluate the performance of our regression model, we split the dataset into training and testing sets. The cvpartition function creates a partition object, cv, which holds the indices for the training and testing subsets. We specify a holdout method with a 30% testing set size. The training set is used to train the regression model, and the testing set is used to evaluate its performance.
% Split the dataset into training and testing sets
cv = cvpartition(size(X, 1), 'HoldOut', 0.3);
trainX = X(training(cv), :);
trainY = Y(training(cv));
testX = X(test(cv), :);
testY = Y(test(cv));Build the multiple linear regression model
Using the fitlm function, we build a multiple linear regression model. We provide the training data trainX (predictors) and trainY (response) as inputs to the function. The model is fitted to the training data.
model = fitlm(trainX, trainY);
Evaluate the model
After fitting the model, we can evaluate its performance using various metrics. We obtain the regression coefficients, R-squared value, and p-values. The Coefficients property of the model object provides the coefficients, Rsquared.Ordinary gives the R-squared value, and pValue provides the p-values associated with each predictor.
% Evaluate the model coefficients = model.Coefficients; rsquared = model.Rsquared.Ordinary; pvalues = model.Coefficients.pValue;
Make predictions on the test set
To assess how well the model generalizes to unseen data, we make predictions on the test set using the predict function. We pass the trained model and the test data testX as inputs. The function returns predicted MPG values corresponding to the test data.
predictions = predict(model, testX);
Analyze the results and perform further analysis as required
This code uses the “carsmall” dataset, splits it into training and testing sets, builds a multiple linear regression model, evaluates the model using coefficients, R-squared values, and p-values, and finally makes predictions on the test set.
Feel free to modify the code and incorporate additional analysis or visualization techniques to suit your specific requirements and gain deeper insights from the multiple linear regression analysis.
Conclusion
Multiple linear regression is a valuable tool for understanding the relationships between multiple variables and making predictions. In this tutorial, you learned how to perform multiple linear regression analysis in MATLAB, including loading and preprocessing data, building regression models, evaluating model performance, and making predictions. MATLAB’s built-in functions and tools provide a convenient and efficient environment for conducting advanced statistical analysis.
Remember to refer to MATLAB’s documentation for further details on specific functions and additional options that can enhance your multiple linear regression analysis.
Tutorials on MATLAB basics
Interested readers can read the following tutorials on the basics of MATLAB programming language:




