proc (0)=ac_res(k,res,n,t,mod);
/*
ac_res.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 LM_3,LM_5;
local pv_LM_5;
local x_tw,y_tw;
local u,u_lag;
local res_mat,res_mat_lag;
local B;
local z;
local Z_mu,P,Q;
local fisher;
local pv_fisher;

u=res;

/* LM_5 statistic */

res_mat=reshape(u,n,t)';
res_mat_lag=lag(res_mat);
res_mat_lag=trimr(res_mat_lag,1,0);
res_mat=trimr(res_mat,1,0);

u_lag=vec(res_mat_lag);
u=vec(res_mat);
B=(u'*u_lag)/(u'*u);

LM_5=sqrt((n*(t^2))/(t-1))*B;
pv_LM_5=cdfnc(LM_5);

fisher=0.5*sqrt(n*t-t-k)*ln((1+B)/(1-B));
pv_fisher=cdfnc(fisher);


print "----------------Autocorrelation tests---------------";
print "  Testing AC in FE model with two-way fixed effects";
print "****************************************************";
print "  LM_5 statistic:  " LM_5;
print "         p-value:  " pv_LM_5;
print "Fisher statistic:  " fisher;
print "         p-value:  " pv_fisher;
print "****************************************************";
print;
print "**********************************************************";
print "Note: The AC tests are conducted in a " mod;
print "**********************************************************";
print;print;

retp;
endp;