Finding local maxima on a loess surface
On May 10, 2012, at 12:10 AM, Diego Rojas wrote:
---------- Forwarded message ----------
From: Diego Rojas <dirojasb at gmail.com>
Date: Wed, May 9, 2012 at 3:05 PM
Subject: Re: [R] Finding local maxima on a loess surface
To: David Winsemius <dwinsemius at comcast.net>
Thanks again, would you please try to ilustrate further your point
with
this example code. How would you find the coordinates for the maxima
on the
surface. Consider that in the surface I'm dealing with there are at
least
35 maxima.
require(MASS)
topo.lo <- loess(z ~ x * y, topo, degree = 1, span = 0.25,
normalize = FALSE)
topo.mar <- list(x = seq(0, 6.5, 0.1), y = seq(0, 6.5, 0.1))
new.dat <- expand.grid(topo.mar)
topo.pred <- predict(topo.lo, new.dat)
## draw the contour map based on loess predictions
library(rgl)
persp3d(topo.mar$x, topo.mar$y, topo.pred, shade=0.5, col="blue")
hasmax <- function(mtx, x, y) if( (mtx[x,y] > mtx[x,y-1]) &
(mtx[x,y] > mtx[x,y+1]) &
(mtx[x,y] > mtx[x-1,y]) &
(mtx[x,y] > mtx[x+1,y]) ) {return(TRUE ) } else {return(FALSE)}
for(x in 3:(dim(topo.pred)[1] -4)) {
for(y in 3:(dim(topo.pred)[2]-4) ) {
if( hasmax(topo.pred, x , y) ){print(c(x,y))} }}
#
[1] 40 7
Note: that topo.pred has a border of two and three row/columns of NA's
that made this very annoying to debug. A proper function would
probably need to pre-qualify the index ranges.
I tried a sign change approach but generalizing to 2d created
conceptual difficulties I could not resolve, so I just checked in both
directions for the local point being greater than its neighbors. You
obviously could do something other than printing coordinates at a
maximum
David. > > Thanks fot your help > > On Sat, May 5, 2012 at 9:34 AM, David Winsemius <dwinsemius at comcast.net > >wrote: > >> >> On May 4, 2012, at 3:00 PM, Diego Rojas wrote: >> >> Thanks, I know about it but i wat to find several local maxima, so in >>> other words I need a way to identify the places in the surface >>> where both >>> slopes are equal to 0 and the second derivative is negative. >>> >> >> There is no way that I know that will produce a mathematical >> function that >> would support symbolic manipulations of that sort for the results >> obtainable from a loess-object. I was expecting that you would be >> approaching this numerically and doing evaluations on a grid. >> Testing for >> equality to 0 is not a good practice if following that route. Sign >> reversal >> would be a more sensible criterion. ( And you _would_ be using >> predict.loess(). ) >> >> Still no data example or code offered, so not pursuing further >> efforts at >> illustration. >> >> >> >>> On Fri, May 4, 2012 at 9:28 AM, David Winsemius <dwinsemius at comcast.net >>> > >>> wrote: >>> >>> On May 3, 2012, at 6:09 PM, Diego Rojas wrote: >>> >>> If a run a LOESS model and then produce a smoothed surface: Is >>> there any >>> way to determine the coordinates of the local maxima on the surface? >>> >>> ?predict # it has a loess method. > David Winsemius, MD West Hartford, CT