I am trying to use the characteristics of neighbors of neighbors of New Jersey's municipalities as instrumental variables. I am using spatial durbin models. But I encounter some errors when some municipalities have no neighbors of neighbors.
One way I am getting the neighbors is using poly2nb function: geo.nb <- poly2nb(geo_2019); where geo_2019 is my dataset; And if there is an island, I am using k-nearest neighbors with k = 2;
I am able to create a .nb list for neighbors of neighbors. But some municipalities have no neighbors of neighbors; for example, municipality 26 has neighbor of 86 only; and 86's neighbor is 26, then both these municipalities have no neighbors of neighbors. So my thought is when I run regressions using r-command, "lagsarlm", the regression will treat municipalities without any neighbors of neighbors as missing values, and ignores it. So I use "nb2listw" command with zero.policy = TRUE first, and then use "lagsarlm" command with zero.poly = TRUE later, but when I rn the command "lagsarlm", one error appeared:
"Error in eigen(similar.listw_Matrix(get("listw", envir = env)), only.values = TRUE) : infinite or missing values in 'x'"
It might have something to do with the observations with no neighbors of neighbors. Any help would be greatly appreciated!!!!
Here is my data and codes:
#data is at https://github.com/xu003822/Community-peer-effect/blob/main/geo_control_certi.RData
library(sf)
library(spdep)
library(ggplot2)
library(rgdal)
library(sp)
library(spatialreg)
library(readxl)
library(data.table)
library(ggplot2)
library(splm)
library(mapview)
library(reshape)
library(tidyverse)
load("geo_control_certi.RData")
geo_2019 <- filter(geo_control_certi, year == 2019)
#create a function that using poly2nb to create neighbors and using nearest neigbor to create neigbor for #islands
addnbs <- function(geo_2019){
geo.nb <- poly2nb(geo_2019)
count = card(geo.nb)
if(!any(count==0 | count==1)){
return(geo.nb)
}
## get nearest neighbour index, use centroids:
#getting the nearest neigbor for each municipality#return two nearest neigbor
nnbs = knearneigh(coordinates(geo_2019), k=2)$nn
#nnbs is a matrix, cite the number in it using nnbs[i,j]
#get the index for the municipality that doesn't have a neigbor
# give k nearest neigbor to the muni that have either 0 or 1 neigbor
no_edges_from = which(count==0|count==1)
for(i in no_edges_from){
for (j in 1:2){
geo.nb[[i]][j] = nnbs[i,j]
}
}
return(geo.nb)
}
n2 = addnbs(geo_2019)
instru.nei = n2
#------getting neighbor of neighbor
for(i in 1:length(n2)){
k=1
for(j in 1:length(n2[[i]])){#e.g., n2[[n2[[i]][j]]] = n2[[9]] # x is in (1 38 228 297)# x then is in (1 9 199 297)
for(xx in n2[[n2[[i]][j]]]) {
if (!(xx%in%n2[[i]])&(xx!=i)&(!(xx%in%instru.nei[[i]]))){
instru.nei[[i]][k] = xx
k=k+1
}
}
}
instru.nei[[i]] <- instru.nei[[i]][1:k-1]
}
}
index_zero <- c() # this list will be used to indicate municipality which has no neigbor of neigbor
for (i in 1:324) {
if(length(instru_nofn[[i]])==0)
index_zero <- append(index_zero, i)
}
#give 0 to the neigobr of neigbor to muni that has no neibor of neibor
for (i in index_zero) {
instru_nofn[[i]] = as.integer(0)
}
queen.non <- nb2listw(instru_nofn, zero.policy=TRUE)
#--------spatial durbin model (SAR) using
sar.durbin = lagsarlm(Certified ~ Median_Age + Percent_Children + Percent_Bachelor + Pop_new + Percent_sixty + White + Black + Asian + Hispanic +
Percent_Unemployed + Mean_trave_time + Percent_Manufacture + Median_income + Percent_poverty,
data = geo_2019, listw = queen.non, type="mixed", zero.policy=TRUE)
summary(sar.durbin)