Clear Filters
Clear Filters

Difference linear regression / linear solver

4 views (last 30 days)
Lisa
Lisa on 26 Jun 2017
Commented: dpb on 27 Jun 2017
Hi I have a theoretical question.
I run a linear regression using fitlm that showed good results. Then, I wanted to introduce some constraints, therefore I applied lsqlin. However, the results using lsqlin were very different compared to fitlm, even if I don't use the constraints.
Could you please explain to me what is the main difference the linear regression and the solver, that may contribute to different results?
Thank you Lisa
  3 Comments
Torsten
Torsten on 26 Jun 2017
To answer your question, we must have more information about your regression problem.
Best wishes
Torsten.
dpb
dpb on 26 Jun 2017
As Torsten says, only way to provide any specific answer would require the code and data. For the no constraints case one should get same result as lsqlin returns x = C\d which is OLS solution.

Sign in to comment.

Answers (1)

dpb
dpb on 26 Jun 2017
Edited: dpb on 26 Jun 2017
As commented above, if given same problem, all return same result--
>> x=1:10;y=x+rand(size(x));
>> b=polyfit(x,y,1)
b =
0.9631 0.7126
>> fit(x.',y.','poly1')
ans =
Linear model Poly1:
ans(x) = p1*x + p2
Coefficients (with 95% confidence bounds):
p1 = 0.9631 (0.8776, 1.049)
p2 = 0.7126 (0.1825, 1.243)
>> [x.' ones(length(x),1)]\y.'
ans =
0.9631
0.7126
>> lsqlin([x.' ones(length(x),1)],y.')
ans =
0.9631
0.7126
>>
As can be seen, all give the same result.
One can only presume perhaps you left out the ones column in the design matrix for the constant term in the lsqlin case?
>> lsqlin(x.',y.') % zero intercept model...
ans =
1.0649
>>
>> [x.' ]\y.'
ans =
1.0649
>>
  2 Comments
Lisa
Lisa on 26 Jun 2017
Thank you very much. That exactly answered my question. I see now that there are the same results when adding the constant term. Another error of mine was that I used the option 'RobusOpt' in fitlm. These two aspects contributed to the different results.
One more question would be now: I wanted to use lsqlin instead of fitlm because of the option to set upper and lower bounds. Hence, would there be an option for lsqlin (or another function) to implement both robust fitting as well we upper and lower bounds?
Thanks
dpb
dpb on 27 Jun 2017
Don't think is a prepackaged routine to do both, no.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!