Извлечь пространственные идентификаторы из объекта списка весов, spdep R

Я использовал mat2listw{spdep} создать объект списка весов, который я позже буду использовать в пространственной регрессии. Я хотел бы извлечь из этого объекта списка весов идентификаторы полигонов, использованных для его создания. Можно ли восстановить эту информацию с объекта?

Вот воспроизводимый пример:

library(spdep)
library(UScensus2000tract)

# load data
  data("oregon.tract")

# get coordinates of centroids
  coords <- coordinates(oregon.tract)

# calculate a simple distance matrix between coordinates
  d <- dist(coords) 
  d <- as.matrix(d)

# Calculate Spatial weights Matrix (travel time)
  my_weights <- mat2listw(d, row.names = row.names(oregon.tract))

# Now I'd like to extract from my_weights the polygon ids

1 ответ

Решение
library(spdep)
library(UScensus2000tract)

# load data
  data("oregon.tract")

# get coordinates of centroids
  coords <- coordinates(oregon.tract)

# calculate a simple distance matrix between coordinates
  d <- dist(coords) 
  d <- as.matrix(d)

# Calculate Spatial weights Matrix (travel time)
  my_weights <- mat2listw(d, row.names = row.names(oregon.tract))

# Extract region ids from attributes of my_weights
region.ids <- attributes(my_weights)$region.id

head(region.ids)
[1] "oregon_0" "oregon_1" "oregon_2" "oregon_3" "oregon_4" "oregon_5"
Другие вопросы по тегам