This tutorial provides an overview of the various libraries that contain tools for calculating spatial statistics. However, be aware that the focus of this tutorial is on finding the libraries that implement these different statistics, not on defining them or discussing their substantive interpretation. It is strongly recommended that users familiarize themselves with spatial statistical methods from another source before jumping to this tutorial.

The first thing to know about libraries for spatial statistics is that there are lots of them, and there is also a lot of redundancy across libraries. In this tutorial, I will focus on a small subset of these libraries that seem to be the most well-established and comprehensive, but if you find you are missing something, additional resources abound.

1. spatstat for Spatial Point Patterns

The best library for studying the statistical properties of point distributions is probably spatstat, which also comes with a companion textbook, website, and [a set of tutorials](click the “Vignette” links)]( spatstat has also been around for a long time, has been widely used, and repeatedly updated, suggesting most bugs are likely to have been found and patched (which is not always the case for R packages).

Here are a few commonly used methods:

1.1 Using spatstat with Spatial* Objects

spatstat is not directly compatible with the sp library, so to use spatstat we need to do a quick conversion:

sf <- readOGR("rgis5_data/sfpd_incident_shapefile", "sfpd_incident_2015")
OGR data source with driver: ESRI Shapefile 
Source: "rgis5_data/sfpd_incident_shapefile", layer: "sfpd_incident_2015"
with 124016 features
It has 11 fields
# Syntax is: ppp(x.coordinates, y.coordinates, x.range, y.range)
ss.object <- ppp(sf@coords[, "coords.x1"], sf@coords[, "coords.x2"], sf@bbox[1, 
    ], sf@bbox[2, ])

# Do Kest! Note there are lots of versions of kest...
plot(Kest(ss.object, correction = "good"))

2. spdep for Spatial Econometrics

spatstat only works with points. If you wish to analyze spatial correlation of polygons, spdep can be very helpful. A primary author of spdep is Roger Bivand, who also wrote the sp library, so unlike spatstats, spdep plays very well with sp objects!

2.1 Installing spatstat

If install.packages("spatstat") does not work for you:

  1. Visit the CRAN spatstat site

  2. In the “downloads” section, download the r-release binaries file associated with your operating system.

  3. Set your working directory to wherever you placed the downloaded file.

  4. Run install.packages("", repos=NULL) (updating the file name to the most recent version and appropriate suffix)

Hopefully you’re set to go!

2.2 Neighbor lists and Spatial weights

We generally creating spatial weighting matrices in two steps:

  1. Create a “neighbor list” (nb object)
  2. Convert it to a spatial weighting matrix (listw object)

An nb object just records which features are “neighbors” of one another (you either are or are not a neighbor). The function allows users to specify the A listw is a full, normalized weighting matrix. In most cases, you start by making an nb object then convert it to a listw using the nb2listw command. Details of methods not shown below – like graph distance or k nearest neighbor methods can be found here.

pa <- readOGR("rgis5_data/palo_alto_demographic_shapefile", "palo_alto")
OGR data source with driver: ESRI Shapefile 
Source: "rgis5_data/palo_alto_demographic_shapefile", layer: "palo_alto"
with 371 features
It has 5 fields
pa$share.hispanic <- pa$hispanc/(pa$hispanc + pa$White)