[R] find multiple mode, sorry for not providing enough information

Abby Spurdle @purd|e@@ @end|ng |rom gm@||@com
Mon Mar 16 22:14:51 CET 2020


Sorry, internet's not working properly today.
Third time lucky....

Here's a solution to your original question:

---------
freq <- c (1,2,5,5,10,4,4,8,1,1,8,8,2,4,3,1,2,1,1,138,149,14,1,1)

unique.consecutive <- function (x)
{   dx <- diff (x)
    x [dx != 0]
}

which.maxs <- function (x, ..., include.endpoints=FALSE)
{   dx <- diff (x)
    if (any (dx == 0) )
    stop ("function needs unique-consecutive values")
    ndx <- length (dx)
    I <- c (FALSE, dx [-ndx] > 0 & dx [-1] < 0, FALSE)
    if (include.endpoints)
    {   I [1] <- (dx [1] < 0)
        I [ndx + 1] <- (dx [ndx] > 0)
    }
    which (I)
}

freq.sub <- unique.consecutive (freq)
maxv <- freq.sub [which.maxs (freq.sub, include.endpoints=TRUE)]

maxv
unique (maxv)
---------

Some comments:

My package, probhat, contains early prototype-quality functions for
discrete kernel smoothing.
This can be used to "smooth" frequency data.
Which in turn, can eliminate spurious modes.

https://cran.r-project.org/web/packages/probhat/vignettes/probhat.pdf

Unfortunately, bandwidth selection is manual.
Also note that currently it only returns probability mass (not frequency)
but it's very easy to to get frequency from probability mass.

I'm planning to resume work on this package in two to three days, so I'm
open to suggestions...

	[[alternative HTML version deleted]]



More information about the R-help mailing list