I am trying to find the local maxima for a probability density function (found using R's density
method). I cannot do a simple "look around neighbors" method (where one looks around a point to see if it's a local maximum with respect to its neighbors) as there is a large volume of data. Furthermore, it seems more efficient and generic to use something like Spline interpolation and then find the roots of the 1st derivative, as opposed to building a "look around neighbors" with fault tolerance and other parameters.
So, my questions:
- Given a function from
splinefun
, what methods will find the local maxima? - Is there an easy / standard way to find derivatives of a function returned using
splinefun
? - Is there a better/standard way to find the local maxima of a probability density function ?
For reference, below is a plot of my density function. Other density functions I'm working with are similar in form. I should say that I am new to R, but not new to programming, so there may be a standard library or package for achieving what I need.
Thanks for your help!!
density()
doesn't estimate the density for every datum, it estimates the density at n values, where n is a user-specified parameter with default value n = 512. $\endgroup$msExtrema {msProcess}
) and was only able to identify a few of the maximums, never all, by playing with the tolerance settings. $\endgroup$msExtrema
, it's a simple wrapper forpeaks
from thesplus2R
package, which you'd be better off using directly if you only want the local maxima and not the local minima. I can't see why using the defaultspan=3
wouldn't find all the local maxima. And 2^15=32768 shouldn't be large enough for efficiency to be a big worry. $\endgroup$peaks
appears to be buggy: It callsmax.col
with the default setting ofties.method = "random"
, which not only breaks ties at random but also sets a relative tolerance of 1e-5 for declaring a tie. The former is confusing, the latter is definitely not what you want here.peaks()
also takes astrict
parameter that is poorly documented and, looking at the function's code, does nothing. Ah, the joys of user-contributed software libraries! You might well be able to fix it though, as you say you're not new to programming, $\endgroup$