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 Haversine 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: Download Here

Update: September 2, 2015

I have added a C-MEX (compiled C++) version of the KNN Weight Matrix Code to the zip archive along with a demonstration file. The C-MEX version will only work on 64-bit Windows machines at the moment. The speed improvement between the standard MATLAB function and the C-MEX version is approximately 95% on my machine.

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

- LM Lag Test
- LM Error Test
- LM Lag Robust Test
- LM Error Robust Test
- LM Combined Lag / Error Test
- LM Spatial Error Components Test
- LeSage and Pace Spatial Hausman Test

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:

- LM Lag Panel Test
- LM Error Panel Test
- LM Lag Robust Panel Test
- LM Error Robust Panel Test

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:

- Distance Based on Cutoff Value
- Inverse Distance Based on Cutoff 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