1

I want to partition a data series using kernel density. Here is my plan:

  1. Using kernel density function (like density()) with variant window width to calculate the density of this series.
  2. On every kernel curve with different window width, I find all the turning points (including minimal and maximal) to partition the data.

So, I need to know where those turning points are in the original data series. I read some information like https://stats.stackexchange.com/questions/30750/finding-local-extrema-of-a-density-function-using-splines. But I do not really understand the method. In that method, d$x[tp$tppos] looks not the original index. So how can I find the positions of all the turning points in the original data based on kernel density curve?

Another related question is: how to find all the minimal/maximal points?

A sample of data series is:

a <- c(21.11606, 15.22204, 16.27281, 15.22204, 15.22204, 21.11606, 19.32840, 15.22204, 20.25594, 15.22204, 14.28352, 15.22195, 19.32840, 19.32840, 15.22204, 14.28352, 21.11606, 21.19069, 15.22204, 25.26564, 15.22204, 19.32840, 21.11606, 15.22204, 15.22204, 19.32840, 15.22204, 19.32840, 15.22204, 15.22204, 21.13656, 15.22204, 15.22204, 19.32840, 15.22204, 17.98954, 15.22204, 15.22204, 15.22204, 15.22204, 15.22204, 19.32840, 15.22204, 14.28352, 15.22204, 19.32840, 15.22204, 19.32840, 25.42281, 21.19069)
Community
  • 1
  • 1
Feng Chen
  • 2,139
  • 4
  • 33
  • 62
  • [This question](http://stackoverflow.com/q/6836409/5967807) has some excellent answers that might be useful. – nya Mar 21 '17 at 10:02

1 Answers1

2

When you take density of a:   Da = density(a)   the result has the y values associated with many x's. That is where the plot comes from. To find the "turning points", you need to find the places that the derivative changes sign. Since the x values given in Da$x are increasing, Each
Da$y[i] - Da$y[i-1] has the same sign as the derivative at the ith point. You can find where these change sign by finding where the product of consecutive values is negative. So, putting this all together, we get:

Da = density(a)
DeltaY = diff(Da$y)
Turns = which(DeltaY[-1] * DeltaY[-length(DeltaY)] < 0) + 1

plot(Da, xlab="", ylab="", main="")
points(Da$x[Turns], Da$y[Turns], pch=16, col="red")

Density Plot with Extrema

You can get different "window widths" using the adjust parameter to density. However, you are going to find that as you make adjust smaller, the density plot will develop many maxima and minima.

G5W
  • 36,531
  • 10
  • 47
  • 80
  • Thanks for you answer. But I still do not know how to find the turning points in the original data. For example, Turns = [126,198,257,370,408]. But a just has 50 points. So, how can I find the points related to the index in Turns? One more thing is: the index in Turns includes minimal and maximal. How can I just find minimal, or maximal? – Feng Chen Mar 21 '17 at 01:26
  • @FengChen Use `Da = density(a, n = length(a))` for `Turns` to reflect indices in `a`. – nya Mar 21 '17 at 09:59
  • Thanks a lot! This is very important to me! – Feng Chen Mar 21 '17 at 22:41