function S=InvSWM_F(xcoord,ycoord,lower,upper,RowStdOpt)
% PURPOSE: uses x,y coordinates to produce distance-based spatial weight matrices
% useful for spatial econometric regressions.
% The user is asked to input x and y coordinates, as well as a lower and upper
% bound threshold for the neighborhood.
% ------------------------------------------------------
% USAGE: W = InvSWM(xcoord,ycoord,lower,upper,rowstdopt)
% where: xcoord = x-direction coordinate
% ycoord = y-direction coordinate
% lower = lower bound distance cut-off
% uppper = upper bound distance cut-off
% rowstdopt = 1 for row-standardization
% = 0 for no standardization
% ------------------------------------------------------
% RETURNS:
% W = a sparse weight matrix based on distance cut-offs
% set by lower and upper input options
% ------------------------------------------------------
%
% written by: Patrick Walsh
% Walsh.Patrick@epa.gov
% Economist
% National Center for Environmental Economics, US EPA
% Drawing from previous code by Shawn Bucholtz, fromerly of the USDA ERS
%start by using Bucholtz's file
A=pdweight(xcoord,ycoord,lower,upper,0);
n=length(xcoord);
% The problem with Bucholtz's file was that it had one's all along the resulting
% diagonal, so that each observation was a neighbor to itself...
W=A-speye(n);
if RowStdOpt==1;
SW=invdist(W,1);
else
SW=invdist(W,0);
end
S=SW;
function S = invdist(SW,N);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Purpose: Transforms a distance weights matrix into an inverse distance
% weights matrix, and optionally standardizes the rows.
% Inputs:
% WD = An n x n spatial weights matrix based on distance
% This matrix should NOT be row-standardized.
% N=row standardize option. If N=1 the output matrix
% will be row standardized. If N=0, it will remain
% unstandardized.
%
% Requires: James P. LeSage's spatial econometrics toolbox.
% Returns: Sparse nxn matrix
% Author: Patrick Walsh
% Department of Economics
% University of Central Florida
% PatJwalsh@gmail.com
%
% Using inspiration from code by
% Justin M Ross
% Department of Economics
% Indiana University, Bloomington
% jross08.googlepages.com
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[n j] = size(SW);
for i=1:n
for j=1:n
if i==j;
SW(i,j)=0; %make sure the i,i th element =0
end;
end;
end;
for i=1:n;
for j=1:n;
if (SW(i,j)>0);
SW(i,j)=1/SW(i,j); %Turn the distances to inverse distances
else
SW(i,j)=0;
end;
end;
end;
if (N==1);
SW=sparse(normbig(SW));
else SW = sparse(SW);
end;
S=SW;
function g=pdweight(xcoord,ycoord,lower,upper,RowStdOpt);
% PURPOSE: uses x,y coordinates to produce distance-based spatial weight matrices
% The user is asked to input x and y coordinates, as well as a lower and upper
% bound cutoff for the neighborhood.
% ------------------------------------------------------
% USAGE: W = pdweight(xcoord,ycoord,lower,upper,rowstdopt)
% where: xcoord = x-direction coordinate
% ycoord = y-direction coordinate
% lower = lower bound distance cut-off
% uppper = upper bound distance cut-off
% rowstdopt = 1 for row-standardization
% = 0 for no standardization
% ------------------------------------------------------
% RETURNS:
% W = a sparse weight matrix based on distance cut-offs
% set by lower and upper input options
% ------------------------------------------------------
% written by: Shawn Bucholtz
% SBUCHOLTZ@ers.usda.gov
% USDA-ERS-ISD-ADB
%This function builds a sparse spatial weight matrix.
%The user is asked to input x and y coordinates, as well as a lower and upper
%bound cutoff for neighborhood.
%The user is given the option of indicating if they would like
%to Row-standarize the weight matrix (1=Row Standardized, 0=Not Row-Standardized)
%Declare inital values;
i=1;
p2=[1 1 -8888];
%Begin loop for each observation;
for i=1:(length(xcoord));
j=1;
p1=[1 1 -9999];%Make an inital value;
%Begin loop to compute distance from observation i to all other observations;
for j=1:(length(xcoord));
if xcoord(i)==xcoord(j) & ycoord(i)==ycoord(j);
D=1;%This ensures that two places with the same x,y location will
%be defaulted to 1 distance unit away from eachother.
%This can happen in the case of two condos who are assocaited with one building location.
else;
Xdist=abs(xcoord(i)-xcoord(j));
Ydist=abs(ycoord(i)-ycoord(j));
D = sqrt(Xdist^2 + Ydist^2);
end;
p = [i j D];
%A loop to generate a list of neighbors j of current obs i
if D > lower & D <= upper;%Check to see if j meets the neighborhood cutoff criteria
if p1(3) == -9999; %If this is the first j in the list of neighbors, then
%make it the inital data set;
p1=p;
else;
p1 = [p1;p];%If this is not the first j in the list of neighbors, then append this
%to previous list of all neighbors j for observation i.
end;
else;
p1=p1;%If distance between i and j did not meet neighborhood cutoff criteria, then
%do not append this to previous list of neighbors for obs. i;
end;
j=j+1; %Step to next neighbor j of observation i
end;
if p1(3) == 0;%A Check to see if observation i had any neighbors within cutoff
i=i+1;%If it did not, then pass loop to next i
else;
%If observation i had at least 1 neighbor within cutoff, then append that data to
%previous data set for i-1
if RowStdOpt == 1;%Row standardize the weight matrix
p1(:,3) = p1(:,3) ./ sum(p1(:,3));
end;
if p2(3) == -8888;%If the current observation i is the first observationervation to have any neighbors
%then make it the first data set;
p2=p1;
else;
p2=[p2;p1];%If the current observation i is not the first observation to have any neighbors
%then append current observation i's neighbors to all other previous i's neighbors
clear p1;
end;
i=i+1;%pass loop to next i
end;
end;
%Generate the sparse matrix
g=sparse(p2(:,1),p2(:,2),p2(:,3));
function SN=normbig(SW);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Purpose: Transforms an unstandardized weights matrix into a standardized
% weights matrix, where all rows sum to one. Is useful with large
% matrices.
% Inputs:
% W = An n x n spatial weights matrix
% This matrix should NOT be row-standardized.
%
%
% Returns: Sparse nxn matrix
% Author: Patrick Walsh
% National Center for Environmental Economics
% US Environmental Protection Agency
% Walsh.Patrick@epa.gov
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[n j] = size(SW);
L=sum(SW');
T=L';
Q=logical(T); %make sure we are not dividing by zero if no neighbors
ones1=ones(n,1);
Q2=Q-ones1;
Q3=Q2*-1;
T2=T+Q3;
for i=1:n;
SW(:,i)=SW(:,i)./T2;
end;
SN=sparse(SW);