1

I have a dataset with two columns x, y as follows

  x           y
  0.5789474   0.0011382324
  1.0000000   0.0024540588
  0.8000000   0.0017039382
  0.7272727   0.0014921618
  0.8421053   0.0018399977
  0.8611111   0.0019049152
  0.3750000   0.0007843210
  0.7837838   0.0016542579
  0.7222222   0.0014784711
  0.7619048   0.0015895130
  0.7435897   0.0015372644
  0.4375000   0.0008791528
  0.8750000   0.0019537960
  0.6666667   0.0013359048
  0.8750000   0.0019537960
  0.8571429   0.0018911749
  0.6896552   0.0013931524
  1.0000000   0.0024540588
  0.9285714   0.0021543502
  0.9523810   0.0022499579

Plotting these values produces a graph as follows

enter image description here

My goal is to find the values of x and y where the slope or rate of change in the curve is maximum. Where the bend or curve is sharpest. I tried ,

(lead(y)-y)/(lead(x)-x) and this is did not work. Any suggestions or advice is much appreciated. Thanks in advance.

bison2178
  • 747
  • 1
  • 8
  • 22
  • 1
    Please clarify your question. The maximum slope is between the two rightmost points. How did you derive the red and blue lines? What do you mean by "curve changes sharply"? Maximum curvature? Do you want this for the raw data or would you be more interested in the result after smoothing? – Roland Jul 16 '19 at 06:47
  • @Maximum Curvature, yes that is what I am looking for and now I going through the maxcurv function, https://rdrr.io/cran/soilphysics/man/maxcurv.html#heading-8 – bison2178 Jul 16 '19 at 15:45
  • For your data you could simply fit a cubic polynomial with `lm` and than solve this analytically. In the general case, you could use non-parametric methods as shown [in this answer](https://stackoverflow.com/a/14207597/1412059) (but be careful, it's a very simple approximation and you also need to find the right degree of smoothing). – Roland Jul 17 '19 at 07:31

2 Answers2

1

Good to see you, bison

In my opinion, Your code works well after sorting.

I don't know why did you say (lead(y)-y)/(lead(x)-x) is not working. See below code :

x <- sort(c(
  0.5789474,1.0000000,
  0.8000000,0.7272727,
  0.8421053,0.8611111,
  0.3750000,0.7837838,
  0.7222222,0.7619048,
  0.7435897,0.4375000,
  0.8750000,0.6666667,
  0.8750000,0.8571429,
  0.6896552,1.0000000,
  0.9285714,0.9523810
))
y <- sort(c(
  0.0011382324,0.0024540588,
  0.0017039382,0.0014921618,
  0.0018399977,0.0019049152,
  0.0007843210,0.0016542579,
  0.0014784711,0.0015895130,
  0.0015372644,0.0008791528,
  0.0019537960,0.0013359048,
  0.0019537960,0.0018911749,
  0.0013931524,0.0024540588,
  0.0021543502,0.0022499579
))

> (lead(y)-y)/(lead(x)-x)
 [1] 0.001517309 0.001831632 0.002253465 0.002490271 0.002619790
 [6] 0.002710761 0.002764148 0.002852761 0.002959226 0.003063622
[11] 0.003231410 0.003403282 0.003462603 0.003519415         NaN
[16] 0.003743680 0.004015511 0.004286123         NaN          NA

x[which.max((lead(y)-y)/(lead(x)-x))]
y[which.max((lead(y)-y)/(lead(x)-x))]

Steve Lee
  • 786
  • 3
  • 10
  • :-) good to see you too Steve , why would I sort x and y separately. Would I not lose the correct ordering of x,y ? – bison2178 Jul 16 '19 at 15:33
1

I am giving pointer:

df = read.table(text=" 
   0.5789474   0.0011382324
   1.0000000   0.0024540588
   0.8000000   0.0017039382
   0.7272727   0.0014921618
   0.8421053   0.0018399977
   0.8611111   0.0019049152
   0.3750000   0.0007843210
   0.7837838   0.0016542579
   0.7222222   0.0014784711
   0.7619048   0.0015895130
   0.7435897   0.0015372644
   0.4375000   0.0008791528
   0.8750000   0.0019537960
   0.6666667   0.0013359048
   0.8750000   0.0019537960
   0.8571429   0.0018911749
   0.6896552   0.0013931524
   1.0000000   0.0024540588
   0.9285714   0.0021543502
   0.9523810   0.0022499579",header=FALSE)

Now calculate slope:

slope= diff(df$V2)/diff(df$V1)

From the maximum slope retrieve the corresponding x,y points:

df[which.max(slope),]

Output:

    V1          V2
 18  1 0.002454059

Please feel free to correct me!

Saurabh Chauhan
  • 3,161
  • 2
  • 19
  • 46
  • you are not wrong but Ronald mentioned something that made me curious about some topics which I think is more relevant. – bison2178 Jul 16 '19 at 15:44