local every minima in a kernel density function with R -
i hope can me. created kernel density function. multimodal curve. curve has several maxima , minima. want r shows me every minima (not maxima) (see enter image description herethe yellow points in attached chart).
i used follwing code:
all.modes <- function(x, h="nrd0") { den <- density(x, bw=h, kernel=c("gaussian")) den.s <- smooth.spline(den$x, den$y, all.knots=true, spar=0.8) s.0 <- predict(den.s, den.s$x, deriv=0) s.1 <- predict(den.s, den.s$x, deriv=1) s.derv <- data.frame(s0=s.0$y, s1=s.1$y) nmodes <- length(rle(den.sign <- sign(s.derv$s1))$values)/2 lengths <- rle(den.sign <- sign(s.derv$s1))$length if (is.na(nmodes) == true) { nmodes <- 0 } cum <- cumsum(lengths) x <- den$x[cum] y <- den$y[cum] return(list(x=x, y=y)) }
but code shows every modes.
thank help.
Comments
Post a Comment