MATLAB回归生成标准误和T-VALUE
function ret = regress222(y, X, stat)
% This function estimates OLS coefficients from a regression of
% y on X. Standard errors are "regular" OLS standard errors.
%
% SYNTAX: ret = regress(y, X, stat)
%
% The value returned (ret) depends on stat:
% 0: [beta, standard errors, t-statistics]
% 1: residuals
% calculate Bhat
b = pinv(X)*y; % inv(X'*X)*X'*y;
[N, k] =size(X);
% calculate residuals
e = y - X * b;
s2 = e' * e/(N - k);
if (stat==1) % return residuals
ret = e;
elseif (stat==0) % return [beta, standard errors, t-statistics]
% Following code borrowed from "Spatial Econometrics" library
if N < 10000
[q r] = qr(X,0);
xpxi = (r'*r)\eye(k);
else % use Cholesky for very large problems
xpxi = (X'*X)\eye(k);
end;
varBhat = s2 * xpxi;
% calculate standard errors and t-stats
se = sqrt(diag(varBhat));
t = b./ se;
ret = [b se t];
end
end
% x=[277,276.8,276.6,276.4,276.2,276,275.8,275.6,275.4,275.2,275,274.8,274.6,274.4,274.2,274,273.8,273.6,273.4,273.2,273,272.8,272.6,272.4,272.2,272,271.8,271.6,271.4,271.2,271,270.8,270.6,270.4,270.2,270,269.8,269.6,269.4,269.2,269;];
% y=[0.15644,0.15706,0.15782,0.15874,0.1598,0.16092,0.16208,0.16318,0.1643,0.16544,0.1666,0.16772,0.16884,0.16992,0.17094,0.1719,0.17276,0.1735,0.17408,0.17444,0.17458,0.17456,0.17438,0.17408,0.17366,0.1732,0.17268,0.17208,0.17134,0.1705,0.16952,0.16846,0.16734,0.1662,0.1651,0.16402,0.16292,0.16188,0.16086,0.15984,0.15886;];
% X = [x; ones(1,length(x))];
% b = regress(y.',X.')
% b2 = regress222(y.',X.',0)
% b3 = regress222(y.',X.',1)
浙公网安备 33010602011771号