/********************************************************************************/

/** Procedure to Compute Spatial Correlation Consistent Standard Errors **/

/********* for Linear Instrumental Variable Panel Data Models **************/

/******************************** based on ************************************/

/******************** John C. Driscoll and Aart C. Kraay (1998) **************/

/************** "Consistent-Covariance Matrix Estimation With**************/

/********************* Spatially Dependent Panel Data"**********************/

/****** Review of Economics and Statistics.  November.  pp. 549-560 *****/

/* Authors:

             			John C. Driscoll

             			Department of Economics

             			Brown University, Box B

             			Providence RI 02912

				John_Driscoll@brown.edu



             			Aart C. Kraay

             			The World Bank

             			Washington DC 20433

                                akraay@worldbank.org        						*/



/* Notes:

        The procedure computes coefficient estimates and standard errors 

consistent for spatial correlation, autocorrelation and heteroskedasticity for linear

instrument-variable panel data models of the form



y(i,t)=x(i,t)'b+e(i,t)



where x(i,t) is an Lx1 vector of explanatory variables; b is an Lx1 coefficient

vector; e(i,t) is a residual; and  i=1,...,N indexes cross-sectional units and 

t=1,...,T indexes time periods.  The model is identified by a Kx1 vector

of orthogonality conditions E[z(i,t)'e(i,t)]=0, where z(i,t) is a Kx1 vector of 

instrumental variables with K>=L.  If the model contains individual effects,

these must first be removed by the appropriate transformation (i.e. differencing

or taking deviations from individual means).  The procedure does not support

missing values.



The procedure takes the form



scc(y,x,z,N,T,lag), 



where

        y is an NT by 1 vector containing the dependent variable

        x is an NT by L matrix of explanatory variables

        z is an NT by K matrix of instrumental variables, K>=L

        N is the number of cross-sectional units

        T is the number of time-series observations

        lag is the lag-truncation parameter.



The variables y,x and z must be supplied stacked as in paper (i.e. the first

N rows contain the N cross-sectional units for the first time period, the second

N rows contain the N cross-sectional units for the second time period, etc.). 

We use the Bartlett kernel.  The procedure outputs a Kx2 matrix with the two-stage 

least squares estimates in the first column and spatial-correlation consistent standard 

errors in the second.  To obtain OLS estimates, simply use the same variables for z and x. */



proc(1)=scc(y,x,z,N,T,lag);

local K,thetahat,r,h,j,shat,ht,htl,omegaj,jj,sdtheta,tt;

K=cols(z);



/* Obtain IV estimator thetahat and estimated residuals r */

thetahat=invpd(z'x)*z'y;

r=y-x*thetahat;



/* Form TxK matrix h.  Each row is the 1xK vector of cross-sectional */

/* averages of the moment conditions evaluated at thetahat, ht(thetahat)' */

h=(z[1:N,.]'r[1:N,.])';

j=2;

do until j>T;

    h=h|((z[(j-1)*N+1:j*N,.]'r[(j-1)*N+1:j*N])');

j=j+1;

endo;

h=h./N;



/* Form estimator of S */

shat=zeros(K,K);

ht=h;

tt=1;

omegaj=zeros(K,K);

do while tt < rows(ht)+1;

omegaj=omegaj+ht[tt,.]'ht[tt,.];

tt=tt+1;

endo;

shat=(1/T)*omegaj;

if lag ==0;

shat=shat;

elseif lag>0;



j=1;

do while j < lag+1;

    omegaj=zeros(K,K);

    tt=j+1;

    do while tt < rows(ht)+1;

        omegaj=omegaj+ht[tt,.]'ht[tt-j,.];

    tt=tt+1;

    endo;

    shat=shat+(1-abs(j)/(lag+1))*(1/T)*(omegaj+omegaj');

    j=j+1;

endo;

endif;



/* Compute standard errors for the IV estimator */

sdtheta=diag((invpd((z'x)/(N*T))*shat*invpd((z'x/(N*T)))/(T))).^(.5);



retp(thetahat~sdtheta);

endp;



/***************************************************************************/

/***************************************************************************/

/***************************************************************************/









