Multiple Linear regression to obtain coefficients

I need to perform a multiple linear regression to obtain the coefficients for an equation of the form y=a+bx+cz. a,b, and c being the coefficients and x and z are variables for which I have data: 24 data points. 8 value for x for which I have 3 values of y. Those 3 values correspond each to 1 values of z. So I have 3 values of z. Thus x*z=8*3=24 data points. Here is my code but it's wrong after checking the solution for a specific data point. I am not sure what is wrong. I want to use lsqin in a simple way like this. Any help would be appreciated.
function multiple_linear_regression
z=[0 2 4];
x=[10 12 13 14 16 18 20 23]';
data=[13 11 10.5;
8 12 10;
10 10 9;
9 9 8;
9 7 10;
10 8 9;
8 6 5.8;
5 3.5 2];
y=lsqlin(data,x);
end
Thank you

6 Comments

HUH?
You have listed 3 values of z. You have 8 values of x. Where did the array data come from? What is y?
Somehow I don't think you understand how to do regression, or maybe what it is.
If your goal is to estimate coefficients of [a,b,c] in the model
y = a + b*x + c*z
then you have not provided sufficient information to do so in any meaningful way. It is certainly true that the way you have tried to use lsqlin will not succeed.
So please try explaining your problem more clearly. Provide all of the information.
The data matrix are CO2 concentrations. x is distance and z is contaminant concentration. y would be concentration of CO2 given by an equation of the type:
y = a + b*x + c*z
My goal is to have a predictive equation for other CO2 concentrations. The data matrix are values collected in the field. Hope this is more clear.
My best guess here is that the possible values of x and z are as Stephane has listed them, and data is the 2-dimensional results for the (x,z) combinations. Therefore, the better definition of the variables for input into a typical MATLAB regression routine is
x = [10 12 13 14 16 18 20 23 10 12 13 14 16 18 20 23 10 12 13 14 16 18 20 23 ]';
z = [0 2 4 0 2 4 0 2 4 0 2 4 0 2 4 0 2 4 0 2 4 0 2 4]';
This still leaves a few questions, Stephane. My main one is why did you think that lsqlin is the best function for your regression? You don't mention any other constraint. If you have the Statistics and Machine Learning Toolbox, I would probably just use regress. But I can imagine some possible constraints that might require lsqlin.
It doesn't look like this is working. I only have experience with lsqlin but I am open to any other Matlab functions that could solve for the coefficients a, b, and c. How could regress be implemented in this case? To add extra clarification to my system: I have CO2 concentrations at different locations for three different contaminant concentrations. I am in need of a predictive equation for CO2 concentrations in function of distance and contaminant concentration (so in the form stated above).
So then data is y in the model?
y = a + b*x + c*z
Yes, sorry about the confusion.

Sign in to comment.

 Accepted Answer

Assuming that data is actually y in the model posed...
[xx,zz] = ndgrid(x,z);
abc = [ones(24,1),xx(:),zz(:)]\data(:)
abc =
17.135
-0.52085
-0.24062
It is not a terribly good model, but then your data is a bit noisy given that model.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!