[GIS] Converting Spatial Points to Neighbours List using R

rsp

I am trying to convert some spatial points into a listw object to run the moran.mc function on my data. The data are not adjacent but the points cluster in spatial patterns. Here is how I have tried to write the code:

library(sp)
library(raster)
library(spdep)

latitude <- c(56.33, 48.12, 51.13, 60.83, 52.46, 60.42, 51.07, 52.02, 52.87, , 55.27)

longitude <- c(84.58, 88.35, -55.93, -101.55, -116.19, -150.90, -89.90, 
-90.13, -89.93, -88.93)

core_locations <- cbind(longitude, latitude)

pts <- *SpatialPoints(core_locations)

I have tried two approaches to converting this into the listw; the first is to create a weights matrix, but I can't figure out how to pass this to moran.mc

dist <- pointDistance(pts, lonlat = TRUE)

weights_matrix <- 1 / dist
weights_matrix[!is.finite(weights_matrix)] <- NA
rtot <- rowSums(weights_matrix, na.rm = TRUE)
weights_matrix <- weights_matrix / rtot

The second is try to convert the points into polygons, but maybe this is just a bad idea….

p <- Polygon(pts)
ps <- Polygons(list(p),1)
sps <- SpatialPolygons(list(ps))
w <- poly2nb(sps)
ww <-  nb2listw(w)

*I have a spatial points data frame for these locations with associated attribute data for each point.

Best Answer

SpatialPoints has different way to create neighbourhood list than SpatialPolygons. I faced the same problem and this link might help you out. https://stat.ethz.ch/pipermail/r-sig-geo/2009-December/007220.html

You need to create nb using function knn2nb which needs function knearneigh and then using function nb2listw to create the nb list.