proc (0)=mixed(names,x,y,t,n,theta,re_part,sigma_v,sigma_1);
/*
mixed.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,P_re;
local x_org,y_org; /* Original x and y */
local x_w,y_w;
local beta,se,res,t_val,k,df,pv;
local mask,fmat,z;
local th_method,type_re;
local name;

if re_part==1; /* Individual random effects implies time fixed effect */
 P=ones(n,n)/n.*.eye(t);
 P_re=eye(n).*.ones(t,t)/t;
 Q=eye(n*t)-P;
 type_re="Individual RE / Time FE";
elseif re_part==2; /* Time random effects implies individual fixed effect */
 P=eye(n).*.ones(t,t)/t;
 P_re=ones(n,n)/n.*.eye(t);
 Q=eye(n*t)-P;
 type_re="Time RE / Individual FE";
endif;

th_method= "Wallace & Hussain (1969)" | "Amemiya (1971)" | "Swamy    and Arora (1972)" | "Nerlove (1971)";

k=cols(x);
name=names[2:k+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; print;

print "***********************************************************************************************";
print "******************************Two way mixed RE / FE estimation****************************";
print "**************************Type:  " type_re "*************************************";
print "************************************************************************************************";
print;print;
for i (1,4,1);
y_w=(Q*y)-theta[i]*(P_re*(Q*y));
x_w=(Q*x)-theta[i]*(P_re*(Q*x));
beta=y_w/x_w;
res=y_w-x_w*beta;
/* se=sqrt(diag(res'*res/(n*t-n-k)*inv(x_w'*x_w))); */
se=sigma_v[i]*sqrt(diag(inv(x_w'*x_w))); /* Alternative way to calculate SEs */
t_val=beta./se;
df=rows(y)-rows(beta);
pv=2*cdftc(abs(t_val),df);
print "**************************************************************************";
format/sa/rds 30,-1;
print $ "Weight method: " th_method[i];
format /m1 /rd 8,4; print "Weight (theta): " theta[i];
print "---Variance Components---";
format /m1 /rd 8,4; print "Sigma_1 : " sigma_1[i];
format /m1 /rd 8,4; print "Sigma_v : " sigma_v[i];
print "**************************************************************************";
print "Variable" "        Coef  " "   Std err " "    t-value   " "  p-value     ";
print "********" "        ****  " "   ******* " "    *******   " "  *******     ";
z=printfm(name~beta~se~t_val~pv,mask,fmat);
print "**************************************************************************";
print;print;

endfor;


retp;
endp;
