newey_westreg.asv

来自「时间序列分析中常用到的matlab代码」· ASV 代码 · 共 37 行

ASV
37
字号
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));
T_stat(:,(size(T_stat,2)+1):size(T_stat,2)*2)=tcdf(T_stat,size(y,1)-size(x,2))

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?