[R] find inflexion point of discrete value list with R

David Winsemius dwinsemius at comcast.net
Mon Jan 2 22:09:17 CET 2012


On Jan 2, 2012, at 11:49 AM, Ben Bolker wrote:

> Jonas Stein <news <at> jonasstein.de> writes:
>
>>
>> i have a list of values like this
>>
>> x y
>> 1 3
>> 2 2
>
>  [snip]
>
>>
>> and need the inflexion [sic] points (and all max and min).
>> Is there a nice way to get the local max, min and inflexion points?
>
>  diff(y) gives you the first difference, the analogue of the gradient
>  diff(diff(y)) gives the second difference, the analogue of the second
> derivative.
>
>  dy <- diff(y)
>  d2y <- diff(dy)
>  which(dy==0)  ## critical values
>  sign(s2y)[which(dy==0)]  ## test for max/min/saddle
>  which(d2y==0)   ## inflection points

I would think that testing for d2y==0 would be akin to the error in  
numeric analysis warned about in FAQ 7.31. Seems unlikely that in real  
data that there would always be three points in a row with equal  
differences at a "true" inflection and even then, many of the ones you  
did find satisfying that criterion would not be in fact inflection  
points. Wouldn't it be better to fit a spline and then do your testing  
on the spline approximation?

Counter-example:
  x=1:10
 > y=c(1,2,3,5,7,10,13,16,20,24)
 >  dy <- diff(y)
 >  d2y <- diff(dy)
 > which(d2y==0)
[1] 1 3 5 6 8

And actually the original data was a pretty good counter-example as  
well.


-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list