[R] how to find how many modes in 2 dimensions case
Ravi Varadhan
rvaradhan at jhmi.edu
Tue Jun 12 21:00:24 CEST 2007
Hi Patrick,
Here is a simple R code for locating ALL the local maximum of a bivariate
function, which is known on a rectangular grid. I have illustrated it with
a function called the Branin function, which is commonly used as a test
function in the global optimization literature. It has 6 local maxima, two
of which are global.
branin <- function(x1,x2,p) {
p[1] * x1^2 + p[2]*x1^4 + p[3]*x1^6 - x1*x2 + p[4]*x2^2 + p[5]*x2^4
}
x <- seq(-2, 2, length=100)
y <- seq(-1, 1, length=100)
p <- c(-4, 2.1, -1/3, 4, -4)
z <- outer(x, y, branin,p=p)
persp(x, y, z, theta=30, phi=30, col="lightblue")
# 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: -1.72 y: 0.798 function: 0.214
x: -1.60 y: -0.576 function: -2.10
x: -0.101 y: 0.717 function: 1.03
x: 0.101 y: -0.717 function: 1.03
x: 1.60 y: 0.576 function: -2.10
x: 1.72 y: -0.798 function: 0.214
Of course, this brute-force grid search is highly inefficient for dimensions
greater than 2.
Hope this is helpful,
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