Skip to content

Instantly share code, notes, and snippets.

@BenMatheja
Created February 23, 2016 09:53
Show Gist options
  • Select an option

  • Save BenMatheja/7b16d9aa421adf7b858b to your computer and use it in GitHub Desktop.

Select an option

Save BenMatheja/7b16d9aa421adf7b858b to your computer and use it in GitHub Desktop.
estimatePeaks = function (dfOfClusters) {
# helper: source: https://github.com/kerschke/flacco/blob/master/R/feature_ela_distribution.R
number_of_peaks = function(x, smoothing.bandwidth = "SJ", modemass.threshold = 0.01, ...) {
intdens = function(a, b) {
mean(y[a:b]) * diff(d$x[c(a, b)])
}
d = density(x, bw = smoothing.bandwidth, ...)
y = d$y
n = length(y)
index = 2L : (n - 1L)
min.index = c(1L, which((y[index] < y[index - 1L]) & y[index] < y[index + 1L]), n + 1L)
modemass = vapply(1L : (length(min.index) - 1L), function(i)
intdens(min.index[i], min.index[i + 1L] - 1L), double(1))
sum(modemass > modemass.threshold)
}
## implementation - https://stat.ethz.ch/pipermail/r-help/2008-September/172460.html
#h = hist(as.matrix(dfOfClusters[-4]), freq = T, breaks = 100, plot= F)
h = hist(as.matrix(dfOfClusters[-4]), breaks = 100, plot= F)
noOfPeaks = number_of_peaks(h$counts, modemass.threshold = %%)
return(noOfPeaks)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment