添加一条曲线,该曲线适合R中曲线的峰值? [英] add a curve that fits the peaks from a plot in R?

查看:109
本文介绍了添加一条曲线,该曲线适合R中曲线的峰值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果给定了两个矢量及其图,是否存在添加适合峰的曲线的函数? 例如,我有:

Is there a function that adds a curve that fits the peaks if given two vectors and their plot? For example, I have:

x = c(0:20)

x= c(0:20)

x [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

x [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

y [1] 19.4 17.9 8.1 11.3 7.8 8.0 5.0 1.7 3.9 5.4 7.5 5.4 4.7 5.0 4.9 3.5 2.9 2.4 1.4 1.7

y [1] 19.4 17.9 8.1 11.3 7.8 8.0 5.0 1.7 3.9 5.4 7.5 5.4 4.7 5.0 4.9 3.5 2.9 2.4 1.4 1.7

图(x,y,xlim = range(x),y​​lim = range(y))

plot(x,y,xlim=range(x),ylim=range(y))

最好, 七海

推荐答案

从数学上讲,您的问题定义很不明确.您为y值提供了一系列离散值而不是函数.这意味着无法区分找到局部最大值.

Mathematically speaking, your problem is very poorly defined. You supply a range of discrete values, not a function, for your y values. This means it can not be differentiated to find local maxima.

也就是说,这里有一些代码可能会让您入门.它利用了称为peaks的函数((归因于Brian Ripley ):

That said, here is a bit of code that might get you started. It makes use of a function called peaks, (attributed to Brian Ripley):

peaks<-function(series,span=3){
  z <- embed(series, span)
  s <- span%/%2
  v<- max.col(z) == 1 + s
  result <- c(rep(FALSE,s),v)
  result <- result[1:(length(result)-s)]
  result
} 

x <- c(1:20)
y <- c(19.4, 17.9, 8.1, 11.3, 7.8, 8.0, 5.0, 1.7, 3.9, 
       5.4, 7.5, 5.4, 4.7, 5.0, 4.9, 3.5, 2.9, 2.4, 1.4, 1.7)

plot(x,y, type="l")
p <- which(peaks(y, span=3))

lines(x[p], y[p], col="red", type="b)

问题在于局部峰的概念定义不明确.您是什么意思?提供的峰值算法使您可以修改span.玩一玩,看看是否有帮助.

The problem is that the concept of local peaks is poorly defined. How local do you mean? The peaks algorithm as supplied allows you to modify the span. Have a play and see whether it is helpful at all.

这篇关于添加一条曲线,该曲线适合R中曲线的峰值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆