ich habe ein großes Problem mit meinen Standardfehlern (eigentlich kein Matlab spezielles Thema sondern Öko allgemein, aber hier kennen sich ja einige aus).
Und zwar habe ich Paneldaten (verschiedene Länder über 30 Jahre) und möchte nun die Standardfehler zweidimensional Clustern. Nachdem Paper von Cameron, Gelbach und Miller (2010): "Robust inference" berechnet man erst die Sandwich Matrix für die eine Clusterdimension, addiert dann die zweite Clusterdimension hinzu und zieht dann die White-Covariance matrix wegen Doppeltzählungen ab. Leider bekomme ich dort an einigen stellen negative Standardfehler (was lt. des Papers möglich ist). Mich würde interessieren, ob jemand damit schonmal gearbeitet hat und wie man dieses Problem umgehen, korrigieren kann.
Hier mein Code:
% Calculate the coefficient estimate b.
b = inv(X'*X)*X'*y;
% Calculate the regression residuals.
e = y - X*b;
% Calculate the variance robust to clustering on the first dimension % supplied using the function 'singlecluster'.
varBhat = singlecluster(xpxi, X, e, g);
% If two cluster variables are supplied, adjust for the second cluster % dimension. Follow Cameron et al. (2011, p. 241, equation (2.11)). ifnargin == 4
h = varargin{1};
% Add the component attributable to the second cluster dimension.
temp = varBhat + singlecluster(xpxi, X, e, h);
% Subtract the variance attributable to the intersection cluster. % This equals to the White heteroskedasticity-consistent covariance % matrix (see Thompson (2009, p. 1).
White = N/(N-k)*xpxi*(X'*diag(e.^2)*X)*xpxi;
varBhat = temp - White;
function varB = singlecluster(xpxi, X, e, g) % Function that handles a single cluster dimension.
% Identify unique clusters.
G = unique(g, 'rows'); % returns the different values in the cluster % vector.
M = size(G,1); % returns the amount of clusters. [N,k] = size(X);
% Now calculate the "meat" of the sandwich variance estimator. % See "Robust Inference with Multi-way Clustering" % by Cameron et al. (2011, p. 240, equation (2.4)).
mid = 0; % the variance estimators for each cluster are subsequently % added.
% This code handles clusters defined by more than one variable. It % treats observations having the same value for ALL cluster % variables as members of the same cluster. for i=1:M % loop over all entries in G (number of clusters in % the respective dimension)
test=(1:size(g,1))'; % returns the values from 1 to the number of % observations. for j=1:size(G,2)% for clustering in one dimension (g or h) % size(G,2)=1 for the intersection (gh) size(G,2)=2.
test2 = find(g(:,j)==G(i,j));
test=intersect(test,test2); % returns the values common to test end
X_g = X(test,:);% chooses the rows in X that belong to a certain % cluster (e.g. country).
e_g = e(test,:);
mid = mid + X_g'*(e_g)*(e_g)'*X_g;
% see Cameron et al. (2011, p. 240), equation % (2.4). The sum of the individual sandwich variances % is computed as "mid" is taken as the basis for % the next round of the loop. end;
% Calculate cluster-robust variance matrix estimate
q_c = (N-1)/(N-k)*M/(M-1); % see the modification in Cameron et al.
%(2011, p. 241)for smaller samples.
Du kannst Beiträge in dieses Forum schreiben. Du kannst auf Beiträge in diesem Forum antworten. Du kannst deine Beiträge in diesem Forum nicht bearbeiten. Du kannst deine Beiträge in diesem Forum nicht löschen. Du kannst an Umfragen in diesem Forum nicht mitmachen. Du kannst Dateien in diesem Forum posten Du kannst Dateien in diesem Forum herunterladen
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.