如何从图中找到R中的局部最大值 [英] How to find local maximum in R from graph

查看:551
本文介绍了如何从图中找到R中的局部最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的编码有问题.我应该

I'm having a problem with my coding. I'm supposed to

  1. 绘制该方程式的图[y = f(x)],其中f(x)=(10 *((x-1)^ 2)^(1/3))/(x ^ 2 + 9)表示介于-5和5之间的x的10001个值

  1. Plot a graph of this equation [y=f(x)] where f(x) = (10*((x-1)^2)^(1/3))/(x^2 + 9) for 10001 values of x between (and including) -5 and 5

在a中的10001个x值中,找到f(x)的两个局部最大值.

Among the 10001 x values in a, find the two local maximums of f(x).

我尝试这样做:

# question1
x <- seq(-5,5,length=10001)
y <- (10*((x-1)^2)^(1/3))/(x^2 + 9)

plot(x,y) # This will produce a graph with two max point

# question2
x[which.max(y)]
y[which.max(y)]

但是,我只能得到最大点之一的坐标,而对如何获得另一个最大点却一无所知.

however, i only get the coordinates one of the maximum point and clueless as to how do i get another maximum point.

推荐答案

您可以使用软件包ggpmisc中的find_peaks.

You can use find_peaks from package ggpmisc.

library(ggpmisc)
x[ggpmisc:::find_peaks(df$y)]
y[ggpmisc:::find_peaks(df$y)]

输出为:

[1] -1.5  3.0
[1] 1.6373473 0.8818895

请注意,函数find_peaks被标记为 internal .因此,您需要使用:::来访问它.

Note that the function find_peaks is noted as internal. You therefore need to access it using :::.

您可以使用spanstrict参数进一步参数化对find_peaks的调用.有关详细信息,请参见??find_peaks.

You can further parametrize the call to find_peaks using the span and strict argument. See ??find_peaks for details.

您也可以使用ggplot2ggpmisc软件包直接绘制此图:

You can also directly plot this using the ggplot2 and ggpmisc packages:

x <- seq(-5,5,length=10001)
y <- (10*((x-1)^2)^(1/3))/(x^2 + 9)
df <- data.frame(x = x, y = y)
ggplot(data = df, aes(x = x, y = y)) + geom_line() + stat_peaks(col = "red")

这篇关于如何从图中找到R中的局部最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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