Pages

Monday, May 14, 2012

Spatial Randomness Evaluation in R: Monte Carlo Test

This post is a some kind of reply to this one.

So our goal is to determine whether our point process is random or not. We will use R and spatstat package in particular. Spatstat provides a very handy function for this, that uses K-function combined with Monte Carlo tests. I will spear you from burbling  about theory behind it - the necessary links were already provided. Lets get directly to action.

In this example I will test data about location of my "favourite" illegal dumps in St. Petersburg and Leningrad region.

# we will need: 
library(maptools) 
library(rgdal) 
library(spatstat)

# import data for analysis

S <- readShapePoints("custom_path/dump_centroids.shp", proj4string= CRS("+proj=tmerc +lat_0=0 +lon_0=33 +k=1 +x_0=6500000 +y_0=0 +ellps=krass +towgs84=23.92,-141.27,-80.9,-0,0.35,0.82,-0.12 +units=m +no_defs"))

SP <- as(S, "SpatialPoints")
P <- as(SP, "ppp")

# perform the test itself with a 100 simulations

E <- envelope(P, Kest, nsim = 100)
plot(E, main = NULL)


And here is what we've got in the end, a fancy graph, which demonstrates that our data (Kobs) significantly deviates from a random process (Ktheo):