How to find all the turning points on a kernel density curve when window width varies [closed]

空扰寡人 提交于 2021-01-29 03:58:21

问题


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)

回答1:


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")

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.



来源:https://stackoverflow.com/questions/42915904/how-to-find-all-the-turning-points-on-a-kernel-density-curve-when-window-width-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!