Skip to content
Prev 27715 / 29559 Next

poly2nb neighbour itself should be considered a neighbour

Dear Dexter, Dear Elias,

Many thanks for your response.

I am looking to create a spatial weight matrix (queen contiguity neighbors), with the only difference that a neighbour itself should also be considered a neighbour.

Below I am sending you the code. Basically I create a polygon for NYC and give that polygon I want to create the spatial weight matrix.

@Elias: I used your answer to generate two matrices A1 and A2. But how to turn them into an nb object so I can use the function nb2listw (with the arguments style="W", zero.policy=TRUE).

Thank you and best regards,
Robert


#####

####################
# ---- packages ----?
####################?
?
packages_install <- function(packages){?
 new.packages <- packages[!(packages %in% installed.packages()[, "Package"])]?
 if (length(new.packages)) ?
 install.packages(new.packages, dependencies = TRUE)?
 sapply(packages, require, character.only = TRUE)?
}?
?
packages_required <- c("data.table", "dplyr", "sf", "spdep", "Matrix")?
packages_install(packages_required)?
?
# Working directory?
setwd("C:/Users/User/Documents/Code")?
?
?
#####################?
# ---- zips_nyc ----?
#####################?
?
zips_nyc_bronx <- c("10451", "10452", "10453", "10454", "10455", "10456", "10457", "10458", "10459", "10460", "10461", "10462", "10463", "10464", "10465", "10466", "10467", "10468", "10469", "10470", "10471", "10472", "10473", "10474", "10475")?
zips_nyc_brooklyn <- c("11201", "11203", "11204", "11205", "11206", "11207", "11208", "11209", "11210", "11211", "11212", "11213", "11214", "11215", "11216", "11217", "11218", "11219", "11220", "11221", "11222", "11223", "11224", "11225", "11226", "11228", "11229", "11230", "11231", "11232", "11233", "11234", "11235", "11236", "11237", "11238", "11239")?
zips_nyc_manhattan <- c("10001", "10002", "10003", "10004", "10005", "10006", "10007", "10009", "10010", "10011", "10012", "10013", "10014", "10016", "10017", "10018", "10019", "10020", "10021", "10022", "10023", "10024", "10025", "10026", "10027", "10028", "10029", "10030", "10031", "10032", "10033", "10034", "10035", "10036", "10037", "10038", "10039", "10040", "10044", "10065", "10075", "10128", "10280")?
zips_nyc_queens <- c("11004", "11005", "11101", "11102", "11103", "11104", "11105", "11106", "11354", "11355", "11356", "11357", "11358", "11359", "11360", "11361", "11362", "11363", "11364", "11365", "11366", "11367", "11368", "11369", "11370", "11372", "11373", "11374", "11375", "11377", "11378", "11379", "11385", "11411", "11412", "11413", "11414", "11415", "11416", "11417", "11418", "11419", "11420", "11421", "11422", "11423", "11426", "11427", "11428", "11429", "11432", "11433", "11434", "11435", "11436", "11691", "11692", "11693", "11694", "11695", "11697")?
zips_nyc_staten_island <- c("10301", "10302", "10303", "10304", "10305", "10306", "10307", "10308", "10309", "10310", "10312", "10314")?
zips_nyc <- sort(c(zips_nyc_bronx, zips_nyc_brooklyn, zips_nyc_manhattan, zips_nyc_queens, zips_nyc_staten_island))?
?
?
#####################?
# ---- shapefile ----?
#####################?
?
## shapefile_us?
?
# Shapefile zips import and Coordinate Reference System (CRS) transformation?
# Download: https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_zcta510_500k.zip?
shapefile_us <- sf::st_read(dsn = "Shapefile", layer = "cb_2018_us_zcta510_500k")?
?
# Columns removal?
shapefile_us <- shapefile_us %>% select(-c(AFFGEOID10, GEOID10, ALAND10, AWATER10))?
?
# Column rename: ZCTA5CE10?
setnames(shapefile_us, old=c("ZCTA5CE10"), new=c("zipcode"))?
?
# Column class change: zipcode?
shapefile_us$zipcode <- as.character(shapefile_us$zipcode)?
?
?
## polygon_nyc?
polygon_nyc <- shapefile_us %>% filter(zipcode %in% zips_nyc)?
?
# Variable creation: list of neighbors for each polygon (queen contiguity neighbors)?
nb <- poly2nb(polygon_nyc, queen=FALSE)?
nn <- card(nb)?
A1 <- sparseMatrix(?
  i = rep(1:length(nn), nn),?
  j = unlist(nb[nn>0]), x=1)?
A2 <- crossprod(A1)?
image(A1)?
image(A2)?
?
# next, supplement the neighbor list with spatial weights: "W" row-standardize weights?
W_Matrix <- nb2listw(neighbours = A1, style="W", zero.policy=TRUE)

#####