Can anyone explain why I am getting different answers for the confidence limits for the slope of a linear regression when I use polyfit and polyparci compared with using fitlm and coefCI. For example the following code generates some linearly correlated data with added noise, then does the least squares fit directly, using polyfit and using fitlm, extracting the key items of data at each step:

clear variables

x = (0:10)';

Y = 3.5*x + (((rand(size(x))-0.5)/3).*x);

X = [ones(size(Y)), x];

B1 = X\Y;

Ycalc = X*B1;

R21 = 1 - sum((Y - Ycalc).^2)/sum((Y - mean(Y)).^2);

R2a1 = 1 - ((1-R21)*(length(Y)-1)/(length(Y)-length(B1)));

clear X Ycalc

[p,S] = polyfit(x,Y,1);

B2 = fliplr(p)';

coef = corrcoef(x,Y);

R22 = coef(1,2)^2;

R2a2 = 1 - ((1-R22)*(length(Y)-1)/(length(Y)-length(B2)));

ci2 = polyparci(p,S,0.95);

clear p S coef

mdl = fitlm(x,Y,'y ~ x1');

B3 = mdl.Coefficients{:,1};

R23 = mdl.Rsquared.Ordinary;

R2a3 = mdl.Rsquared.Adjusted;

ci3 = coefCI(mdl,0.05);

ci3 = fliplr(ci3');

clear mdl

As one would expect, all of the approaches produce the same regression coefficients, R-squared and adjusted R-squared values. However, the confidence intervals generated by polyparci and coefCI are different. In all cases I have tried, the range of the confidence limits returned by coefCI is wider than that from polyparci.

Can anyone explain why the methods produce different results?

Thanks, Brian

## 0 Comments

Sign in to comment.