proc (4)=fe_1wi(names,x,y,t,n);
/*
fe_1w.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 Z,P,Q,x_w;
local b;
local est_res,se,t_val,pv,df;
local k;
local mask,fmat;

Z=eye(N).*.ones(T,1);
P=Z*inv(Z'*Z)*Z';
Q=eye(N*T)-P;

b=(Q*y)/(Q*x);
k=rows(b);
est_res=(Q*y)-(Q*x)*b;
x_w=Q*x;
se=sqrt(diag((est_res'*est_res)/(n*t-n-k)*inv(x_w'*x_w)));
t_val=b./se;
df=rows(y)-rows(b);
pv=2*cdftc(abs(t_val),df);
names=names[2:rows(b)+1,1];

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;

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

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




proc (4)=fe_1wt(names,x,y,t,n);

local Z,P,Q,x_w;
local b;
local est_res,se,t_val,pv,df;
local k;
local mask,fmat;

P=ones(n,n)/n.*.eye(t); /* Takes the average over time periods, i.e. calculates time-spec eff. */
Q=eye(N*T)-P;

b=(Q*y)/(Q*x);
k=rows(b);
est_res=(Q*y)-(Q*x)*b;
x_w=Q*x;
se=sqrt(diag((est_res'*est_res)/(n*t-t-k)*inv(x_w'*x_w)));
t_val=b./se;
df=rows(y)-rows(b);
pv=2*cdftc(abs(t_val),df);
names=names[2:rows(b)+1,1];

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;

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

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