MATLAB Answers

How to create a multiple linear regression model

198 views (last 30 days)
Keegan Carvalho
Keegan Carvalho on 27 Jun 2018
Answered: Ameer Hamza on 27 Jun 2018
I wanted to create a regression model in which sl = sst+at+vlm (Column headings in the csv file). I have attached the csv file and will be grateful if someone could provide me with the code. I wanted to get a formula like y = 1 +ax1 +bx2 +cx3.
Thanks!

  3 Comments

Adam Danz
Adam Danz on 27 Jun 2018
How far have you got in your attempt to solve this? Have you written any code that starts this task? There's a TON of help if you 'google' ('how to create multiple linear regression models in matlab') to get you started. Matlab also has a lot of literature on multiple linear regression.
Keegan Carvalho
Keegan Carvalho on 27 Jun 2018
Actually i tried it but I'm a little confused on the result.
Estimate SE tStat pValue
________ _______ _______ ________
(Intercept) 8477.6 4797.1 1.7672 0.098969
x1 39.134 46.506 0.84148 0.41422
x2 128.31 80.696 1.59 0.13415
x3 -0.86657 0.61528 -1.4084 0.18083
This is what I got.
I used the following code:
data=readtable('seyreg.csv')
X = [data.sst,data.at,data.vlm]
lm = fitlm(X,data.sl,'linear')
Is this correct?
Adam Danz
Adam Danz on 27 Jun 2018
What are you confused about and is what correct? Your code along with your data should produce those results. Interpreting those results is a whole other story that can't be told without knowing more background information. Is there something unexpected in the output?
If you're having trouble understanding the output table or are uncertain of the inputs to the model, read the matlab literature on fitlm .
If your uncertainty is due to not understanding linear models it would be helpful to read a chapter about them or watch some introductory videos.
If there's a more specific question about the matlab code or function, people may be able to help more.

Sign in to comment.

Answers (1)

Ameer Hamza
Ameer Hamza on 27 Jun 2018
Your use of fitlm() is correct. Alternatively, if you don't need all the extra information provided by fitlm() and speed is a concern then you can use MATLAB mldivide (\.) to solve it more efficiently.
data = readtable('seyreg.csv');
X = [data.sst data.at data.vlm ones(size(data.sst))];
Y = data.sl;
coefficient = X\Y;
the coefficient are in same order as the column of X.

  0 Comments

Sign in to comment.

Tags


Translated by