Skip to content
Prev 152625 / 398500 Next

re cursive root finding

Hi,

Here is one way you can locate the peaks and troughs of a smoothed function
estimate (using the example data from smooth.spline() demo):

##-- example from smooth.spline()

y18 <- c(1:3,5,4,7:3,2*(2:5),rep(10,4)) 

xx <- seq(1,length(y18), len=201) 

s2 <- smooth.spline(y18) # GCV 

d1 <- predict(s2, xx, der=1)

# We plot the smooth and its first derivative

par(mfrow=c(2,1))

plot(y18, main=deparse(s2$call), col.main=2) 

lines(predict(s2, xx), col = 2) 

plot(d1$x, d1$y, type="l")

abline(h=0) # We can visually pick intervals where first derivative is zero

# Using uniroot() to locate the zeros of derivative

deriv.x <- function(x, sobj, deriv) predict(sobj, x=x, deriv=deriv)$y 

uniroot(deriv.x, interval=c(5,8), sobj=s2, deriv=1)$root

uniroot(deriv.x, interval=c(8,11), sobj=s2, deriv=1)$root

uniroot(deriv.x, interval=c(15,18), sobj=s2, deriv=1)$root


Hope this helps,
Ravi.

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of baptiste auguie
Sent: Friday, August 08, 2008 1:25 PM
To: Hans W. Borchers
Cc: r-help at r-project.org
Subject: Re: [R] re cursive root finding
On 8 Aug 2008, at 16:44, Hans W. Borchers wrote:

            
I probably agree with this, although the process of using a spline leaves me
with a "real" function.
I guess my question is really how to find those indices.  
predict.smooth.spline can give me the derivative for any x values I want, so
I don't need to compute the gradient numerically. However, I still don't
know how to locate the zeros automatically. How did you find these values?

smooth <- smooth.spline(values$x, values$y)

predict(smooth, der=1) -> test

which(test$y == 0 ) #gives (obviously) no answer, while

which(test$y < 1e-5) # gives many ...
Thanks,

baptiste
+ neighbors]))  # uniroot could be used
_____________________________

Baptiste Augui?

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag

______________________________________________
R-help at r-project.org 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.