📄 olspanelkeanej.m
字号:
function results = olsPanelKeaneJ(p,t,y,x);
% PURPOSE: Computes regression with Keane and Runkle (JPE 1998)
% covariance matrix for cross-sectional, fully robust (see
% Woolridge (2002) eqn (7.26)) for within-panel variable
% NOTE: Automatically adds constant
% NOTE: Allows for arbitrary within-panel serial correlation
% and heteroskedasticity; assumes zero cross-sectional
% serial corrlation and constant cross-sectional
% correlation within time period
%---------------------------------------------------
% USAGE: results = olsPanelKeaneJ(p,t,y,x)
% where: p = NTx1 vector of panel variables
% t = NTx1 vector of time variables
% y = NTx1 vector of independent variables
% x = NTxK matrix of regressors (excluding constant)
%---------------------------------------------------
% RETURNS: a structure
% results.meth = 'ols'
% results.beta = bhat (nvar x 1)
% results.tstat = t-stats (nvar x 1)
% results.yhat = yhat (nobs x 1)
% results.resid = residuals (nobs x 1)
% results.sige = e'*e/(n-k) scalar
% results.rsqr = rsquared scalar
% results.rbar = rbar-squared scalar
% results.dw = Durbin-Watson Statistic
% results.nobs = nobs
% results.nvar = nvars
% results.y = y data vector (nobs x 1)
% results.bint = (nvar x2 ) vector with 95% confidence intervals on
%---------------------------------------------------
% Requires ols() function from LeSage Econometrics toolbox
%---------------------------------------------------
% Judson Caskey
% University of Michigan
% jcaskey@umich.edu
%---------------------------------------------------
if size(p,1)~=size(y,1) | size(p,1)~=size(x,1)...
| size(y,1)~=size(x,1) | size(t,1)~=size(y,1) | size(t,1)~=size(x,1)
error('p,t,x and y must have same number of rows');
elseif size(p,2)~=1 | size(y,2)~=1 | size(t,2)~=1
error('p, t and y must be column vectors');
end;
panelitems = unique(p);
timeitems = unique(t);
m = size(panelitems,1);
n = size(x,1);
% Check for duplicate times for a given panel item
ptmp=sortrows([p,t],[1,2]);
if sum(and(ptmp(2:end,1)==ptmp(1:end-1,1),ptmp(2:end,2)==ptmp(1:end-1,2))) > 0
error('Cannot have duplicate panel/time indices');
end;
clear ptmp;
x=[x,ones(size(x,1),1)];
resultstmp = ols(y,x);
x = sortrows([p,t,resultstmp.resid,x],[2,1]);
% Lower triangle (without diagonal) elements give cross-products for t
b=0;
for k=1:1:size(timeitems,1);
etmp = x(x(:,2)==timeitems(k),3);
b = b + 2*sum(sum(tril(etmp*etmp',-1)));
end;
clear etmp;
b = b/size(x,1)/(size(panelitems,1)-1);
% Construct variance matrix with blocks for each time for cross-sectional
varmat=zeros(size(x(:,4:end),2),size(x(:,4:end),2));
for k=1:1:size(timeitems,1);
xtmp=x(x(:,2)==timeitems(k),4:end);
varmat = varmat + xtmp'*(b*ones(size(xtmp,1),size(xtmp,1))...
-b*eye(size(xtmp,1)))*xtmp;
end;
clear xtmp;
% Add variance for within-panel unit
for k=1:1:size(panelitems,1);
xtmp=x(x(:,1)==panelitems(k),4:end);
utmp=x(x(:,1)==panelitems(k),3);
gtmp=utmp'*xtmp;
varmat=varmat+gtmp'*gtmp;
end;
% Remove panel, time and residual variables from x
x = x(:,4:end);
% Add robust variance and replace t-statistics
results=resultstmp;
results = rmfield(results,'bint');
results.varmat=m*n/(m-1)/(n-1)*inv(x'*x)*varmat*inv(x'*x);
results.tstat=results.beta./sqrt(diag(results.varmat));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -