R 中的空间聚类(简单示例)
spatial clustering in R (simple example)
我有这个简单的data.frame
lat<-c(1,2,3,10,11,12,20,21,22,23)
lon<-c(5,6,7,30,31,32,50,51,52,53)
data=data.frame(lat,lon)
思路是根据距离找到空间聚类
首先,我绘制地图 (lon,lat) :
plot(data$lon,data$lat)
很明显我根据点的位置之间的距离得到了三个聚类。
为此,我在 R 中尝试了这段代码:
d= as.matrix(dist(cbind(data$lon,data$lat))) #Creat distance matrix
d=ifelse(d<5,d,0) #keep only distance < 5
d=as.dist(d)
hc<-hclust(d) # hierarchical clustering
plot(hc)
data$clust <- cutree(hc,k=3) # cut the dendrogram to generate 3 clusters
这给出:
现在我尝试绘制相同的点,但使用来自簇的颜色
plot(data$x,data$y, col=c("red","blue","green")[data$clust],pch=19)
这里是结果
这不是我要找的。
其实我想找这样的剧情
感谢您的帮助。
像这样的事情怎么样:
lat<-c(1,2,3,10,11,12,20,21,22,23)
lon<-c(5,6,7,30,31,32,50,51,52,53)
km <- kmeans(cbind(lat, lon), centers = 3)
plot(lon, lat, col = km$cluster, pch = 20)
由于您要对空间数据进行聚类,因此 DBSCAN
最适合您的数据。
您可以使用 fpc 提供的 dbscan()
函数进行聚类,一个 R 包。
library(fpc)
lat<-c(1,2,3,10,11,12,20,21,22,23)
lon<-c(5,6,7,30,31,32,50,51,52,53)
DBSCAN <- dbscan(cbind(lat, lon), eps = 1.5, MinPts = 3)
plot(lon, lat, col = DBSCAN$cluster, pch = 20)
这是一种不同的方法。首先,它假设坐标是 WGS-84 而不是 UTM(平面)。然后它使用层次聚类将给定半径内的所有邻居聚类到同一个聚类(使用方法 = single
,它采用 'friends of friends' 聚类策略)。
为了计算距离矩阵,我使用了包 fields
中的 rdist.earth
方法。此包的默认地球半径是 6378.388(赤道半径),这可能不是人们想要的,因此我将其更改为 6371。有关详细信息,请参阅 this article。
library(fields)
lon = c(31.621785, 31.641773, 31.617269, 31.583895, 31.603284)
lat = c(30.901118, 31.245008, 31.163886, 30.25058, 30.262378)
threshold.in.km <- 40
coors <- data.frame(lon,lat)
#distance matrix
dist.in.km.matrix <- rdist.earth(coors,miles = F,R=6371)
#clustering
fit <- hclust(as.dist(dist.in.km.matrix), method = "single")
clusters <- cutree(fit,h = threshold.in.km)
plot(lon, lat, col = clusters, pch = 20)
如果您不知道聚类的数量(例如 k-means 选项),这可能是一个很好的解决方案,并且与 minPts = 1 的 dbscan 选项有些相关。
---编辑---
与原始数据:
lat<-c(1,2,3,10,11,12,20,21,22,23)
lon<-c(5,6,7,30,31,32,50,51,52,53)
data=data.frame(lat,lon)
dist <- rdist.earth(data,miles = F,R=6371) #dist <- dist(data) if data is UTM
fit <- hclust(as.dist(dist), method = "single")
clusters <- cutree(fit,h = 1000) #h = 2 if data is UTM
plot(lon, lat, col = clusters, pch = 20)
我有这个简单的data.frame
lat<-c(1,2,3,10,11,12,20,21,22,23)
lon<-c(5,6,7,30,31,32,50,51,52,53)
data=data.frame(lat,lon)
思路是根据距离找到空间聚类
首先,我绘制地图 (lon,lat) :
plot(data$lon,data$lat)
很明显我根据点的位置之间的距离得到了三个聚类。
为此,我在 R 中尝试了这段代码:
d= as.matrix(dist(cbind(data$lon,data$lat))) #Creat distance matrix
d=ifelse(d<5,d,0) #keep only distance < 5
d=as.dist(d)
hc<-hclust(d) # hierarchical clustering
plot(hc)
data$clust <- cutree(hc,k=3) # cut the dendrogram to generate 3 clusters
这给出:
现在我尝试绘制相同的点,但使用来自簇的颜色
plot(data$x,data$y, col=c("red","blue","green")[data$clust],pch=19)
这里是结果
这不是我要找的。
其实我想找这样的剧情
感谢您的帮助。
像这样的事情怎么样:
lat<-c(1,2,3,10,11,12,20,21,22,23)
lon<-c(5,6,7,30,31,32,50,51,52,53)
km <- kmeans(cbind(lat, lon), centers = 3)
plot(lon, lat, col = km$cluster, pch = 20)
由于您要对空间数据进行聚类,因此 DBSCAN
最适合您的数据。
您可以使用 fpc 提供的 dbscan()
函数进行聚类,一个 R 包。
library(fpc)
lat<-c(1,2,3,10,11,12,20,21,22,23)
lon<-c(5,6,7,30,31,32,50,51,52,53)
DBSCAN <- dbscan(cbind(lat, lon), eps = 1.5, MinPts = 3)
plot(lon, lat, col = DBSCAN$cluster, pch = 20)
这是一种不同的方法。首先,它假设坐标是 WGS-84 而不是 UTM(平面)。然后它使用层次聚类将给定半径内的所有邻居聚类到同一个聚类(使用方法 = single
,它采用 'friends of friends' 聚类策略)。
为了计算距离矩阵,我使用了包 fields
中的 rdist.earth
方法。此包的默认地球半径是 6378.388(赤道半径),这可能不是人们想要的,因此我将其更改为 6371。有关详细信息,请参阅 this article。
library(fields)
lon = c(31.621785, 31.641773, 31.617269, 31.583895, 31.603284)
lat = c(30.901118, 31.245008, 31.163886, 30.25058, 30.262378)
threshold.in.km <- 40
coors <- data.frame(lon,lat)
#distance matrix
dist.in.km.matrix <- rdist.earth(coors,miles = F,R=6371)
#clustering
fit <- hclust(as.dist(dist.in.km.matrix), method = "single")
clusters <- cutree(fit,h = threshold.in.km)
plot(lon, lat, col = clusters, pch = 20)
如果您不知道聚类的数量(例如 k-means 选项),这可能是一个很好的解决方案,并且与 minPts = 1 的 dbscan 选项有些相关。
---编辑---
与原始数据:
lat<-c(1,2,3,10,11,12,20,21,22,23)
lon<-c(5,6,7,30,31,32,50,51,52,53)
data=data.frame(lat,lon)
dist <- rdist.earth(data,miles = F,R=6371) #dist <- dist(data) if data is UTM
fit <- hclust(as.dist(dist), method = "single")
clusters <- cutree(fit,h = 1000) #h = 2 if data is UTM
plot(lon, lat, col = clusters, pch = 20)