proc (0)=lm_test(x,y,n,t);
/*
lm_test.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_1,LM_2;
local HO_1,HO_2;
local LM_tot;
local u;

u=y-(ones(n*t,1)~x)*(y/(ones(n*t,1)~x)); @u is the ols residual@

/* B-P LM test for the null of no heteroscedasticity */
/* LM_1 is used to test the null that (sig_mu)^2=0 i.e. the variance of the individual eff. is zero */
/* LM_2 is used to test the null that (sig_lambda)^2=0 i.e. the variance of the time eff. is zero */
/* LM_tot is used to test the null that (sig_lambda)^2=(sig_mu)^2=0 i.e. the variance of the time eff. and ind. eff. is zero jointly*/
/* The alternative is that a RE model is correct */

LM_1=(n*t)/(2*(t-1))*(1-(u'*(eye(n).*.ones(t,t))*u)/(u'*u))^2;
LM_2=(n*t)/(2*(n-1))*(1-(u'*(ones(n,n).*.eye(t))*u)/(u'*u))^2;

LM_tot=LM_1+LM_2;

print "**********************************************************************************";
print "LM-test for the null that the stated variance (variances) is (are) equal to zero";
print "Under the null the random effects model becomes the pooled model";
print "The alternative is that the RE-model is correct";
print "**********************************************************************************";
print "--------------------------------------------------------------------------------------------------";
print "(sig_mu)^2=0 no variance component for individual spec. eff."; 
print "                          LM_1:  " LM_1;
print "p-value (Chi_sq_df=1): " cdfchic(LM_1,1);
print "--------------------------------------------------------------------------------------------------";
print "(sig_lambda)^2=0 no variance component for time spec. eff."; 
print "                          LM_2:  " LM_2;
print "p-value (Chi_sq_df=1): " cdfchic(LM_2,1);
print "--------------------------------------------------------------------------------------------------";
print "(sig_lambda)^2=(sig_lambda)^2=0"; 
print "               LM_1+LM_2:  " LM_tot;
print "p-value (Chi_sq_df=2): " cdfchic(LM_tot,2);
print "--------------------------------------------------------------------------------------------------";
print "**********************************************************************************";
HO_1=sqrt((n*t)/(2*(t-1)))*((u'*(eye(n).*.ones(t,t))*u)/(u'*u)-1);
HO_2=sqrt((n*t)/(2*(n-1)))*((u'*(ones(n,n).*.eye(t))*u)/(u'*u)-1);
print "********************Honda (1985) correction of the LM test*********************";
print "**********************************************************************************";
print "--------------------------------------------------------------------------------------------------";
print "sig_mu=0 (std. dev. for indiv. spec. eff. eq. to 0)"; 
print "                          HO_1:  " HO_1;
print "p-value One-sided N(0,1): " cdfnc(HO_1);
print "--------------------------------------------------------------------------------------------------";
print "sig_lambda=0 (std. dev. for time spec. eff. eq. to 0)"; 
print "                          HO_2:  " HO_2;
print "p-value One-sided N(0,1): " cdfnc(HO_2);
print "--------------------------------------------------------------------------------------------------";
print "**********************************************************************************";

retp;
endp;
