Code covered by the BSD License  

Highlights from
Nearest Neighbor Spatial Weights Matrix

Nearest Neighbor Spatial Weights Matrix



08 May 2012 (Updated )

uses x,y coordinates to produce one of several types of nearest-neighbor spatial weight matrix.

function S=nnSWM(xcoord,ycoord,N,Contig,RowStdOpt)
% PURPOSE: uses x,y coordinates to produce a nearest-neighbor spatial
%          weight matrix. The user is asked to input x and y coordinates,
%          as well as the number of neighbors. Several configuration
%          options are available for the specification of the individual
%          elements of the SWM.
% ------------------------------------------------------
% USAGE: W = nnSWM(xcoord,ycoord,N,Contig,Rowstdopt)
% where:     xcoord = x-direction coordinate
%            ycoord = y-direction coordinate
%            N = number of neighbors to include in the SWM
%            Contig = 1 for individual SWM(i,j) entries = 1
%                   = 0 for individual SWM(i,j) entries = inverse distance
%            rowstdopt = 1 for row-standardization
%                      = 0 for no standardization
% ------------------------------------------------------
%          S = a sparse weight matrix based on the given number of
%              neighbors. A 1 is entered in SWM(i,j) if i and j are 
%              deemed neighbors (unless contig=0, where inverse distance
%              appears for the designated n neighbors.
% ------------------------------------------------------
% written by: Patrick Walsh
% Economist
% National Center for Environmental Economics, US EPA

% First, preallocate a matrix for the nearest neighbor results
Neighbors = zeros(length(xcoord)*N,3);

% Start looping over all observations
for i = 1:length(xcoord)
	% Create a matrix to put all the distances between i and j and i and j
	DistMat = zeros(length(xcoord),3);
	% Start loop between i and all other j
	for j=1:length(xcoord)
		% Calculate distance between observations
		D = sqrt(Xdist^2 + Ydist^2);
		DistMat(j,:) = [i j D];
	% Now that we have the matrix of distances and is and js, want to take the nearest
	% N neighbors by distance,  excluding the self distance (i==j)
	% First, find i==j and remove it (don't want an observation to be a neighbor to itself)
	A = find(DistMat(:,1)==DistMat(:,2));
	% Next sort the distance matrix
	DistMatSort = sortrows(DistMat,3);
	% Keep the closest N items
	Nneighbors = DistMatSort(1:N,:);
	% this matrix contains the N nearest neighbors
	% If Contig == 1, replace the distances with equal weight
	if Contig == 1;
		Nneighbors(:,3) = ones(N,1);
		% replace any zeros in the distance matrix to -1 (so that division does not crap out)
		Dists = Nneighbors(:,3);
		Dists = 1./Dists;
		% bring back to zero
		Nneighbors(:,3) = Dists;
	% If indicated, standardize the weights matrix
	if RowStdOpt == 1;
		Nneighbors(:,3) = Nneighbors(:,3)./sum(Nneighbors(:,3));
	% Put this set of N observations into the preallocated matrix.
	Neighbors(((i-1)*N+1):i*N,:) = Nneighbors;
S = sparse(Neighbors(:,1),Neighbors(:,2),Neighbors(:,3),n,n);

Contact us