proc (0)=level_fe(x,y,n,t);
/*
level_fe.src is written by Kristian Jönsson (November 12, 2002), Department of Economics, Lund University.
Contact info:  kristian.jonsson@nek.lu.se

See separate documentation and 'Econometric Analysis of Panel Data' 2nd edition, Baltagi (2001).

Note:
This code can be used freely as long as proper reference is given.
No performance guarantee is made.
Bugreports are welcome.
*/
local P_tw,P_mu,P_lambda;
local Q_tw,Q_mu,Q_lambda;
local res_tw,res_ow_t,res_ow_i,res_pool;
local rss_tw,rss_pool,rss_ow_t,rss_ow_i;
local x_tw,y_tw,x_ow_t,y_ow_t,x_ow_i,y_ow_i;
local rss,df;
local F_stat,pv;

P_mu=eye(n).*.ones(t,t)/t;     /* Time average for different individuals */
P_lambda=ones(n,n)/n.*.eye(t);  /* Individual average for different time-periods */ 

Q_mu=eye(n*t)-P_mu; /* Sweeps the time-average of different indivuduals */
Q_lambda=eye(n*t)-P_lambda; /* Sweeps the indididual-average of different time-units */

Q_tw=eye(n).*.eye(t)-eye(n).*.(ones(t,t)/t)-(ones(n,n)/n).*.eye(t)+(ones(n,n)/n).*.(ones(t,t)/t);

/* Two-way fixed effects model */
x_tw=Q_tw*x;
y_tw=Q_tw*y;
res_tw=y_tw-x_tw*(y_tw/x_tw);
rss_tw=res_tw'*res_tw;

/* One way model with individual spec. FE */
x_ow_i=Q_mu*x;
y_ow_i=Q_mu*y;
res_ow_i=y_ow_i-x_ow_i*(y_ow_i/x_ow_i);
rss_ow_i=res_ow_i'*res_ow_i;

/* One way model with individual spec. FE */
x_ow_t=Q_lambda*x;
y_ow_t=Q_lambda*y;
res_ow_t=y_ow_t-x_ow_t*(y_ow_t/x_ow_t);
rss_ow_t=res_ow_t'*res_ow_t;

/* Pooled model with an intertcept */
res_pool=y-(ones(n*t,1)~x)*(y/(ones(n*t,1)~x));
rss_pool=res_pool'*res_pool;

/* Stack the quantities into vectors */
df=zeros(4,1);
rss=zeros(4,1);
rss[1]=rss_tw; df[1]=n*t-n-t-cols(x);
rss[2]=rss_ow_i; df[2]=n*t-n-cols(x);
rss[3]=rss_ow_t; df[3]=n*t-t-cols(x);
rss[4]=rss_pool; df[4]=n*t-cols(x)-1;

print "********************************************************************";
print "***************************Chow test output***********************";
print "********************************************************************";
print "F-test for significance of fixed effects";
print "-------------------------------------------------------------------------";
print "Null hypothesis: Pooled model";
print "Alternative hyp : Two-way fixed effects model";
print "Exclusion of both time and individual FE";
F_stat=((rss[4]-rss[1])/(df[4]-df[1]))/(rss[1]/df[1]);
pv=cdffc(F_stat,df[4]-df[1],df[1]);
format /m1 /rd 8,4;
print "F_statistic:  " F_stat;
print "p-value:       " pv;
print "------------------------------------------------------------------------";
print "Null hypothesis: One-way individual FE   ";
print "Alternative hyp : Two-way fixed effects model";
print "Exclusion of time FE";
F_stat=((rss[2]-rss[1])/(df[2]-df[1]))/(rss[1]/df[1]);
pv=cdffc(F_stat,df[2]-df[1],df[1]);
print "F_statistic:  " F_stat;
print "p-value:       " pv;
print "-------------------------------------------------------------------------";
print "Null hypothesis: One-way time FE   ";
print "Alternative hyp : Two-way fixed effects model";
print "Exclusion of individual FE";
F_stat=((rss[3]-rss[1])/(df[3]-df[1]))/(rss[1]/df[1]);
pv=cdffc(F_stat,df[3]-df[1],df[1]);
print "F_statistic:  " F_stat;
print "p-value:       " pv;
print "-------------------------------------------------------------------------";
print "Null hypothesis: Pooled model   ";
print "Alternative hyp : One-way individual FE model";
print "Exclusion of individual FE given that time FE is zero";
F_stat=((rss[4]-rss[2])/(df[4]-df[2]))/(rss[2]/df[2]);
pv=cdffc(F_stat,df[4]-df[2],df[2]);
print "F_statistic:  " F_stat;
print "p-value:       " pv;
print "-------------------------------------------------------------------------";
print "Null hypothesis: Pooled model   ";
print "Alternative hyp : One-way time FE model";
print "Exclusion of time FE given that individual FE is zero";
F_stat=((rss[4]-rss[3])/(df[4]-df[3]))/(rss[3]/df[3]);
pv=cdffc(F_stat,df[4]-df[3],df[3]);
print "F_statistic:  " F_stat;
print "p-value:       " pv;
print "********************************************************************";
print "********************************************************************";

retp;
endp;