proc (4)=fe_2w(x,y,t,n);
/*
fe_2w.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,Q;
local y_tw,x_tw,res,k;
local b,se,t_val,df,pv;
local Z,mask,fmat,i;
local name;

Q=eye(n).*.eye(t)-eye(n).*.(ones(t,t)/t)-(ones(n,n)/n).*.eye(t)+(ones(n,n)/n).*.(ones(t,t)/t);
k=cols(x);
x_tw=Q*x;
y_tw=Q*y;
b=y_tw/x_tw;
res=y_tw-x_tw*b;
se=sqrt(diag(res'*res/(n*t-n-t-k+1)*inv(x_tw'*x_tw)));
t_val=b./se;
df=rows(y)-rows(b);
pv=2*cdftc(abs(t_val),df);

mask = ones(1,5); mask[1]=0; /*zero for character output and 1 for numeric output */
fmat=zeros(5,3); fmat[1,.]="*.*s"~8~8;
for i (2,5,1);
fmat[i,.]="*.*lf"~12~3; 
endfor;

name=names[2:k+1,1];

print "****************** Two-way Fixed effects model *****************";
print "Variable" "        Coef  " "   Std err " "    t-value   " "  p-value     ";
print "********" "        ****  " "   ******* " "    *******   " "  *******     ";
z=printfm(name~b~se~t_val~pv,mask,fmat);
print "****************************************************************";


retp(b,se,t_val,pv);
endp;