Findpeaks函数由于未知原因错过峰 [英] Findpeaks function misses peaks for an unknown reason
问题描述
我有一些数据试图在其中找到峰.它有23个峰,这些峰在数据中可见.但是,findpeaks函数似乎只能识别其中的21个,但我不知道为什么.
I’ve got some data that I’m trying to locate the peaks in. It’s got 23 peaks, which are visible in the data. However, the findpeaks function only seems to be able to identify 21 of them, but I can’t work out why.
我非常感谢您提供调试和获取最后两个高峰的帮助.
I’d be very grateful for some help debugging and picking up the last two peaks.
data <- data.frame(time = c(1669, 1670, 1671, 1672, 1673, 1674, 1675, 1676, 1677, 1923,
1924, 1925, 1926, 1927, 1928, 1929, 2176, 2177, 2178, 2179, 2180,
2181, 2182, 2428, 2429, 2430, 2431, 2432, 2433, 2434, 2435, 2687,
2688, 2689, 2690, 2691, 2692, 2693, 2694, 3202, 3203, 3204, 3205,
3206, 3207, 3208, 3209, 3730, 3731, 3732, 3733, 3734, 3735, 3736,
3737, 3738, 4258, 4259, 4260, 4261, 4262, 4263, 4264, 4265, 4266,
4267, 4268, 4269, 4789, 4790, 4791, 4792, 4793, 4794, 4795, 4796,
4797, 4798, 4799, 4800, 5325, 5326, 5327, 5328, 5329, 5330, 5331,
5332, 5333, 5334, 5335, 5336, 5337, 5849, 5850, 5851, 5852, 5853,
5854, 5855, 5856, 5857, 5858, 5859, 5860, 5861, 6096, 6097, 6098,
6099, 6100, 6101, 6102, 6103, 6104, 6105, 6106, 6107, 6108, 6109,
6110, 6354, 6355, 6356, 6357, 6358, 6359, 6360, 6361, 6362, 6363,
6364, 6365, 6366, 6367, 6857, 6858, 6859, 6860, 6861, 6862, 6863,
6864, 6865, 6866, 6867, 6868, 6869, 6870, 6871, 6872, 7346, 7347,
7348, 7349, 7350, 7351, 7352, 7353, 7354, 7355, 7356, 7357, 7358,
7359, 7360, 7361, 7362, 7816, 7817, 7818, 7819, 7820, 7821, 7822,
7823, 7824, 7825, 7826, 7827, 7828, 7829, 7830, 7831, 7832, 7833,
7834, 7835, 7836, 8264, 8265, 8266, 8267, 8268, 8269, 8270, 8271,
8272, 8273, 8274, 8275, 8276, 8277, 8278, 8279, 8280, 8281, 8282,
8283, 8284, 8695, 8696, 8697, 8698, 8699, 8700, 8701, 8702, 8703,
8704, 8705, 8706, 8707, 8708, 8709, 8710, 8711, 8712, 8713, 8714,
8715, 8716, 9091, 9092, 9093, 9094, 9095, 9096, 9097, 9098, 9099,
9100, 9101, 9102, 9103, 9104, 9105, 9106, 9107, 9108, 9109, 9110,
9111, 9460, 9461, 9462, 9463, 9464, 9465, 9466, 9467, 9468, 9469,
9470, 9471, 9472, 9473, 9474, 9475, 9476, 9477, 9478, 9479, 9799,
9800, 9801, 9802, 9803, 9804, 9805, 9806, 9807, 9808, 9809, 9810,
9811, 9812, 9813, 9814, 9815, 9816, 9817, 9818, 9819, 10102,
10103, 10104, 10105, 10106, 10107, 10108, 10109, 10110, 10111,
10112, 10113, 10114, 10115, 10116, 10117, 10118, 10119, 10120,
10121, 10122, 10123, 10124, 10377, 10378, 10379, 10380, 10381,
10382, 10383, 10384, 10385, 10386, 10387, 10388, 10389, 10390,
10391, 10392, 10393, 10394, 10395, 10396, 10397, 10398, 10399,
10400),
peak=c(698, 1022, 1369, 1514, 1510, 1298, 993, 726, 526, 820, 1177,
1418, 1495, 1327, 1000, 705, 860, 1222, 1499, 1531, 1349, 1031,
707, 538, 874, 1176, 1379, 1380, 1204, 893, 621, 550, 841, 1172,
1402, 1472, 1324, 1025, 732, 750, 1043, 1277, 1380, 1313, 1117,
859, 624, 707, 966, 1192, 1325, 1314, 1201, 1002, 756, 542, 500,
626, 734, 875, 982, 1058, 1045, 981, 860, 739, 621, 500, 500,
622, 767, 908, 1011, 1082, 1110, 1061, 975, 836, 701, 564, 538,
677, 823, 969, 1061, 1117, 1129, 1091, 1038, 902, 779, 645, 525,
583, 699, 837, 929, 1020, 1050, 1062, 1059, 996, 904, 802, 681,
576, 530, 636, 749, 864, 954, 1032, 1084, 1108, 1080, 1018, 944,
850, 747, 637, 526, 566, 665, 755, 852, 908, 983, 1009, 1006,
976, 918, 848, 757, 669, 577, 532, 618, 709, 787, 873, 934, 992,
1004, 996, 972, 919, 848, 753, 679, 592, 514, 566, 636, 722,
786, 860, 905, 935, 962, 949, 943, 906, 859, 795, 729, 650, 563,
501, 502, 552, 640, 702, 763, 841, 895, 945, 992, 1022, 1043,
1050, 1045, 1024, 961, 902, 838, 775, 706, 622, 555, 502, 582,
652, 724, 798, 871, 938, 974, 1035, 1053, 1066, 1079, 1055, 1015,
985, 927, 863, 764, 718, 637, 545, 542, 618, 689, 761, 818, 892,
934, 988, 1005, 1037, 1030, 1022, 1001, 969, 930, 875, 813, 752,
680, 628, 557, 501, 511, 578, 625, 688, 743, 786, 825, 854, 887,
903, 916, 894, 884, 871, 825, 791, 747, 696, 632, 582, 525, 525,
576, 637, 672, 718, 746, 789, 830, 822, 828, 835, 831, 802, 781,
743, 697, 661, 625, 551, 515, 548, 597, 643, 694, 754, 792, 830,
866, 874, 889, 890, 896, 862, 849, 818, 771, 739, 684, 625, 570,
517, 524, 573, 632, 698, 736, 797, 848, 877, 898, 941, 948, 943,
944, 916, 893, 881, 823, 792, 749, 696, 648, 588, 541, 504, 559,
626, 671, 712, 764, 818, 844, 864, 906, 915, 920, 929, 912, 889,
857, 838, 796, 767, 712, 656, 627, 569, 534),
colour="red")
library(pracma)
peaks <- data.frame(findpeaks(data$peak, npeaks=23, threshold=100, peakpat = "[+]{1,}[0]*[-]{1,}", sortstr=TRUE))
data$n <- seq(1,length(data$peak))
data <- merge(x=data, y=peaks, by.x="n", by.y="X2", all.x=TRUE, all.y=TRUE)
ggplot(data, aes(x=time, y=peak, fill=colour, colour=colour)) +
geom_col(orientation="x") +
geom_point(aes(x=time, y=X1))
推荐答案
阈值
参数在 findpeaks
中存在错误.如果将其设置为 0
,则会找到所有25个峰.
There is a bug in findpeaks
with the threshold
parameter. If you set this to 0
you will find all 25 peaks.
findpeaks(data$peak, peakpat = "[+]{1,}[0]*[-]{1,}",
threshold = 0, npeaks = 0)
并设置 minpeakdistance = 5
将返回您正在寻找的23个峰值(至少我希望如此).
And setting minpeakdistance = 5
will return the 23 peaks you were looking for (at least I hope so).
我认为该软件包的下一版本将提供经过纠正的 findpeaks
函数.
I assume a corrected findpeaks
function will be provided with the next version of the package.
PS:
我认为这不是Q& D工具.我知道它已经为许多用户很好地使用了.当然,其他软件包中可能会有更好,更可靠的峰发现功能.
PS:
I don't think this is a Q&D tool. I know it has served well for a lot of users. Of course, there may be better and more reliable peak finding functions in other packages.
这篇关于Findpeaks函数由于未知原因错过峰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!