Query regarding fama macbeth regression
2 views (last 30 days)
Show older comments
Dear all,
I need to run FamaBeth regressions.
My Y is a T*N matrix, where T is the number of periods and N the number of firms.
My X is also T*N. Hence, I only have one predictor for each regression.
In the first stage of the so-called FamaMacBeth regression, I must run, for each firm, a time series regression of the dependent variable on the predictor. Hence, I am to run N regressions in the first stage. Then, I store the N estimated coefficients in a N*1 vector called "beta".
In the second stage, I must run, for each time period t, a cross-sectional of the dependent variable on the betas found in the first stage. I thus get T coefficients which I store in a T*1 vector called "lambda".
In a third stage, I simply compute the mean of the vector lambda and the standard errors.
The problem is that my matrices Y and X both contain NaNs. Hence, I try to account for them by, for each of the T*N regressions I ran, deleting all rows for which a NaN appears in the relevant subvector of the Y and X matrices (relavant for each regression; as each regression uses different parts of Y and X matrices).
The problem is that I keep on getting these messages
"> In famamacbeth at 37 Warning: Rank deficient, rank = 1, tol = 1.1538e-015. "
for both the estimation of the beta coefficients and the lambda coefficients.
When I run loops separately, I am able to get each coefficient (associated with that loop) without receiving any message, though.. which kind of puzzles me since the regression should be exactly the same.
I am not sure what the problem is, as the code makes perfect sense in my mind! Hope someone can enlighten me!
function [lambda beta lambdaFinal lambdaStd] = famamacbeth(ymat,xmat)
%preallocate memory
[T N] = size(ymat);
alpha1 = NaN(N,1);
beta = NaN(N,1);
alpha2 = NaN(T,1);
lambda = NaN(T,1);
%first stage
for n = 1:N %here I run a time series regression for each firm
%I extract the relevant column(firm)
localX = xmat(:,n);
localY = ymat(:,n);
%I get rid of the nans here
index1 = isnan(localY);
index2 = isnan(localX);
index = index1 + index2;
index = ~logical(index);
localX = localX(index);
localY = localY(index);
%I run the regression
R = numel(localX);
localX = horzcat(ones(R,1),localX); %add intercept
alphaBeta = localX\localY;
alphaBeta = alphaBeta';
alpha1(n,1) = alphaBeta(1,1);
beta(n,1) = alphaBeta(1,2);
end
%second stage
Ylocal = ymat';
for t = 1 : T %here I run a cross-sectional regression for each time period t
y = Ylocal(:,t);
localX = beta;
index1 = isnan(y);
index2 = isnan(localX);
index = index1 + index2;
index = ~logical(index);
localX = localX(index);
y = y(index);
L = numel(localX);
localX = horzcat(ones(L,1),localX);
alphaLambda = localX\y;
alphaLambda = alphaLambda';
alpha2(t,1) = alphaLambda(1,1);
lambda(t,1) = alphaLambda(1,2);
end
%"third stage"
lambdaFinal = mean(lambda);
lambdaStd = sum((lambda-lambdaFinal).^2)/(T^2);
end
0 Comments
Answers (0)
See Also
Categories
Find more on Descriptive Statistics 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!