Suggested citation:

Mendez C. (2020). Spatial autocorrelation analysis in R. R Studio/RPubs. Available at https://rpubs.com/quarcs-lab/spatial-autocorrelation

This work is licensed under the Creative Commons Attribution-Share Alike 4.0 International License.

Acknowledgment:

Material adapted from multiple sources, in particular the course materials of Guy Lansley & James Cheshire (2016).

1 Libraries

knitr::opts_chunk$set(echo = TRUE)

library(tidyverse)  # Modern data science workflow
library(sf)
library(sp)
library(spdep)
library(rgdal)
library(rgeos)
library(tmap)
library(tmaptools)
library(spgwr)
library(grid)
library(gridExtra)


# Change the presentation of decimal numbers to 4 and avoid scientific notation
options(prompt="R> ", digits=4, scipen=999)

2 Motivation

Waldo Tober’s first law of geography is that “Everything is related to everything else, but near things are more related than distant things.” So we would expect most geographic phenomena to exert a spatial autocorrelation of some kind. In population data this is often the case as persons with similar characteristics tend to reside in similar neighbourhoods due to a range of reasons including house prices, proximity to workplaces and cultural factors.

3 Tutorial objectives

  • Run various measures of spatial correlation
  • Evaluate both global and local measures of spatial autocorrelation
  • Identify spatial clusters

4 Replication files

6 Import datasets

6.1 Non-spatial data

Census.Data <-read.csv("practicaldata.csv")
glimpse(Census.Data)
## Rows: 749
## Columns: 5
## $ OA            <chr> "E00004120", "E00004121", "E00004122", "E00004123", "E00…
## $ White_British <dbl> 42.36, 47.20, 40.68, 49.66, 51.14, 41.42, 48.54, 48.68, …
## $ Low_Occupancy <dbl> 6.2937, 5.9322, 2.9126, 0.9259, 2.0000, 3.9326, 5.5556, …
## $ Unemployed    <dbl> 1.8939, 2.6882, 1.2121, 2.8037, 3.8168, 3.8462, 4.5455, …
## $ Qualification <dbl> 73.63, 69.90, 67.58, 60.78, 65.99, 74.21, 62.45, 60.35, …

6.2 Spatial data

Output.Areas <- readOGR(".", "Camden_oa11")
## Warning in OGRSpatialRef(dsn, layer, morphFromESRI = morphFromESRI, dumpSRS =
## dumpSRS, : Discarded datum OSGB_1936 in Proj4 definition: +proj=tmerc +lat_0=49
## +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/carlos/GitHub/tutorial-spatial-autocorrelation", layer: "Camden_oa11"
## with 749 features
## It has 1 fields
# Run it in console (long output)
glimpse(Output.Areas)

Using the sf package

Output.Areas2 <- read_sf("Camden_oa11.shp")
glimpse(Output.Areas2)
## Rows: 749
## Columns: 2
## $ OA11CD   <chr> "E00004527", "E00004525", "E00004522", "E00004287", "E0000420…
## $ geometry <POLYGON [m]> POLYGON ((530648 181230, 53..., POLYGON ((530511 1815…

7 Merge datasets

OA.Census <- merge(Output.Areas, Census.Data, by.x="OA11CD", by.y="OA")

Transform it into a sf object

OA.Census_sf <- st_as_sf(OA.Census)

8 Spatial distribution

tm_shape(OA.Census_sf) + 
  tm_fill("Qualification",
          palette = "Reds", 
          style = "quantile", 
          title = "% with a Qualification") +
  tm_borders(alpha=.4)