# Re: [R] how to find how many modes in 2 dimensions case

From: Ravi Varadhan <rvaradhan_at_jhmi.edu>
Date: Tue, 12 Jun 2007 15:21:49 -0400

Patrick,

Here is an example closer to what you are interested in - locating bumps in kernel density estimator. I am using the example from package KernSmooth, using the function bkde2D().

# Another example for locating maxima in kernel density estimation  data(geyser, package="MASS")
x <- cbind(geyser\$duration, geyser\$waiting)  est <- bkde2D(x, bandwidth=c(0.7,7))
persp(est\$fhat)

```x <- est\$x1
y <- est\$x2
z <- est\$fhat

```

# here is a brute-force algorithm to locate ALL the local maxima

for (i in 2:(nrow(z)-1) ) {
for (j in 2:(ncol(z)-1) ) {
lmax <- (z[i,j] > z[i-1,j]) & (z[i,j] > z[i+1,j]) & (z[i,j] > z[i,j-1]) & (z[i,j] > z[i,j+1])
if(lmax) cat("x: ",x[i], "y: ", y[j], "function: ", z[i,j], "\n") }
}

```x:  0.724 y:  41.1 function:  2.71e-20
x:  0.858 y:  39.4 function:  1.08e-19
x:  0.992 y:  35.9 function:  2.17e-19
x:  2.07 y:  82.4 function:  0.00795
x:  4.08 y:  77.2 function:  0.00722
x:  4.35 y:  54.9 function:  0.00778

```

Of these, you can ignore the first 3, which have zero density.

Ravi.

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan_at_jhmi.edu

-----Original Message-----
From: r-help-bounces_at_stat.math.ethz.ch
[mailto:r-help-bounces_at_stat.math.ethz.ch] On Behalf Of Patrick Wang Sent: Friday, June 08, 2007 3:35 PM
To: Bert Gunter
Cc: r-help_at_stat.math.ethz.ch
Subject: Re: [R] how to find how many modes in 2 dimensions case

Thanks for the reply,

maybe I shall say bumps, I can use persp to show a density on a X Y dimensions.
one peak is one mode I think. I try to find an automatic way to detect how many peaks of the densities.

Pat

```> Note that "the number of modes" (local maxima??)  is a function of the
> bandwidth, so I'm not sure your question is even meaningful.
>
> Bert Gunter
> Genentech Nonclinical Statistics
> South San Francisco, CA 94404
> 650-467-7374
>
> -----Original Message-----
> From: r-help-bounces_at_stat.math.ethz.ch
> [mailto:r-help-bounces_at_stat.math.ethz.ch] On Behalf Of Patrick Wang
> Sent: Friday, June 08, 2007 11:54 AM
> To: R-help_at_stat.math.ethz.ch
> Subject: [R] how to find how many modes in 2 dimensions case
>
> Hi,
>
> Does anyone know how to count the number of modes in 2 dimensions using
> kde2d function?
>
> Thanks
> Pat
>
> ______________________________________________
> R-help_at_stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

______________________________________________
```
R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 12 Jun 2007 - 19:26:31 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 12 Jun 2007 - 19:32:02 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.