Effacer les filtres
Effacer les filtres

linear regession with 3 Independent variables

23 vues (au cours des 30 derniers jours)
Isaac
Isaac le 4 Mar 2013
Hi guys,
I have only been using matlab for three weeks and will never look at an excel sheet in the same way.
However, i have been trying to do a multiple regression test and have tried:
y = data3(:,1); x = data3(:,2:3 ); p = polyfit(x,y,1)
which does not work and have also tried: [foo gof] = fit([x1, x2,x3], y, 'poly11')
Just short of going insane, i thought maybe some of you could lend your wisdom.
Truly grateful Isaac

Réponses (2)

Wayne King
Wayne King le 4 Mar 2013
Modifié(e) : Wayne King le 4 Mar 2013
It looks like you are trying to construct a linear model with 2 predictor variables and 1 response variable. In the typical linear regression model you would try to fit a model of the form:
Y = beta0+beta1*X1+beta2*X2
Do you have the Statistics Toolbox installed?
If so use regress()
y = data3(:,1);
X = ones(length(y),3);
X(:,2:3) = data3(:,2:3);
[B,BINT,R,RINT,STATS] = regress(y,X);
The first column of the design matrix, X, is a vector of ones for fitting the constant term in the model.
If you do not have the Statistics Toolbox installed, you can obtain the least squares estimates with
beta = X\y;
The B vector (or beta) vector gives you the coefficients in the model
Y = B(1)+B(2)*X(:,2)+B(3)*X(:,3)

Shashank Prasanna
Shashank Prasanna le 4 Mar 2013
Issac, the simplest way is to use \ as Wayne mentioned:
>> [ones(length(x),1) x]\y
This will give you the three coefficients you want where the first column of ones is for the intercept terms.
There are numerous ways to do this in MATLAB.
If you want to use the curve fitting toolbox:
fit(x,y,'poly11')
You can see that the answers match. You can also use the statistics toolbox or the optimization toolbox to do the same.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by