title: Calculate inflection points with R
body: <p>I am trying to do a "variance filtering using the inflexion point of ranked list of median absolute deviation values" to reproduce a paper.</p>
  
  <p>So far I obtained the median absolute values (MAD :) with <code>mad</code>:</p>
  
  <pre><code>mad.v &lt;- apply(x, 2, mad, na.rm = T)
  mad.v &lt;- mad[!is.na(mad.v)]
  </code></pre>
  
  <p>Then I create a function to calculate the percentage of those values</p>
  
  <pre><code>count.p &lt;- function(data, per){
    # Calculates the percentatge above some data
    sum(data &gt;= per)/length(data)
  }
  perc &lt;- unlist(lapply(mad.v, count.p, data = mad.v))
  </code></pre>
  
  <p>When I plot it I obtain something like:</p>
  
  <p><a href="https://i.stack.imgur.com/1Nwgb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1Nwgb.png" alt="percentatge over values"></a>
  Where the vertical blue line is the inflexion point of the blue tendency and the red one for the other. Which I don't really trust, as the red tendency has already changed of concavity before that point. </p>
  
  <p><strong>How can I obtain the inflection point?</strong></p>
  
  <p>I have seen the inflection package but I didn't know how to use <code>ede</code> properly in the image above that where supposed to be the inflection points. I also found the RootsExtremaInflections package but I neither knew how to use it properlly (I did: <code>inflexi(mad.v, perc, 3000, 13000, 2, 5, "x", "y", 3,3)</code>)</p>
  
  <p>Other things I tried are this other <a href="https://stats.stackexchange.com/q/76959/105234">question</a> and doing myself the <a href="https://en.wikipedia.org/wiki/Secant_method" rel="nofollow noreferrer">secant method</a> on the derivatives of the function (which I am unsure how to calculate them)</p>
  
  <pre><code>sampledata &lt;- matrix(rnbinom(5000, 2, 0.5), 10, 500)
  </code></pre>