function results = sar_boot(y,x,W,nreps)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% USAGE: results = sar_boot(y,x,W,nreps)
% DESCRIPTION: Bootstrap Spatial Autoregression Model (SAR)
% y dependent variable
% x indepdent variables (include constant)
% W spatial weight matrix
% nreps number of repititions in bootstrap
%
% RETURNS: Two columns, the first being the bootstrap coefficients, the
% second column being their associated variance as a measure of bias. The
% last row of the columns is rho.
% NOTES: Requires LeSage's Spatial Econometrics toolbox and the Bootstrap Matlab
% Toolbox from Zoubir and Iskander of the CSP Group.
% Methodology is based on Anselin (1988) Spatial Econometrics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Written By:
% Justin M. Ross
% School of Public & Environmental Affairs
% Indiana University
% Bloomington, IN
% justross@indiana.edu
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[n k] = size(x);
resSAR=sar(y,x,W);
u = resSAR.resid;
r = resSAR.rho;
b = resSAR.beta;
I = eye(n,n);
Wi = inv([I-r*W]);
XB = x*b;
Bout=zeros(nreps,k); %For SAR X
Rout=zeros(nreps,1); %For SAR rho
for i=1:nreps;
ur=bootrsp(u,1); %returns nx1 sample of u
yr = Wi*XB+Wi*ur;
bres=sar(yr,x,W);
br=bres.beta;
rr=bres.rho;
Bout(i,:)=br';
Rout(i,:)=rr;
end;
Br=mean(Bout);
Rr=mean(Rout);
Bvar=var(Bout);
Rvar=var(Rout);
rvar=[Bvar Rvar]';
coeff = [Br Rr]';
results = [coeff rvar];