0

I am new to using shapefiles in R and I was wondering if you can help me get a better understanding.

I need to create a spatial adjacency matrix W so that I can build a spatial model. W is an n x n matrix where n is the number of area polygons. The diagonal entries are wii = 0 and the off-diagonal entries wij = 1 if areas i and j share a common boundary and wij = 0 otherwise.

I know I would probably need to construct a contiguity matrix (I chose to use a queen neighborhood). But I am not sure how to further derive my spatial adjacency matrix from this.

#load relevant packages
library(sf)
library(tmap)
library(tmaptools)
library(dplyr)

#import data
mydata <- read.csv("tobago_communities.csv")

#import shapefile
mymap <-st_read("C:/Users/ndook/OneDrive/Desktop/Tobago/2011_parish_data.shp", stringsAsFactors = FALSE)

#join data and shapefile into one dataframe
map_and_data <- inner_join(mymap, mydata, by = "TGOLOC_ID")

#generate map
tm_shape(map_and_data) + tm_polygons("Unemployment")

#specify queen neighborhood
queen_tobago.nb <- poly2nb(mymap)

So I'm assuming the queen neighborhood would somehow be relevant to getting the spatial adjacency matrix but I am stuck at this point. Any further suggestions would be greatly appreciated.

mt1022
  • 16,834
  • 5
  • 48
  • 71
Outlier
  • 417
  • 2
  • 10

1 Answers1

0

The poly2nb function does generate a neighborhood list. Note that you need to call the option queen=T if you want queen neighborhood.

Some R packages expect a list representation of the spatial matrix, others might want a matrix form. The nb2listw function turns the neighborhood list into a list of spatial weights. With the nb2mat function, you get a matrix representation that you are probably looking for (https://rdrr.io/rforge/spdep/man/nb2mat.html).

Dominix
  • 433
  • 3
  • 9