Matlab

How to Perform Linear Regression Model in MATLAB Using fitlm()

Linear regression is the famous and basic form of predictive analysis that performs the relationship between the response variable as well as the predictive variable. It is used to fit the data model that is linear in the model coefficients. The linear regression is the least square fit that fits both polynomials and lines among the other models.

MATLAB allows us to perform a linear regression model using the built-in fitlm() function. This blog will walk through the steps to perform a linear regression model in MATLAB using the fitlm() function.

Why Do We Perform Linear Regression?

Understanding the relationship between two variables is important and it can be done through linear regression. We can work with linear regression to predict the estimated value of a dependent variable based on the value of one or more dependent variables. It can also help identify which variables are most important which helps us in predicting the value of a dependent variable.

What is the Linear Regression Model?

As discussed earlier, linear regression defines the relationship between response as well as predictive variables. If it defines a relationship between one dependent and one independent variable, its simplest equation is given below:

y = c+b*x

 
Here:

    • y represents the estimated dependent variable.
    • c represents constant value.
    • b represents regression coefficients.
    • x represents the independent variable.

How to Create Linear Regression Model in MATLAB

MATLAB enables us to create a linear regression model using the fitlm() function. This function takes some mandatory and optional inputs and provides us with a linear regression model.

Syntax

In MATLAB, you can use the fitlm() function in the following ways:

mdl = fitlm(tbl)
mdl = fitlm(X,y)
mdl = fitlm(___,modelspec)

 
Here:

The function mdl=fitlm(tbl) generates a linear regression model by fitting the variables of the table or array denoted by tbl. By default, this function takes the last variable as a response variable.

The function mdl=fitlm(X,y) generates a linear regression model of the response variable y by fitting the data set x.

The function mdl = fitlm(___,modelspec) generates a linear regression model using any of the previous syntaxes by defining the model specification using any of the input parameter combinations.

Example 1: How to Create a Linear Regression Model By Fitting a Table

This MATLAB code generates a linear regression model by fitting the given table tbl.

load carsmall
tbl = table(Weight,Acceleration,MPG,'VariableNames',{'Weight','Acceleration','MPG'});
tbl(1:10,:)
lm = fitlm(tbl,'MPG~Weight+Acceleration')

 
The above code will load the dataset carsmall, then it creates a table consisting of variables Weight, Acceleration, MPG. It prints the first 10 rows of the table, then fits a linear regression model to the MPG variable using the Weight and Acceleration variables.

Example 2: How to Create a Linear Regression Model by Fitting the Data Set

The given example generates a linear regression model in MATLAB by fitting the given matrix X.

load carsmall
X = [Weight, Horsepower, Acceleration];
mdl = fitlm(X,MPG)

 
The above code loads the carsmall dataset in MATLAB and creates a matrix having variable Weight, Horsepower, and Acceleration. Then it uses the fitlm() function to fit the linear regression model to the MPG variable using the variable in the X matrix.

Conclusion

A linear regression defines the relationship between the predictive variable as well as the response variable. It is the basic and the most common type of predictive analysis. It is used for fitting the data model that is linear in model coefficients. MATLAB enables us to create a linear regression model using the built-in fitlm() function. This guide has presented a beginner-level implementation of the fitlm() function for creating a linear regression model in MATLAB using some simple examples for better understanding.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.