# Converting Spatial Points to Neighbours List using R?

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.

• The Moran's statistic is not appropriate for unmarked (without associated values) points. However, you are looking for the spdep knearneigh or dnearneigh function to create an appropriate list object. Please read the spdep vignettes on package use and creating neighbors. Nov 23, 2017 at 20:19
• Thanks - very helpful. I have edited my original post - I should have said that I have a spatial points data frame with associated attributes for each point. Nov 24, 2017 at 13:54