📄 newey_westreg.m
字号:
function [Coefficients, S_err,R_sq,T_stat,Cov]=newey_westreg(y,x,intercept)
%linear regression based on Newey and west herteroscedasticity and
%autocorrelation consistent covariance
% input:
% y: dependant variable (column vector)
% x: independent variable (matrix with the same rows as y)
%intercept: 0 means not including y-intercept in regression;1 means including
%y-intercept
% output:
% Coefficients: estimated parameters
% S_err: standard deviation of residuals
% R_sq: a value measuring the fitness
% T_sta: t statistics for estimated parameter
% Cov: Newey and west herteroscedasticity and autocorrelation consistent covariance
if intercept==1
x=[ones(length(y),1),x];
end
[Coefficients, S_err, XTXI, R_sq, F_val, Coef_stats, Y_hat, residuals]=mregress(y, x, 0);
K=size(x,2);N=length(y);
a=zeros(K);c=zeros(K);
q=round(N^(1/3));%specify maximum number of lags
for i=1:N
a=a+residuals(i)^2*x(i,:)'*x(i,:);
end
if q>=1
for i=1:q
b=zeros(K);
for j=(i+1):N
b=b+x(j,:)'*residuals(j)*residuals(j-i)*x((j-i),:)+x((j-i),:)'*residuals(j)*residuals(j-i)*x(j,:);
end
c=c+b*(1-i/(q+1));
end
end
Cov=N/(N-K)*inv((x'*x))*(a+c)*inv((x'*x));
T_stat=Coefficients./sqrt(diag(Cov));
for i=1:size(T_stat,1);
for j=1:size(T_stat,2);
T_stat1(i,j)=tcdf(T_stat(i,j),(size(x,1)-size(x,2)));
end
end
T_stat=[T_stat T_stat1];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -