Skip to content

Instantly share code, notes, and snippets.

@abelsonlive
Created May 17, 2012 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abelsonlive/2715325 to your computer and use it in GitHub Desktop.
Save abelsonlive/2715325 to your computer and use it in GitHub Desktop.
Add p-values from Local Moran's test to a shapefile with counts.
library("foreign")
library("rgdal")
library("sp")
library("spdep")
library("maptools")
library("SpatialEpi")
library("plyr")
data <- read.dbf("counts.dbf")
shapes <- readShapePoly("counts.shp")
#create a network of neighboring shapes
neighbors <- poly2nb(shapes)
#create weights based off of a shapes neighbors
weights <-nb2listw(neighbors, style="B", zero.policy=TRUE)
#local moran's test for autocorrelation
morans <- localmoran(shapes$counts, weights, zero.policy=TRUE)
#bind moran's pvalues back to the data
data$dbf$pval <- morans[,5]
#rewrite data
write.dbf(data, "counts.dbf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment