panshape.m (1802B)
1 % panshape.m function [sy, NN] = panshape(y,mean0,theta,unbal); 2 % 3 % y: A TxN matrix of data (T years N countries) 4 % mean0: 1==> demean 2==> detrend 0==> Nothing 5 % 6 % sy = shaped y 7 % NN = vector of 1=keep 0=missing data for countries 8 % 9 % For unbal~=1 (or omitted), 10 % This procedure ensures balanced panels -- if any observation is 11 % missing, the procedure makes every observation for that country a 12 % missing value (so that at the next stage it will be deleted). 13 % 14 % If unbal==1, then the procedure will return unbalanced panel. 15 % 16 % Notice that the return is stacked and ready to be included 17 % in a regression. 18 % 19 % mean0=1 ==> demean 20 % mean0=2 ==> detrend & demean 21 % mean0=3 ==> return only means for between regression. 22 % mean0=4 ==> theta difference (GLS random effects), incl constant 23 % mean0=33==> return vector of means, with multiple obs per cty 24 % (this is for withbetw.m) 25 26 function [sy, NN] = panshape(y,mean0,theta,unbal); 27 sy =[]; 28 NN=[]; 29 [T N] = size(y); 30 if exist('unbal')~=1; unbal=0; end; 31 32 for i=1:N; % Loop over Countries = N 33 s1 = y(:,i); 34 if (any(isnan(s1))==1) & unbal==0; % Make a Balanced Panel 35 if mean0~=3; 36 s1=ones(T,1)*NaN; 37 else; 38 s1=NaN; 39 end; 40 NN=[NN; 0]; 41 else; 42 s1=packr(s1); 43 if s1~=[]; 44 NN=[NN; 1]; 45 else; 46 NN=[NN; 0]; 47 end; 48 if mean0==1; % demeaning 49 s1=demean(s1); 50 elseif mean0==2; 51 s1=detrend(s1); % detrend (also demeans) 52 elseif mean0==3; % Between Estimator 53 s1=mean(s1); 54 elseif mean0==4; 55 s1=s1-theta*mean(s1); % Random Effects 56 elseif mean0==33; % Means for withbetw 57 s1=mean(s1)*ones(length(s1),1); 58 end; 59 end; 60 sy=[sy; s1]; 61 end; 62 63