MATLAB: Least Square regression with QR decomposition vs fitlm: sensible differences in coefficient estimates

fitlmleast squareslinear regressionlinearmodel.fitMATLABprecisionqr decomposition

Hello, I have written a code to calculate the coefficient estimates of a linear model:
X1 = [X ones(size(X,1),1)];
[Q, R] = qr(X1);
Rhat = R(1:size(X1,2), 1:size(X1,2));
z = Q(:,1:size(X1,2))'*y;
coef = Rhat\z;
yresid = y - yfit;
SSresid = sum(yresid.^2);
SStotal = (length(y)-1) * var(y);
Rsq = 1 - SSresid/SStotal;
DFE = size(X,1)-size(CoefEst,1);
Rsq_adj = 1 - SSresid/SStotal * (length(y)-1)/DFE;
RMSE = sqrt(sum(yresid.^2)/DFE);
I get all the values correctly, but when I compare my results with the results from the Matlab linear model fitlm (I am actually using LinearModel.fit for compatibility with Matlab 2012) I see a sensible difference in the numbers, such as:
  • QR decomposition:
  • intercept = -0.178049482
  • coef_1 = 0.766365889
  • coef_2 = -1.614509588
  • coef_3 = -0.810796672
  • fitlm
  • intercept = -0.182752884
  • coef_1 = 0.756872854
  • coef_2 = -1.626579368
  • coef_3 = -0.800464247
The numbers are closed enough to make me think that this is due to an approximation or a different method used by fitlm to compute the coefficient estimates. However, I would really like to get the same results. How can I do it? I'm not allowed to use any Matlab toolbox functions. Thank you.

Best Answer

My guess is your matrix X1 is singular, something I cannot test, since you have not provided X. However, is there a good reason why you did not just use
coef = X1\y;
or
coef = pinv(X1)*y;
Neither of pinv or backslash are MATLAB toolbox functions. In fact, you used backslash when you computed coef anyway. Anyway, if you think they are, qr is also a MATLAB function.