Neighbors and Adjacency (Spatial Weight) Matrices

Important note

In what follows, I describe some modules for spatial data that were available before Stata introduced their own sp commands for the analysis of spatial data in version 15.

As far as I can judge, the modules I describe here are still available and working. For information about the inbuilt sp commands, see Stata's help system (help sp will provide access to the introductory remarks and an overview of all available commands).



There are basically two commands: spatwmat and spmat (from the sppack package).


Spatwmat

This procedure, written by Maurizio Pisati, creates Stata matrices. It will create adjacency (or spatial weight) matrices in two ways: It can read either external files that contain the weights, or it creates the weights from variables (columns) in the current data set that specify the latitude and the longitude. Typically, this latter information will stem from the database file, as this file contains one row per case. Note that the latitude and longitude will represent the centroids of areas; therefore, only distance-based weights (but no weights based on neighborship) can be computed in this case.

Reading weight matrices

spatwmat using "myweights.dta", name(name-of-matrix) eigenval(name-e-matrix) stand

This reads the external matrix myweights.dta and stores it as a standardized, i.e. row-normalized matrix in name-of-matrix; furthermore, a vector of eigenvalues is stored in name-e-matrix.

Creating weight matrices from latitude and longitude

spatwmat, stand name(name-of-matrix) xcoord(lat) ycoord(long) band(0 10)

This creates (standardized) matrix name-of-matrix from coordinates stored in variables lat and long. Option band(0 10) indicates that objects will be considered as neighbors only within the indicated range.

Note that "both the x-coordinate and the y-coordinate must be expressed in projected units, e.g., metres, kilometres, miles, or arbitrary digitizing units", as the help file states.


Spmat

Creating matrices

spmat permits the creation of both contiguity-based and distance-based matrices. They are special objects that, as for as I know, cannot be looked at interactively. However, they may be saved or exported to disk (see below).

spmat contiguity name-of-matrix using mycoord, id(id-var) norm(row)

This uses the coordinates file mycoord to create the row-normalized contiguity matrix name-of-matrix. Two other options for nomalization are available.

spmat idistance name-of-matrix LON LAT , id(id) norm(row)

This uses the current working file with variables LON (for longitude) and LAT (for latitude) to create the inverse distance matrix name-of-matrix.

Additional commands

Eigenvalues, by default, are not stored in separate matrices; rather, the information is added to the matrix. This is achieved with a separate command. Assuming that matrix mymatrix has been created, the following command will add eigenvalues:

spmat eigenvalues mymatrix

However, you can store the eigenvalues in a separate Mata vector as follows:

spmat eigenvalues(name-of-vector) mymatrix

You can obtain basic information about matrices created with spmat:

spmat summarize name-of-matrix, links detail

Matrices can be saved to disk:

spmat save name-of-matrix using name-for-saving.spmat

They may be exported to an ASCII file:

spmat export name-of-matrix using name-for-saving.txt

© W. Ludwig-Mayerhofer, Stata Guide | Last update: 19 Aug 2016