Cluster Standard Errors with fitlm

I have panel data (county, year) and want to run a regression with individual-specific effects that are uncorrelated (a fixed effects regression in economics parlance). Does fitlm automatically cluster the standard errors? If not, is there a way to do this?

댓글 수: 4

Aditya Patil
Aditya Patil 2021년 7월 15일
Can you elaborate on what you mean by clustering of standard errors? I assume you are using fitlme, and want the errors for all the inputs to be clustered under some condition?
Joshua
Joshua 2021년 7월 15일
Clustered standard errors refers to Cluster Robust Covariance Matrices (see Greene's Econometric Analysis section 11.3.3). The need arises when errors within a group are correlated but the erros between groups are not.
I am using fitlm with a categorical variable. I believe fitlm employs a least squares dummy variable approach.
Aditya Patil
Aditya Patil 2021년 9월 22일
Can you have a look at the examples provided in https://www.mathworks.com/help/stats/fitlme.html and let me know if this serves your usage? By providing random effect as (1 + x | g), you should be able to have correlation within group errors, while errors outside group will be uncorrelated.
Joshua
Joshua 2021년 9월 22일
Fitlme does not provide the option to cluster errors in estimation of the coefficient variance matrix. Nor does it provide the option to return the estimated data covariance matrix, which could be used to cluster the coefficient standard errors.
I wrote a function that estimates the Cluster Robust Variance matrix based the idea that X is 'augmented' prior to input.
Here is a fixed effects estimation. I apologize that it is not well commented.
%%%SCRIPT
%%GENERATE DUMMY MATRIX
id = unique(ID);
for ii = 1:G
D(:,ii) = (ID == id(ii)); %#ok<SAGROW>
end
%AUGMENT MATRIX
Md = eye(N)-((D*inv(D'*D))*D');
%ESTIMATE COEFFICIENTS
b = (inv(X'*Md*X))*(X'*Md*y);
%FIXED EFFECTS ERROR
efe = Md*y-(Md*X*b);
%COEFFIENT VARIANCE
crobust = (G/(G-1))*((N-1)/(N-G-K)); %correction
Vrobust = CRV(Md*X,efe,ID,crobust);
%FUNCTION
function V = CRV(X,e,ID,c)
if nargin<4
[N,K] = size(X); G = numel(unique(ID)); c = (G/(G-1))*((N-1)/(N-K));
end
if numel(c)>1
error('correction is not a scalar value');
end
%CLUSTER ROBUST VARIANCE MATRIX
g = unique(ID); G = numel(g);
%initialize 'Meat' matrix
M = 0;
for ii = 1:G
selvec = g(ii) == ID;
wi = e(selvec);
M = M+X(selvec,:)'*(wi*wi')*X(selvec,:);
end
V = c*inv(X'*X)*M*inv(X'*X);

댓글을 달려면 로그인하십시오.

답변 (1개)

Aditya Patil
Aditya Patil 2021년 7월 16일

1 개 추천

Currently, clustered standard errors is not supported in Statistics and Machine Learning Toolbox. I have brought the request to the notice of concerned developers.

카테고리

도움말 센터File Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

질문:

2021년 6월 17일

댓글:

2021년 9월 22일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by