Skip to content

Instantly share code, notes, and snippets.

@aammd
Created August 19, 2013 23:08
Show Gist options
  • Save aammd/6275260 to your computer and use it in GitHub Desktop.
Save aammd/6275260 to your computer and use it in GitHub Desktop.
a friend and I worked out a simple means to detect local maxima of a mixed frequency distribution -- useful for determining if populations have diverged or not!
x1 <- rnorm(100,5,sd=2)
x2 <- rnorm(200,15,3)
x3 <- c(x1,x2)

## calculate the density function 
den_x <- density(x3)]
## this can be plotted:
plot(den_x)
## the density object provides useful information, such as the bandwidth used for smoothing:
str(den_x)

Now, how can we find the local minima? by using diff to calculate something like the second derivative of the density curve (HT answerer on SO):

which(diff(sign(diff(den_x$y)))==-2)+1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment