I'm trying to implement panel regression in Matlab (fixed effects), and I found this link on the web (
http://www.mathworks.com.au/help/stats/panel-analysis-with-panel-corrected-standard-errors.html), which I'm trying to extend to the case where there are more than 1 independent variable.
To this end, I've come thus far to this code:
function [Coef,CovY,Res,CovB] = FEPanelReg(x,y,ID,TIME)
% FEPanelReg works for a strictly balanced panel dataset
% Inside FEPanelReg is called MVREGRESS, which treats NaNs as missing values
% CovY is the variance-covariance matrix of y
% CovB is the variance-covariance matrix of the coefficients (useful for t-testing)
% ID has to be a series of continual numbers for N entities (1,2,3,...,N)
% TIME has to a series of continual numbers for the time length of T (1,2,...,T)
% The size of y is N*T-by-1
% The size of x is N*T-by-K
unique_ID = unique(ID); N = length(unique_ID);
unique_TIME = unique(TIME); T = length(unique_TIME);
Y = reshape(y,N,T);
NT = N*T;
X = cell(T,1);
for i = 1:T
x0 = zeros(N,T);
x0(:,i) = 1;
X{i} = [x0,x(i:T:NT,:)];
end
[Coef,CovY,Res,CovB] = mvregress(X,Y,'algorithm','cwls');
Then I got these errors:
Error using statcheckmvnr (line 32)
Invalid number of cell array elements - should be either NumSamples or 1.
Error in mvregress (line 230)
[NumSamples, NumSeries, NumParams, Data, Design, goodrows] = ...
Error in FEPanelReg (line 22)
[Coef,CovY,Res,CovB] = mvregress(X,Y,'algorithm','cwls');
I know MVREGRESS can take on a cell array, which enables itself to estimate a regression model on a panel (longitudinal) dataset, but I don't know what to fix here to make it happen. Please help!
P.S. if I'm not wrong, I guess the link contains a bit of errors. In the link it specifies K = 7 for 6 intercept terms and a slope, while it earlier says "yij denote the response for city j = 1,...,d, in year i = 1,...,n. Similarly, xij is the corresponding value of the predictor variable. In this example, n = 6 and d = 8." Then there should be 8 intercepts and one slope (since there is only one independent variable included), leading to K = 9. However, in the code posted below, copied from the link, you will find the for-loop runs i from 1 to n (the number of years, which is equal to 6) and x0 is set up as a 8-by-6 matrix, corresponding with the number of cities (8) and the number of years (6).
K = 7; N = n*d;
X = cell(n,1);
for i = 1:n
x0 = zeros(d,K-1);
x0(:,i) = 1;
X{i} = [x0,x(i:n:N)];
end
If this is wrong, I call for a fix; if not, please enlighten me where I'm wrong.
Jason