What is the difference between the regress function and the fitlm function

24 次查看(过去 30 天)
% X = input data
% Y = outcome
% Using the fitlm command to estiamte the multiple liner regression model
lin_mdl = fitlm(X,Y);
b1 = lin_mdl.Coefficients.Estimate;
% Using the regress command to estiamte the multiple liner regression model
b = regress(Y,X)
b2 = [mean(Y) - b'*mean(X)'; b] %To estimate the intercept term
% Comparing the coefficients
[b1 b2]
The output of this function gives different results. Why is that happening? The intercept and the 13th and 14th rows are different in the two cases.
ans =
17.1356 -0.0000
-1.1637 -1.1637
0.2319 0.2319
-14.1594 -14.1594
-0.3783 -0.3783
-0.1204 -0.1204
1.1688 1.1688
0.2103 0.2103
0.1817 0.1817
-0.7232 -0.7232
0.1832 0.1832
-0.0504 -0.0504
0 17.1356
135.8924 153.0281
39.8538 39.8538
-9.4579 -9.4579
0.0452 0.0452
0.6175 0.6175
0.2658 0.2658
0.2980 0.2980
0.3391 0.3391
-0.3060 -0.3060
-0.3109 -0.3109
0.0031 0.0031
-18.0225 -18.0225
-19.0582 -19.0582
-19.5642 -19.5642
-10.1484 -10.1484
-12.0962 -12.0962
-15.1616 -15.1616
-25.3793 -25.3793
-23.5957 -23.5957
-25.5307 -25.5307
-28.9162 -28.9162
-32.5474 -32.5474
-12.9198 -12.9198
-6.3773 -6.3773
2.7314 2.7314
2.5699 2.5699
8.3264 8.3264
13.9870 13.9870
11.0497 11.0497
-20.8487 -20.8487
-12.7635 -12.7635
-13.2119 -13.2119
-17.0616 -17.0616
-18.2134 -18.2134
-11.9230 -11.9230
-26.3549 -26.3549

回答(2 个)

Tom Lane
Tom Lane 2016-2-22
Take a look at the 12th and 13th columns of X. It looks to me like the 12th may be constant or may differ by a constant from the 13th. The fitlm function has some way of dealing with that, and that resulted in the coefficient of the 12th variable being forced to 0. Your technique essentially forced the intercept to 0.
  1 个评论
Prashanth Ravindran
编辑:Prashanth Ravindran 2016-2-23
Hi Tom,
I realized my error. I was not adding a colomn of 1's before using the regress function. The command regress([ones(size(X,1),1) X],Y) would give the same output as fitlm(X,Y).
No they are not constants. some are continuous variables (2:23) and some are binary variables (actually categorical variables with k levels converted to k-1 binary variables) which are [1, 24:end]. But I have another query about the output given by 'fitlm' or 'regress' and mathematical equation which is explained here http://www.mathworks.com/matlabcentral/answers/269529-why-is-fitlm-or-regess-and-estimation-using-mathematical-equations-giving-different-results. Any help would be very beneficial. Thanks

请先登录,再进行评论。


Myagmarsuren Sanaakhorol
The key difference is intercept: 1. "fitlm(x,y)" function uses intercept by default 2. "regress(y,x)" function uses no intercept by default (you can add intercept by adding "ones" matrix)

Community Treasure Hunt

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

Start Hunting!

Translated by