panelreg3.m (4430B)
1 % panelreg.m function [u,NN,K,stdest,beta,vcv]= 2 % panelreg(y,x,Method,yname,xnames,nnames); 3 % This is the FUNCTION for the panel regressions. 4 % Run the panel regressions in levels including fixed effects: 5 % 6 % y = a_i +b_i*t + Xbeta + epsilon 7 % 8 % and then we can do some hypothesis testing. 9 % 10 % Method is a 2x1 vector Method=[FE Time] 11 % FE=0 ==> No Fixed Effects 12 % FE=1 ==> Fixed Effects 13 % FE=2 ==> F.E. and All of the X coefficients vary. 14 % FE=3 ==> Between Estimator 15 % FE=4 ==> Random Effects--GLS (Theta differenced) 16 % Time=0 ==> No time trends 17 % Time=1 ==> Single time trend 18 % Time=2 ==> Country Specific Time trends (automatically FE) 19 % 20 % Note: If Method is scalar, Time=0 is assumed. Also, the between 21 % (FE=3) and the GLS (FE=4) assume no time trends... 22 23 24 function [u,NN,K,stdest,beta,vcv]=... 25 panelreg(y,x,Method,yname,xnames,nnames); 26 27 if length(Method)==1; 28 Time=0; 29 else; 30 Time=Method(2); 31 end; 32 FE=Method(1); 33 34 % So now we've got our data and the years, feed this into panlag. 35 % Take out means (mean0=1) so that we are running the fixed effects 36 % regression. Then we have to adjust the degrees of freedom. 37 % Notice: PanShape returns the Balanced Panel only. 38 39 [T0,N0]=size(y); % Years x Countries 40 [Tx,NX]=size(x); % Years x Countries*Variables 41 numv=NX/N0; % Number of Variables 42 X=[]; 43 44 % Random Effects: Greene, p. 490 45 % Run the Within and Between regressions and use the output to construct 46 % estimates of theta. Then call Panshape with Theta to 47 % theta-difference the data and then run the regression. 48 49 if FE==4; % Random Effects/GLS 50 51 % Pooled; 52 Meth=0; [uP,NP,KP,sigP,bP,vcvP]=panelreg(y,x,Meth,yname,xnames,nnames); 53 54 % Within; 55 Meth=1; [uW,NW,KW,sigW,bW,vcvW]=panelreg(y,x,Meth,yname,xnames,nnames); 56 Ftest(uP,uW,KW-KP,NW-KW,'Test H0: Fixed Effects are not different'); 57 58 % Between; 59 Meth=3; [uB,NB,KB,sigB,bB,vcvB]=panelreg(y,x,Meth,yname,xnames,nnames); 60 61 T=NW/NB; 62 sig2e=sigW^2; 63 sig2u=sigB^2-sig2e/T; 64 fprintf('Estimated Sig2u: %7.4f\n',sig2u); 65 theta=1-sigW/sqrt(T*sig2u+sig2e); 66 fprintf('Estimated Theta: %7.4f\n',theta); 67 fetime=4; 68 else; 69 fetime=(FE>0)+(Time==2); % Param for panshape 1=demean 2=detrend 70 theta=[]; 71 end; 72 73 if FE==3; fetime=3; end; % Panshape =3 ==> between. 74 [y keeps]=panshape(y,fetime,theta); 75 for i=1:numv; 76 [xx Nx]=panshape(x(:,(i-1)*N0+(1:N0)),fetime,theta); 77 X=[X xx]; 78 keeps=keeps.*Nx; 79 end; 80 NN=sum(keeps); 81 prevest=0; 82 83 data=packr([y X]); 84 y=data(:,1); 85 data(:,1)=[]; 86 NT=length(y); 87 88 disp ' '; 89 fprintf('Observations Kept in the Sample: %5.0f\n',sum(keeps)); 90 say(nnames(keeps,:)); 91 disp ' '; 92 fprintf('Eliminated for NaN Reasons: %5.0f\n',sum(~keeps)); 93 say(nnames(~keeps,:)); 94 disp ' '; 95 96 if FE==0 & Time==0; 97 rhs=[ones(NT,1) data]; 98 indv=['Constant'; xnames]; 99 tle='Panel Estimation--Pooled Regression'; 100 elseif FE==3; 101 rhs=[ones(NT,1) data]; 102 indv=['Constant'; xnames]; 103 tle='Panel Estimation--Between Estimate'; 104 elseif FE==1 & Time==0; 105 rhs=data; 106 indv=xnames; 107 tle='Panel Estimation--Fixed Effects'; 108 prevest=NN; 109 elseif FE==4; 110 rhs=[ones(NT,1)*(1-theta) data]; % Difference the Constant! 111 % rhs=[ones(NT,1) data]; % Difference the Constant! 112 y=y/sigW; 113 rhs=rhs/sigW; % Corrects VCV 114 indv=['Constant'; xnames]; 115 tle='Panel Estimation--Random Effects GLS'; 116 prevest=NN-1; 117 elseif FE==0 & Time==1; 118 commont=kron(ones(NN,1),(1:(NT/NN))'); 119 rhs=[ones(NT,1) commont data]; 120 indv=['Constant'; 'Time '; xnames]; 121 tle='Panel Estimation--No F.E.'; 122 elseif FE==1 & Time==1; 123 commont=kron(ones(NN,1),(1:(NT/NN))'); 124 rhs=[commont data]; 125 indv=['Time '; xnames]; 126 tle='Panel Estimation -- F.E./CommonTrend'; 127 prevest=NN; 128 elseif FE==1 & Time==2; 129 rhs=data; 130 indv=xnames; 131 tle='Panel Estimation -- F.E./CountryTrends'; 132 prevest=2*NN; 133 else; 134 disp 'Not Yet Implemented!!!'; break; 135 end; 136 if exist('yname')==1; depv=yname; else; depv='Y '; end; 137 [u,NN,K,stdest,beta,vcv] =ols(y,rhs,tle,depv,indv,prevest); 138 if FE==4; % Hausman Test, p 495 Greene 139 % vcvW 140 % vcv 141 Kp=K-prevest; 142 Sigma=vcvW-vcv(2:Kp,2:Kp); 143 bdiff=bW-beta(2:Kp); 144 W=bdiff'*inv(Sigma)*bdiff; 145 pval=1-chicdf(W,Kp-1); 146 fprintf('Hausman Test of E(e|X)=0: W=%6.2f P-Value=%4.2f\n',[W pval]); 147 disp ' '; 148 end; 149