MATLAB: Multivariate fit with two unknown coefficients and known intercepts

linear fitmultivariateregression

I have an equation in the form: y = A + B*x1 + C*x2
I have 740 groups of values for y, A, x1, and x2 and want to do a multivariate linear fit to estimate B and C.
Could someone help me out with which function to use and how to set it up? Thank you!

Best Answer

Using the modeling tools you know, this is correct:
% some random data
x1 = randn(100,1);
x2 = randn(100,1);
Y = x1 + 2*x2 + 1 +randn(size(x1))/100;
Y1 = Y-1;
T = table(Y,x1,x2);
lm = fitlm(T,'Y ~ x1 + x2 - 1')
lm =
Linear regression model:
Y ~ x1 + x2
Estimated Coefficients:
Estimate SE tStat pValue
________ ________ ______ __________
x1 0.799 0.10355 7.7158 1.0195e-11
x2 1.8409 0.095852 19.206 5.4359e-35
Number of observations: 100, Error degrees of freedom: 98
Root Mean Squared Error: 0.977
Effectively, subtract 1 from Y, then fit a model with no intercept at all. To use the model, just add 1 back into your prediction at the end. So you create a model with no constant term at all, and then put it back in when you use the model.
If you have other tools available, for example, LSQLIN from the optimization toolbox, there are other ways you can do it. Or you can download my own LSE from the file exchange . It can constrain a parameter to a specific given value.
coefs = lse([ones(size(x1)),x1,x2],Y,[1 0 0],1)
coefs =
1
1.0009
1.998
Using lsqlin, I would do this:
lsqlin([ones(size(x1)),x1,x2],Y,[],[],[1 0 0],1)
Minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
ans =
1
1.0009
1.998
In either of the latter two cases, you get the coefficients as just a vector, with less additional information that the stats toolbox gives you.
Related Question