首页 文章

将多个峰拟合到数据集并提取R中的单个峰信息

提问于
浏览
2

我有许多数据集,包括不同高程的特征计数 . 目前,每1米间隔的数据为1-30米 . 绘制时,我的许多数据集都显示3-4个峰值,这些峰值表示高度层 .

这是一个示例数据集:

高度< - c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22, 23,24,25,26,27,28,29,30)计数<-c(4000,2000,500,300,200,100,0,0,400,700,800,800,500,1000,1500,2000,2500,2200,1700,1100,500,0,0 ,1000,1500,2000,3000,4000,4000,2000)

我想对这些数据集采用某种方式的曲线函数,以确定“峰值”的总数,峰值中心位置(即高度)和峰值宽度 . 我可以通过前一段时间使用fityk软件手动拟合多个高斯函数来执行这种分析,但我想知道是否可以通过R自动执行这样的过程?

我已经探索了一些关于将峰值拟合到直方图的其他帖子,例如通过mixtools包,但是我不知道你是否可以提取单个峰值信息 .

您将提供的任何帮助将不胜感激 .

2 回答

  • 3

    "How do I fit a curve to my data"是一个过于宽泛的问题,因为有无数种方法可以做到这一点 . 它也可能比这里更适合https://stats.stackexchange.com/ . 但是,基础R的 ksmooth 是基本平滑器的一个很好的起点:

    plot(Height,Counts)
    smoothCounts<-ksmooth(Height,Counts,kernel="normal",bandwidth=2)
    dsmooth<-diff(smoothCounts$y)
    locmax<-sign(c(0,dsmooth))>0 & sign(c(dsmooth,0))<0
    lines(smoothCounts)
    points(smoothCounts$x[locmax],smoothCounts$y[locmax],cex=3,c=2)
    

    enter image description here

  • 1

    简单的峰识别可以是以下几行 . 看起来合理?

    library(data.table)
    
    dt <- data.table(
    Height = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30),
    Counts = c(4000,2000,500,300,200,100,0,0,400,700,800,800,500,1000,1500,2000,2500,2200,1700,1100,500,0,0,1000,1500,2000,3000,4000,4000,2000)
    )
    
    # crude dHeights/dCounts
    dt[,d1 := c(NA,diff(Counts))]
    # previous crude dHeights/dCounts (d2Heights/dCounts2 will be even more crude so comparing change in dHeight/dCounts instead)
    dt[,d2 := c(tail(d1,-1),NA)]
    
    # local maxima
    dtpeaks <- dt[d1 >=0 & d2 <=0]
    

    我不太确定如何计算峰值的FWHM,如果你能解释这个过程那么我应该能够提供帮助 .

相关问题