2017-05-03 61 views
0

我已經使用mat2listw{spdep}創建了一個權重列表對象,我稍後將在空間迴歸中使用它。我想從這個權重列表對象中檢索用於創建它的多邊形的ID。 是否有可能從對象中恢復此信息?從權重列表對象spdep中提取空間ID R

這裏是一個重複的例子:

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

這可能工作: - '屬性(my_weights)$ region.id' – ahly

+0

@ahly,它完美的作品。請發表您的評論作爲答案,以便我可以接受它。謝謝。 –

回答

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"