目的:我想要在坐标对之间建立一个不同的矩阵。我想用这个矩阵作为输入,使用Moran's I (LISA)和后者在地理加权回归(GWR)中计算局部空间簇。
问题:我知道我可以用dnearneigh{spdep}来计算距离矩阵。但是,我想使用我已经估计过的多边形之间的旅行时间。在实践中,我认为这就像输入一个不同的矩阵,根据另一个特征来表示多边形之间的距离/差异。我已经尝试将我的矩阵输入到dnearneigh{spdep},但是我得到了错误Error: ncol(x) == 2 is not TRUE
dist_matrix <- dnearneigh(diss_matrix_invers, d1=0, d2=5, longlat = F, row.names=rn)有什么建议吗?下面是一个可重复的例子:
编辑:进一步挖掘,我认为我可以使用mat2listw{spdep},但我仍然不确定它是否保持了矩阵和多边形之间的对应关系。如果我添加row.names = T,它将返回一个错误row.names wrong length :(
listw_dissi <- mat2listw(diss_matrix_invers)
lmoran <- localmoran(oregon.tract@data$white, listw_dissi,
zero.policy=T, alternative= "two.sided")可复制示例
library(UScensus2000tract)
library(spdep)
library(ggplot2)
library(dplyr)
library(reshape2)
library(magrittr)
library(data.table)
library(reshape)
library(rgeos)
library(geosphere)
# load data
data("oregon.tract")
# get centroids as a data.frame
centroids <- as.data.frame( gCentroid(oregon.tract, byid=TRUE) )
# Convert row names into first column
setDT(centroids, keep.rownames = TRUE)[]
# create Origin-destination pairs
od_pairs <- expand.grid.df(centroids, centroids) %>% setDT()
colnames(od_pairs) <- c("origi_id", "long_orig", "lat_orig", "dest_id", "long_dest", "lat_dest")
# calculate dissimilarity between each pair.
# For the sake of this example, let's use ellipsoid distances. In my real case I have travel-time estimates
od_pairs[ , dist := distGeo(matrix(c(long_orig, lat_orig), ncol = 2),
matrix(c(long_dest, lat_dest), ncol = 2))]
# This is the format of how my travel-time estimates are organized, it has some missing values which include pairs of origin-destination that are too far (more than 2hours apart)
od_pairs <- od_pairs[, .(origi_id, dest_id, dist)]
od_pairs$dist[3] <- NA
> origi_id dest_id dist
> 1: oregon_0 oregon_0 0.00000
> 2: oregon_1 oregon_0 NA
> 3: oregon_2 oregon_0 39874.63673
> 4: oregon_3 oregon_0 31259.63100
> 5: oregon_4 oregon_0 33047.84249
# Convert to matrix
diss_matrix <- acast(od_pairs, origi_id~dest_id, value.var="dist") %>% as.matrix()
# get an inverse matrix of distances, make sure diagonal=0
diss_matrix_invers <- 1/diss_matrix
diag(diss_matrix_invers) <- 0计算简单距离矩阵
# get row names
rn <- sapply(slot(oregon.tract, "polygons"), function(x) slot(x, "ID"))
# get centroids coordinates
coords <- coordinates(oregon.tract)
# get distance matrix
diss_matrix <- dnearneigh(diss_matrix_invers, d1=0, d2=5, longlat =T, row.names=rn)
class(diss_matrix)
> [1] "nb"现在如何在这里使用我的diss_matrix_invers?
发布于 2017-04-22 14:45:40
关于matlistw{spdep}的使用,您是正确的。默认情况下,函数保留行名以保持矩阵之间的对应关系。您还可以这样指定row.names:
listw_dissi <- mat2listw(diss_matrix_invers, row.names = row.names(diss_matrix_invers)) 创建的列表将包含邻居的适当名称及其作为权重的距离。你可以通过看看邻居来检查这个。
listw_dissi$neighbours[[1]][1:5]你应该可以直接用这个来计算莫兰的I。
dnearneigh{sdep}
您不可能在dnearneigh{spdep}中使用diss_matrix,因为这个函数接受一个坐标列表。
但是,如果需要使用自己的距离矩阵(旅行时间)定义一组给定距离阈值(d1,d2)的邻居。我认为这个函数可以起作用。
dis.neigh<-function(x, d1 = 0, d2=50){
#x must be a symmetrical distance matrix
#create empty list
style = "M" #for style unknown
neighbours<-list()
weights<-list()
#set attributes of neighbours list
attr(neighbours, "class")<-"nb"
attr(neighbours, "distances")<-c(d1,d2)
attr(neighbours, "region.id")<-colnames(x)
#check each row for neighbors that satisfy distance threshold
neighbour<-c()
weight<-c()
i<-1
for(row in c(1:nrow(x))){
j<-1
for(col in c(1:ncol(x))){
if(x[row,col]>d1 && x[row,col]<d2){
neighbour[j]<-col
weight[j]<-1/x[row,col] #inverse distance (dissimilarity)
j<-1+j
}
}
neighbours[i]<-list(neighbour)
weights[i]<-list(weight)
i<-1+i
}
#create neighbour and weight list
res <- list(style = style, neighbours = neighbours, weights = weights)
class(res) <- c("listw", "nb")
attr(res, "region.id") <- attr(neighbours, "region.id")
attr(res, "call") <- match.call()
return(res)
}然后像这样使用它:
nb_list<-dis.neigh(diss_matrix, d1=0, d2=10000)
lmoran <- localmoran(oregon.tract@data$white, nb_lists, alternative= "two.sided")https://stackoverflow.com/questions/43557993
复制相似问题