我通过以下方式使用MASS的kde2d函数创建了一系列2D直方图:
# Loading libraries
library(MASS)
library(RcolorBrewer)
# Loading data
data <- as.matrix(read.table('data.dat'))
# Create the 2dhist object
hist_2d <- kde2d(data[,1],data[,2],n = 60, lims=c(-180,180,-180,180))
# Define the color palette
rf <- colorRampPalette(rev(brewer.pal(11,'Spectral')))
r <- rf(60)
# Defining the axis
at_x = seq(-180,180,by=30)
at_y = seq(-180,180,by=30)
# Plot the 2DHistogram
image(hist_2d,col=r,cex.main=3,main='Q68L',axes=F)
axis(1,lwd.ticks=2,at=at_x,labels=T,cex.axis=2)
axis(2,lwd.ticks=2,at=at_y,labels=T,cex.axis=2)
直方图这样生成looks。如何识别所有高密度区域(我在white squares内标记了这些区域)?解决此问题的理想方法是针对每个高密度区域抛出一个(x,y)范围的函数,以便可以将其应用于多个数据集中。
在此先感谢您是否需要其他信息
答案 0 :(得分:0)
有了正确的数据表示,就可以做到
聚类分析。由于您不提供数据,因此我将举例说明
kde2d
帮助页面上使用的数据-间歇泉数据。
该数据可以很清晰地分离出“高密度”
区域(例如您的示例图片),因此我将仅使用一个简单的区域
k均值聚类。
library(MASS)
attach(geyser)
f2 <- kde2d(duration, waiting, n = 50, lims = c(0.5, 6, 40, 100),
h = c(width.SJ(duration), width.SJ(waiting)) )
image(f2, zlim = c(0, 0.05))
我们需要找到“热点”。为了了解一下 什么值应该被认为是“高”,我们可以看一个箱线图。
boxplot(as.vector(f2$z))
基于此,我会随意使用一些点, z值大于0.012。您需要对此进行调整 您的特定问题。
Hot = which(f2$z > 0.012, arr.ind = TRUE)
HotPoints = data.frame(x=f2$x[Hot[,1]], y=f2$y[Hot[,2]])
plot(HotPoints, pch=20, xlim = c(0.5,6), ylim = c(40,100))
现在我们需要聚类点并找到x和y范围 对于集群。首先,我简单地说明一下 结果是合理的。
KM3 = kmeans(scale(HotPoints), 3)
plot(HotPoints, pch=20, xlim = c(0.5,6), ylim = c(40,100))
for(i in 1:3) {
Rx = range(HotPoints[KM3$cluster == i,1])
Ry = range(HotPoints[KM3$cluster == i,2])
polygon(c(Rx, rev(Rx)), rep(Ry, each=2))
}
我不确定您希望如何将结果呈现给您, 但是将它们全部集中到一个位置的一种方法是:
XRanges = sapply(unique(KM3$cluster),
function(i) range(HotPoints[KM3$cluster == i,1]))
XRanges
[,1] [,2] [,3]
[1,] 3.979592 3.867347 1.734694
[2,] 4.877551 4.316327 2.071429
YRanges = sapply(unique(KM3$cluster),
function(i) range(HotPoints[KM3$cluster == i,2]))
YRanges
[,1] [,2] [,3]
[1,] 47.34694 70.61224 73.06122
[2,] 62.04082 87.75510 95.10204
这给出了三个群集中每个群集的x和y的最小值和最大值。
但是,我在这里做了一些选择,我想指出
我仍然为你留了一些工作。 您 还需要做什么:
1.您需要选择密度的临界点
需要成为一个集群。
2.给定截止点以上的点,您需要说
您要生成多少个群集。
其余的机器在那里。