problem with newey west algorithm in matlab
Show older comments
Hey, i have a question about a function file from this forum that calculates the newey west standard error of a regression. This is the file:
*_function se = NeweyWest(e,X,L)
% PURPOSE: computes Newey-West adjusted heteroscedastic-serial
% consistent standard errors
%---------------------------------------------------
% where: e = T x n vector of model residuals
% X = T x k matrix of independant variables
% L = lag length to use
%
% se = Newey-West standard errors
%---------------------------------------------------
indexxx = sum(isnan(X),2)==0;
X = X(indexxx,:);
e = e(indexxx,:);
[N,k] = size(X);
k = k+1;
X = [ones(N,1),X];
if nargin < 3
% Newey-West (1994) plug-in procedure
L = floor(4*((N/100)^(2/9)));
end
Q = 0;
for l = 0:L
w_l = 1-l/(L+1);
for t = l+1:N
if (l==0) % This calculates the S_0 portion
Q = Q + e(t) ^2 * X(t, :)' * X(t,:);
else % This calculates the off-diagonal terms
Q = Q + w_l * e(t) * e(t-l)* ...
(X(t, :)' * X(t-l,:) + X(t-l, :)' * X(t,:));
end
end
end
Q = (1/(N-k)) .*Q;
se = sqrt(diag(N.*((X'*X)\Q/(X'*X))));
end_*
I want to know the newey west standard error of a regression where i regress the equity premium on a constant only. This means my vector X is a 778x1 vector containing ones. However, this results in a standard error of NAN, because (X'*X)\Q/(X'*X) produces a NAN. At this point, X is a 2x2 vector with value 778 and Q is a 2x2 vector containing 4.9*10^-8. Matlab says: "Warning: Matrix is singular to working precision. > In NeweyWest at 43". Does anyone know how i can get newey west standard errors out of this regression?
Donald
Answers (0)
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!