From: Anna Paulsen on
Hi,

I'm having my first stab at Matlab with calculating Newey-West standard errors.

I searched around and found a code, and I need to define the lags (2).
In the code I found the input is "nlags = "lag length to use".

Can someone give me a clue how to go about this? Thx for any help!

I've pasted the code below:

%PURPOSE: computes Newey-West adjusted heteroscedastic-serial
% consistent standard errors (only se's)
%---------------------------------------------------
% USAGE: [V,S] = nwse(e,X,nlag)
% where: e = T x n vector of model residuls
% X = T x k matrix of independ vars
% nlags = lag length to use
%---------------------------------------------------
% RETURNS:
% V is the Newey-West Var-Cov matrix
% S is the spectral density of u = e.*X
% --------------------------------------------------

% written by: Mike Cliff, Purdue Finance, mcliff(a)mgmt.purdue.edu
% CREATED 11/17/00
% MODIFIED 1/23/01 Input e, X separtely; return V, S; df adjustment
% 2/20/01 Allow for system of eqs (multiple e vectors)

if (nargin ~= 3); error('Wrong # of arguments to nwse'); end;

[T,k] = size(X);
n = cols(e);
S = zeros(n*k,n*k);
if k == 1 && X == ones(T,1)
u = e;
else
u = [];
for i = 1:cols(e)
u = [u repmat(e(:,i),1,k).*X];
end
end

for lag = 0:nlags
rho = u(1:T-lag,:)'*u(1+lag:T,:)/(T-k);
if lag >= 1, rho = rho + rho'; end
wt = 1 - lag/(nlags+1);
S = S + wt*rho;
end

V = kron(eye(n),(X'*X/T)\eye(k));
V = V*S*V/T;



end
From: Wayne King on
"Anna Paulsen" <Pekingente82(a)gmail.com> wrote in message <hibb9b$f1p$1(a)fred.mathworks.com>...
> Hi,
>
> I'm having my first stab at Matlab with calculating Newey-West standard errors.
>
> I searched around and found a code, and I need to define the lags (2).
> In the code I found the input is "nlags = "lag length to use".
>
> Can someone give me a clue how to go about this? Thx for any help!
>
> I've pasted the code below:
>
> %PURPOSE: computes Newey-West adjusted heteroscedastic-serial
> % consistent standard errors (only se's)
> %---------------------------------------------------
> % USAGE: [V,S] = nwse(e,X,nlag)
> % where: e = T x n vector of model residuls
> % X = T x k matrix of independ vars
> % nlags = lag length to use
> %---------------------------------------------------
> % RETURNS:
> % V is the Newey-West Var-Cov matrix
> % S is the spectral density of u = e.*X
> % --------------------------------------------------
>
> % written by: Mike Cliff, Purdue Finance, mcliff(a)mgmt.purdue.edu
> % CREATED 11/17/00
> % MODIFIED 1/23/01 Input e, X separtely; return V, S; df adjustment
> % 2/20/01 Allow for system of eqs (multiple e vectors)
>
> if (nargin ~= 3); error('Wrong # of arguments to nwse'); end;
>
> [T,k] = size(X);
> n = cols(e);
> S = zeros(n*k,n*k);
> if k == 1 && X == ones(T,1)
> u = e;
> else
> u = [];
> for i = 1:cols(e)
> u = [u repmat(e(:,i),1,k).*X];
> end
> end
>
> for lag = 0:nlags
> rho = u(1:T-lag,:)'*u(1+lag:T,:)/(T-k);
> if lag >= 1, rho = rho + rho'; end
> wt = 1 - lag/(nlags+1);
> S = S + wt*rho;
> end
>
> V = kron(eye(n),(X'*X/T)\eye(k));
> V = V*S*V/T;
>
>
>
> end

Hi Anna, without running the code, the function expects 3 input arguments, the third one being the maximum lag in the autocorrelation you want to use. There's a typo in the usage section of the help where the input is called nlag and then explained as nlags, which is the way it is in the code. So for the third input argument enter the positive integer you want as the maximum lag.
wayne
From: Anna Paulsen on
"Wayne King" <wmkingty(a)gmail.com> wrote in message <hice8n$2uu$1(a)fred.mathworks.com>...
> "Anna Paulsen" <Pekingente82(a)gmail.com> wrote in message <hibb9b$f1p$1(a)fred.mathworks.com>...
> > Hi,
> >
> > I'm having my first stab at Matlab with calculating Newey-West standard errors.
> >
> > I searched around and found a code, and I need to define the lags (2).
> > In the code I found the input is "nlags = "lag length to use".
> >
> > Can someone give me a clue how to go about this? Thx for any help!
> >
> > I've pasted the code below:
> >
> > %PURPOSE: computes Newey-West adjusted heteroscedastic-serial
> > % consistent standard errors (only se's)
> > %---------------------------------------------------
> > % USAGE: [V,S] = nwse(e,X,nlag)
> > % where: e = T x n vector of model residuls
> > % X = T x k matrix of independ vars
> > % nlags = lag length to use
> > %---------------------------------------------------
> > % RETURNS:
> > % V is the Newey-West Var-Cov matrix
> > % S is the spectral density of u = e.*X
> > % --------------------------------------------------
> >
> > % written by: Mike Cliff, Purdue Finance, mcliff(a)mgmt.purdue.edu
> > % CREATED 11/17/00
> > % MODIFIED 1/23/01 Input e, X separtely; return V, S; df adjustment
> > % 2/20/01 Allow for system of eqs (multiple e vectors)
> >
> > if (nargin ~= 3); error('Wrong # of arguments to nwse'); end;
> >
> > [T,k] = size(X);
> > n = cols(e);
> > S = zeros(n*k,n*k);
> > if k == 1 && X == ones(T,1)
> > u = e;
> > else
> > u = [];
> > for i = 1:cols(e)
> > u = [u repmat(e(:,i),1,k).*X];
> > end
> > end
> >
> > for lag = 0:nlags
> > rho = u(1:T-lag,:)'*u(1+lag:T,:)/(T-k);
> > if lag >= 1, rho = rho + rho'; end
> > wt = 1 - lag/(nlags+1);
> > S = S + wt*rho;
> > end
> >
> > V = kron(eye(n),(X'*X/T)\eye(k));
> > V = V*S*V/T;
> >
> >
> >
> > end
>
> Hi Anna, without running the code, the function expects 3 input arguments, the third one being the maximum lag in the autocorrelation you want to use. There's a typo in the usage section of the help where the input is called nlag and then explained as nlags, which is the way it is in the code. So for the third input argument enter the positive integer you want as the maximum lag.
> wayne

Thanks wayne - that was less complicated that I thought.

I have run into another issue, though: When I run the code I get the following error message which I have trouble interpreting. Can anybody give a hint?

??? Operands to the || and && operators must be convertible to logical scalar values.

Error in ==> nwse at 25
if k == 1 && X == ones(T,1)
From: Wayne King on
"Anna Paulsen" <Pekingduck82(a)gmail.com> wrote in message <hicia2$puv$1(a)fred.mathworks.com>...
> "Wayne King" <wmkingty(a)gmail.com> wrote in message <hice8n$2uu$1(a)fred.mathworks.com>...
> > "Anna Paulsen" <Pekingente82(a)gmail.com> wrote in message <hibb9b$f1p$1(a)fred.mathworks.com>...
> > > Hi,
> > >
> > > I'm having my first stab at Matlab with calculating Newey-West standard errors.
> > >
> > > I searched around and found a code, and I need to define the lags (2).
> > > In the code I found the input is "nlags = "lag length to use".
> > >
> > > Can someone give me a clue how to go about this? Thx for any help!
> > >
> > > I've pasted the code below:
> > >
> > > %PURPOSE: computes Newey-West adjusted heteroscedastic-serial
> > > % consistent standard errors (only se's)
> > > %---------------------------------------------------
> > > % USAGE: [V,S] = nwse(e,X,nlag)
> > > % where: e = T x n vector of model residuls
> > > % X = T x k matrix of independ vars
> > > % nlags = lag length to use
> > > %---------------------------------------------------
> > > % RETURNS:
> > > % V is the Newey-West Var-Cov matrix
> > > % S is the spectral density of u = e.*X
> > > % --------------------------------------------------
> > >
> > > % written by: Mike Cliff, Purdue Finance, mcliff(a)mgmt.purdue.edu
> > > % CREATED 11/17/00
> > > % MODIFIED 1/23/01 Input e, X separtely; return V, S; df adjustment
> > > % 2/20/01 Allow for system of eqs (multiple e vectors)
> > >
> > > if (nargin ~= 3); error('Wrong # of arguments to nwse'); end;
> > >
> > > [T,k] = size(X);
> > > n = cols(e);
> > > S = zeros(n*k,n*k);
> > > if k == 1 && X == ones(T,1)
> > > u = e;
> > > else
> > > u = [];
> > > for i = 1:cols(e)
> > > u = [u repmat(e(:,i),1,k).*X];
> > > end
> > > end
> > >
> > > for lag = 0:nlags
> > > rho = u(1:T-lag,:)'*u(1+lag:T,:)/(T-k);
> > > if lag >= 1, rho = rho + rho'; end
> > > wt = 1 - lag/(nlags+1);
> > > S = S + wt*rho;
> > > end
> > >
> > > V = kron(eye(n),(X'*X/T)\eye(k));
> > > V = V*S*V/T;
> > >
> > >
> > >
> > > end
> >
> > Hi Anna, without running the code, the function expects 3 input arguments, the third one being the maximum lag in the autocorrelation you want to use. There's a typo in the usage section of the help where the input is called nlag and then explained as nlags, which is the way it is in the code. So for the third input argument enter the positive integer you want as the maximum lag.
> > wayne
>
> Thanks wayne - that was less complicated that I thought.
>
> I have run into another issue, though: When I run the code I get the following error message which I have trouble interpreting. Can anybody give a hint?
>
> ??? Operands to the || and && operators must be convertible to logical scalar values.
>
> Error in ==> nwse at 25
> if k == 1 && X == ones(T,1)

Hi Anna, again not my area at all, but are you sure the author wants to use cols()?

at
n = cols(e); and again
at
for i = 1:cols(e)

If I change those two lines to

n = size(e,2);
and
for i = 1:n

The function runs. Again, I'm not sure what it's producing, but hopefully you can make sense of that. Are you inputing the correct size of input arguments, for example, are
e and X matrices with the same number of rows?

The Matlab function cols() works on database tables. Do you have some other function cols() defined locally? If you type

>>which cols

what do you get?


Wayne
From: Oleg Komarov on
You can use my regstats2 fcn (which need Statistics TB):
http://www.mathworks.com/matlabcentral/fileexchange/26169-regstats2

It has Newey-West HAC and also several HC robust statistics.

On line 462 of the fcn the lag is estimated as:
L = round(4*(nobs/100)^(2/9)); % Recommended by Newey West (1987) and implemented by Eviews.
% L = nobs^.25; % Alternatively uncomment this line and comment the previous one; this approach is suggested in footnote by Greene's Econoemtric Analysis page 267 5th ed.

Oleg