function sp_lag_plot(OUT,linewidth,dotsize)

%SAR_PLOT plots the results of Spatial Autoregression (SAR)

%Written by: TONY E. SMITH, 6/21/99

%INPUTS:  (i)   OUT       = output data cell structure from SAR
%         (ii)  linewidth = (optional) specification of linewidth (default = 1.5)
%         (iii) dotsize   = (optional) specification of dotsize (default = 20)

%Screen OUTPUT: (i)  Plot of predicted versus observed y-values,i.e.,
%                    (y) vs (yHat = Xb)         
%               (ii) Plot of residuals versus predicted residuals,i.e.,
%                    (res) vs (W_res = W*res).


yHat = OUT(:,6);
y = OUT(:,5);
res = OUT(:,3);
W_res = OUT(:,4);

if nargin == 1
   
   lw = 2 ;
   ds = 10 ;
   
elseif nargin == 2
   
   lw = linewidth;
   ds = 10;
   
elseif nargin == 3
   
   lw = linewidth;
   ds = dotsize;
   
else
   
   error('Wrong number of input arguments');
   
end


clf;  %Clear current figure

%PLOT OBSERVED vs PREDICTED

plot(y,yHat,'.',y,y,'r-','LineWidth',lw);

title('SP-LAG Actual vs. Predicted','FontSize',16);

xlabel('Observed Y-Values','FontSize',12);
ylabel('Predicted Y-Values','FontSize',12);

%increase dot size from default(6) to 'ds':

h = findobj('MarkerSize',6);
set(h,'MarkerSize',ds);

%set paper size for printing

set(gcf,'PaperPosition',[1.75 3 5 5]);

pause;



%PLOT RESIDUALS vs PREDICTED RESIDUALS

clf;

b = W_res\res;
pred_res = b*W_res;
n = length(res);
var = (1/n)*(res - pred_res)'*(res - pred_res);
s = sqrt(var);
mu = mean(W_res);
ss = (W_res - mu)'*(W_res - mu);
b_se = s/sqrt(ss);
t = abs(b/b_se);
Pval = tcdf(-t,n-1);
disp(' ');
disp(['SP_LAG Residual P-Value = 'num2str(Pval)]);
disp(' ');

plot(W_res,res,'.',W_res,pred_res,'r-','LineWidth',lw);

title('SP-LAG Residuals vs. Predicted Residuals','FontSize',14);

xlabel('Residuals','FontSize',12);
ylabel('W x Residuals','FontSize',12);


%increase dot size from default(6) to 'ds':

h = findobj('MarkerSize',6);
set(h,'MarkerSize',ds);

%set paper size for printing

set(gcf,'PaperPosition',[1.75 3 5 5]);


