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

Ravi Varadhan rvaradhan at jhmi.edu
Tue Jun 12 21:21:49 CEST 2007


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

Webpage:  http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html

 

----------------------------------------------------------------------------
--------


-----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.



More information about the R-help mailing list