proc (3)=theta_1w(x,y,t,n,ind_ti);
/*
theta_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.
*/

/* 
Let ind_ti equal 1 for indivudual RE and ind_ti equal 2 for time RE 
*/

local k;
local Z_mu,P,Q;
local u,z,x_g;
local sig_v,sig_1,sig_mu,mu;
local theta,sigma_v,sigma_1;
local alfa_within,res_within,beta_within;
local theta_SA,theta_AM,theta_WH,theta_NE,alfa,beta_lsdv,alfa_wave;

theta=ones(4,1);
sigma_v=ones(4,1);
sigma_1=ones(4,1);

if ind_ti==1; /* Individual random effects */
 P=eye(n).*.ones(t,t)/t;
 Q=eye(n*t)-P;
elseif ind_ti==2;
 P=ones(n,n)/n.*.eye(t);
 Q=eye(n*t)-P;
endif;


/* Wallace and Hussein (1969) */

x_g=ones(rows(x),1)~x;
u=y-x_g*(y/x_g);

sig_1=sqrt(u'*P*u/n);
sig_v=sqrt(u'*Q*u/(n*(t-1)));

theta_WH=1-sig_v/sig_1; if theta_WH<0; theta_WH=0; endif;
theta[1]=theta_WH;
sigma_v[1]=sig_v;
sigma_1[1]=sig_1;

/* Amemiya (1971) */

beta_lsdv=(Q*y)/(Q*x);
alfa_wave=meanc(y)-meanc(x)'*beta_lsdv;
u=y-alfa_wave*ones(n*t,1)-X*beta_lsdv;

sig_1=sqrt(u'*P*u/n);
sig_v=sqrt(u'*Q*u/(n*(t-1)));

theta_AM=1-sig_v/sig_1; if theta_AM<0; theta_AM=0; endif;
theta[2]=theta_AM;
sigma_v[2]=sig_v;
sigma_1[2]=sig_1;

/* Swamy and Aurora (1972) */
k=cols(x);
sig_v=sqrt((y'*Q*y-y'*Q*x*inv(x'*Q*x)*x'*Q*y)/(n*(t-1)-k));
sig_1=sqrt((y'*P*y-y'*P*x_g*inv(x_g'*P*x_g)*x_g'*P*y)/(n-k-1));

theta_SA=1-sig_v/sig_1;  if theta_SA<0; theta_SA=0; endif;
theta[3]=theta_SA;
sigma_v[3]=sig_v;
sigma_1[3]=sig_1;

/* Nerlove (1971) */

beta_within=(Q*y)/(Q*x);
res_within=Q*y-Q*x*((Q*y)/(Q*x));
alfa=meanc(y)-meanc(x)'*beta_within;
mu=unique(P*y-alfa*ones(rows(P*y),1)-(P*x)*beta_within,1);
sig_mu=sqrt(sumc((mu-meanc(mu))^2)/(n-1));
sig_v=sqrt(res_within'*res_within/(n*t));
sig_1=sqrt(T*sig_mu^2+sig_v^2);

theta_NE=1-sig_v/sig_1;  if theta_NE<0; theta_NE=0; endif;
theta[4]=theta_NE;
sigma_v[4]=sig_v;
sigma_1[4]=sig_1;

retp(theta,sigma_1,sigma_v);
endp;