我有一个包含linestrings
描述巴西城市之间连接的 shapefile。我想将这些连接转换为将城市代码设置为行名的邻域对象,使其与我的数据框兼容:
> head(regic_link)
Simple feature collection with 6 features and 6 fields
Geometry type: LINESTRING
Dimension: XY
Bounding box: xmin: -45.7931 ymin: -21.1472 xmax: -41.3903 ymax: -15.9032
proj4string: +proj=longlat +ellps=GRS80 +no_defs
id origin_code nome_ori dest_code nome_dest dist_km geometry
1 14016 3123304 Dores do Turvo 3165701 Senador Firmino 11.732462 LINESTRING (-43.1884 -20.97...
2 14117 3124708 Estrela do Indaiá 3166600 Serra da Saudade 9.080594 LINESTRING (-45.7878 -19.52...
3 15205 3138658 Lontra 3135357 Japonvar 11.104687 LINESTRING (-44.3029 -15.90...
4 17147 3163300 São José do Divino 3144904 Nova Módica 13.066889 LINESTRING (-41.3903 -18.48...
5 17151 3163409 São José do Goiabal 3121803 Dionísio 12.078370 LINESTRING (-42.7077 -19.92...
6 12463 3102100 Alto Rio Doce 3121506 Desterro do Melo 17.982702 LINESTRING (-43.412 -21.025...
因此,行名称将设置为origin_code
,而邻居将dest_code
在列表中设置为,反之亦然(最终这将更改为我创建的索引,但这会使事情更容易检查)。本质上,我需要linestring
以下用于多边形的代码的等效项:
nb.orig <- poly2nb(as_Spatial(shp), row.names = shp$index)
names(nb.orig) <- attr(nb.orig, "region.id")
nb2INLA("output/nb_orig.graph", nb.orig)
(shp
是一个由多边形组成的 shapefile,并且该index
变量同时存在于 shapefile 和数据框中)。
到目前为止,我已经使用sfnetworks
andigraph
包创建了一个邻域对象,但还不能将索引值附加到行名:
regic_net <- as_sfnetwork(regic_link, directed = F)
net_adj <- as_adjacency_matrix(regic_net, names = T)
nb_net <- mat2listw(net_adj)$neighbours
默认情况下,该函数as_sfnetwork
为网络中的每个节点分配一个数字(在边缘数据中,这些是变量 'from' 和 'to' 并且不能使用 mutate 进行更改):
> regic_net
# A sfnetwork with 827 nodes and 3786 edges
#
# CRS: NA
#
# An undirected simple graph with 1 component with spatially explicit edges
#
# Edge Data: 3,786 x 7 (active)
# Geometry type: LINESTRING
# Dimension: XY
# Bounding box: xmin: -50.6938 ymin: -22.855 xmax: -39.9496 ymax: -14.2696
from to origin_code nome_ori dest_code nome_dest geometry
<int> <int> <dbl> <chr> <dbl> <chr> <LINESTRING [°]>
1 1 2 3123304 Dores do Turvo 3165701 Senador Firmino (-43.1884 -20.975, -43.18106 -20.97017, -43.17373 -20.96534, -43.16639 -20.9605, …
2 3 4 3163409 São José do Goiab… 3121803 Dionísio (-42.7077 -19.9255, -42.71344 -19.91851, -42.71917 -19.91152, -42.72491 -19.90453…
3 5 6 3167707 Sobrália 3162609 São João do Orien… (-42.0972 -19.2363, -42.1016 -19.2437, -42.106 -19.2511, -42.11039 -19.2585, -42.…
4 5 7 3167707 Sobrália 3168408 Tarumirim (-42.0972 -19.2363, -42.08899 -19.24038, -42.08079 -19.24447, -42.07258 -19.24855…
5 8 9 3115474 Catuti 3141009 Mato Verde (-42.9607 -15.3612, -42.95243 -15.36428, -42.94415 -15.36735, -42.93588 -15.37043…
6 10 11 3130606 Inconfidentes 3108305 Borda da Mata (-46.328 -22.3174, -46.31896 -22.31505, -46.30993 -22.3127, -46.30089 -22.31034, …
# … with 3,780 more rows
#
# Node Data: 827 x 1
# Geometry type: POINT
# Dimension: XY
# Bounding box: xmin: -50.6938 ymin: -22.855 xmax: -39.9496 ymax: -14.2696
geometry
<POINT [°]>
1 (-43.1884 -20.975)
2 (-43.1004 -20.917)
3 (-42.7077 -19.9255)
# … with 824 more rows
然后,这些值在使用和nb
创建的对象的下一步中用作行名称。有谁知道如何附加我自己的索引甚至提取已创建的索引,以便我可以使其与数据框保持一致?我试图创建一个采用/和/但它们不匹配的转换表,该值是两个索引中的最低值,因此并不总是我数据中的来源。as_adjacency_matrix
mat2listw
from
origin_code
to
dest_code
from
更难的是,比预期多 57 个节点,我不知道为什么会发生这种情况。我检查了重复项,它们在数据中具有相同的坐标和代码/名称,但使用不同的索引号单独处理。
总之,我想将线串对象转换为可用于 INLA 模型的邻域对象,在该模型中,由线连接的城市被视为邻居。我需要为这些城市附加一个索引,以便邻域对象将索引设置为行名,并且它可以与模型中的数据结合。希望这是有道理的,如果没有,请告诉我!