Download the county-level police data for the state of Mississippi.
Compute Moran’s I and Geary C statistics for the percentage of whites
(WHITE) and test for statistical significance against the
null hypothesis of no spatial autocorrelation. Create a Moran’s scatter
plot. Explain your results.
setwd("C:/GIS517")
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(spdep))
suppressPackageStartupMessages(library(rgdal))
suppressMessages(library(ggplot2))
suppressPackageStartupMessages(library(knitr))
Download and unzip the data with unzip(). Read the shapefile with
read.OGR()
download.file("http://myweb.fsu.edu/jelsner/temp/data/police.zip", "police.zip")
unzip("police.zip")
pol = readOGR("C:/GIS517/police/police.shp")
## Warning: OGR support is provided by the sf and terra packages among others
## Warning: OGR support is provided by the sf and terra packages among others
## Warning: OGR support is provided by the sf and terra packages among others
## Warning: OGR support is provided by the sf and terra packages among others
## Warning: OGR support is provided by the sf and terra packages among others
## Warning: OGR support is provided by the sf and terra packages among others
## Warning: OGR support is provided by the sf and terra packages among others
## OGR data source with driver: ESRI Shapefile
## Source: "C:\GIS517\police\police.shp", layer: "police"
## with 82 features
## It has 21 fields
## Integer64 fields read as strings: CNTY_ CNTY_ID FIPSNO
Moran’s I Statistic test for percentage of whites.
Create a list of neighbors with poly2nb().
nb_pol <- poly2nb(pol)
nb_pol
## Neighbour list object:
## Number of regions: 82
## Number of nonzero links: 434
## Percentage nonzero weights: 6.454491
## Average number of links: 5.292683
Generate spatial weights object from poly2nb() output.
wnb_pol <- nb2listw(nb_pol)
summary(wnb_pol)
## Characteristics of weights list object:
## Neighbour list object:
## Number of regions: 82
## Number of nonzero links: 434
## Percentage nonzero weights: 6.454491
## Average number of links: 5.292683
## Link number distribution:
##
## 3 4 5 6 7 8 9
## 11 14 20 18 16 2 1
## 11 least connected regions:
## 0 1 3 4 20 64 71 72 74 79 81 with 3 links
## 1 most connected region:
## 36 with 9 links
##
## Weights style: W
## Weights constants summary:
## n nn S0 S1 S2
## W 82 6724 82 32.62703 333.0236
m <- length(pol$WHITE)
s <- Szero(wnb_pol)
Use moran() to generate a Moran’s I statistic and a Kurtosis
statistic.
moran(pol$WHITE, listw = wnb_pol, n = m, S0 = s)
## $I
## [1] 0.5634778
##
## $K
## [1] 2.300738
The I statistic value is positive, which means that there is
clustering behavior happening. The K statistic value is relatively close
to 3 which is expected for normal distributions. This Moran’s I
statistic was calculated with the default ‘W’ weighting in moran(). This
method of weighting is row standardized, meaning the sum of the weights
over all links is equalto the total number of polygons.
Moran’s Scatterplot
Calculate spatial lag with lag.listw().
white <- pol$WHITE
w_white <- lag.listw(wnb_pol, white)
Plot
data.frame(white, w_white) %>%
ggplot(., aes(x = white, y = w_white)) +
geom_point() +
geom_smooth(method = lm) +
xlab("White") + ylab("Spatial Lag of % White")
## `geom_smooth()` using formula 'y ~ x'

Slope should equal the Moran’s I statistic. Compare.
lm(w_white ~ white)
##
## Call:
## lm(formula = w_white ~ white)
##
## Coefficients:
## (Intercept) white
## 27.2333 0.5635
Calculate the Geary C statistic.
geary(pol$WHITE, listw = wnb_pol, n = m, S0 = s, n1 = m - 1, zero.policy=NULL)
## $C
## [1] 0.4123818
##
## $K
## [1] 2.300738
The C statistic value is less than one, which means there is
positive spatial correlation. The K value is relatively close to 3,
which is expected for data with a normal distribution.
Both the Moran’s I statistic and Geary’s C statistic suggest that
there is in fact spatial autoacorrelation of the percentage of white
residents among Mississippi counties. The Moran’s scatterplot shows that
generally, counties with higher percentages of white residents are
neighbored by other counties with high percentages of white
residents.