计算R中多峰分布的模数 [英] Calculate the modes in a multimodal distribution in R
本文介绍了计算R中多峰分布的模数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如您所见,不同的高度不是沿轴均匀分布的,而是围绕某些值聚集的。
数据的历史图和密度图如下所示(下面绘制了两个密度估计值this answer):
如您所见,这是一个多模式分布。
如何计算模式(以R表示)?
这里是供您使用的原始数据:
mm <- c(418, 527, 540, 553, 554, 558, 613, 630, 634, 636, 645, 648, 708, 714, 715, 725, 806, 807, 822, 823, 836, 837, 855, 903, 908, 910, 911, 913, 915, 923, 935, 945, 955, 957, 958, 1003, 1006, 1015, 1021, 1021, 1022, 1034, 1043, 1048, 1051, 1054, 1058, 1100, 1102, 1103, 1117, 1125, 1134, 1138, 1145, 1146, 1150, 1152, 1210, 1211, 1213, 1223, 1226, 1334)
推荐答案
我使用您的mm数据自己构造了一些东西。
首先,让我们绘制mm的密度图,以直观地显示模式:
plot(density(mm))
所以,我们可以看到在这个分布中有两个模式。一个在600左右,一个在1000左右。让我们看看如何找到它们。
为了查找模式索引,我执行了以下函数:
find_modes<- function(x) {
modes <- NULL
for ( i in 2:(length(x)-1) ){
if ( (x[i] > x[i-1]) & (x[i] > x[i+1]) ) {
modes <- c(modes,i)
}
}
if ( length(modes) == 0 ) {
modes = 'This is a monotonic distribution'
}
return(modes)
}
让我们在我们的密度上试试:
mymodes_indices <- find_modes(density(mm)$y) #you need to try it on the y axis
现在mymodes_indices
包含我们的模式的索引,即:
> density(mm)$y[mymodes_indices] #just to confirm that those are the correct
[1] 0.0008946929 0.0017766183
> density(mm)$x[mymodes_indices] #the actual modes
[1] 660.2941 1024.9067
希望能有所帮助!
这篇关于计算R中多峰分布的模数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文