Donald J. Lacombe's MATLAB Code for Spatial Econometrics Page

This page contains MATLAB Code for various spatial econometric routines.

Please click on the following links to access the files.



KNN Weight Matrix Code. The following code produces a k nearest neighbors spatial weight matrix using the Great Circle formula.

The routine generates a sparse spatial weight matrix using user supplied coordinates.

The results can be more accurate than using Euclidean distance formulas. The routine can be slower than other routines if the data set is large.

A demonstration file is included in the zip archive.

KNN Weight Matrix Code Great Circle Distance: Download Here

Update: February 11, 2016

I have updated the routine with a different formula for calcualting the pairwise distances that is even more accurate than the previous code. Many thanks to my colleague and frequent co-author
Stuart McIntyre of the University of Strathclyde for helping with this new routine.


Lagrange Multiplier Testing Suite. The Lagrange Multiplier Testing Suite contains the following tests for spatial dependence:

The Lagrange Multiplier Testing Suite is designed to be used in conjunction with Jim LeSage's Spatial Econometric Toolbox for MATLAB.

LM Testing Suite: Download Here



Lagrange Multiplier Tests for Panel Data. The following file contains MATLAB code to calculate Lagrange Multiplier tests for panel data:


These tests are based on the formulas in J. Paul Elhorst's chapter on spatial panel data models:

Elhorst JP (2009) Spatial Panel Data Models. In Fischer MM, Getis A (Eds.) Handbook of Applied Spatial Analysis, pp. 377-407.
Springer: Berlin Heidelberg New York. http://www.springerlink.com/content/u8086626076458v0/

A demonstration file is included in the zip folder. These files are designed to be used with Jim LeSage's Spatial Econometrics Toolbox for MATLAB.

UPDATE: The previous code that was posted had an error in the LM Robust Error test for the spatial panel. The correct code is now available via the link below.
Many thanks to Paul Elhorst and Raymond Florax for pointing out this error!

LM Panel Testing Code: Download Here




Elhorst Spatial Panel Code with the LeSage and Pace Effects Estimates

UPDATE on 3/21/11: The Elhorst Spatial Panel MATLAB code that has been extended to include the bias correction procedure of Lee and Yu (2010) now include the LeSage and Pace effects estimates that calculate the correct marginal effects in the case of a spatially lagged dependent variable. These routines print out the effects estimates in a similar fashion to the LeSage and Pace spatial econometrics toolbox routines. A demonstration file ("panel_effects_demo") is included in the zip file and these routines are designed to be used in conjunction with the LeSage and Pace Spatial Econometrics toolbox.

Many thanks to Professor Paul Elhorst for confirming the accuracy of the code!

The code is based on 
J. Paul Elhorst's working paper on spatial panel data models and on a book chapter:

Elhorst JP (2011) MATLAB Software for Spatial Panels. Available at http://www.regroningen.nl/elhorst/doc/Matlab-paper.pdf

Elhorst JP (2009) Spatial Panel Data Models. In Fischer MM, Getis A (Eds.) Handbook of Applied Spatial Analysis, pp. 377-407.

Springer: Berlin Heidelberg New York. http://www.springerlink.com/content/u8086626076458v0/



Elhorst Bias Corrected Spatial Panel Code with Effects Estimates



Distance Based Weight Matrix Code

The following zip file has code to calculate two different types of distance based weight matrices:

The code is currently designed to build weight matrices to ensure that all geographic entities have at least one neighbor. The code can be modified to allow for distances in kilometers or miles, or for an arbitrary distance cutoff, such as the average of all distances, or for a specific value.

Distance Based Weight Matrices: Download Here



Row Normalize Across Two Weight Matrices

The following MATLAB routine will allow users to row normalize across two weight matrices. The routine works with full or sparse weight matrices.

Row Normalize for Two Weight Matrices



Distance Based Weight Matrix with Bands

The following zip file contains code to calculate a distance based weight matrix with bands, where geographic entities that are withtin a certain distance band are considered neighbors. Included in the zip file is the code and a demonstration file.

Distance Based Weight Matrix with Bands: Download Here


Related Links

Jim LeSage's Spatial Econometrics Toolbox

Kelley Pace's Spatial Statistics Toolbox

Paul Elhorst's Web Page


Back to Home Page