使用空间包获取r中邻居的邻居



我试图使用新泽西州市政当局邻居的邻居特征作为工具变量。我使用的是空间durbin模型。但是,当一些市政当局没有邻居的时候,我会遇到一些错误。

我获取邻居的一种方法是使用poly2nb函数:geo.nb<-poly2nb(geo_2019(;其中geo_2019是我的数据集;如果有一个岛,我使用k最近邻,其中k=2;

我可以为邻居的邻居创建一个.nb列表。但有些城市没有邻居的邻居;例如26市只有86个邻居;86的邻居是26,那么这两个城市都没有邻居的邻居。所以我的想法是,当我使用r命令运行回归时;lagsarlm";,回归将把没有邻居邻居的市政当局视为缺失值,并忽略它;nb2listw";policy=TRUE的命令,然后使用";lagsarlm";poly=TRUE,但当我收到命令"时;lagsarlm";,出现一个错误:

"特征错误(类似.listw_Matrix(get("listw",envir=env((,仅.values=TRUE(:"x"中的值为无穷大或缺失;

这可能与没有邻居的观察有关。任何帮助都将不胜感激!!!!

这是我的数据和代码:

#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)

如果没有一个可复制的示例,就没有什么可做的。您可以创建两个单独的邻居列表,一个使用knn,另一个使用邻接。然后你可以把他们两个结合起来。这应该(几乎(保证至少有一个邻居。

library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(spdep)
#> Loading required package: sp
#> Loading required package: spData
#> To access larger datasets in this package, install the spDataLarge
#> package with: `install.packages('spDataLarge',
#> repos='https://nowosad.github.io/drat/', type='source')`
library(sfdep)

df <- st_as_sf(Guerry::gfrance85)
nb_contiguity <- st_contiguity(df)
nb_k1 <- st_knn(st_centroid(df), 1)
#> Warning in st_centroid.sf(df): st_centroid assumes attributes are constant over
#> geometries of x
nb_mixed <- nb_union(nb_contiguity, nb_k1)
wt <- st_weights(nb_mixed)
listw <- recreate_listw(nb_mixed, wt)
spatialreg::lagsarlm(
Crime_pers ~ Infanticide + Donations,
data = df, 
listw = listw
)
#> Warning in spatialreg::lagsarlm(Crime_pers ~ Infanticide + Donations, data = df, : inversion of asymptotic covariance matrix failed for tol.solve = 2.22044604925031e-16 
#>   reciprocal condition number = 2.70745e-17 - using numerical Hessian.
#> 
#> Call:
#> spatialreg::lagsarlm(formula = Crime_pers ~ Infanticide + Donations, 
#>     data = df, listw = listw)
#> Type: lag 
#> 
#> Coefficients:
#>          rho  (Intercept)  Infanticide    Donations 
#> 6.188676e-01 4.353576e+03 6.505381e+01 1.955467e-02 
#> 
#> Log likelihood: -859.8795

创建于2022-09-22由reprex包(v2.0.1(

最新更新