title: Incremental Area Under the Curve (iAUC) in R
body: <p>The area under the curve can be calculated using the <em>trapz</em> function of the <em>pracma</em> package in R. iAUC is much more accurate in many instances, particularly in biology. However there is no, to me known, R function to calculate this. The trap function follows:</p>
  
  <p>Example:</p>
  
  <pre><code>a &lt;- c(1, 4, 5, 6)
  b &lt;- c(2, 4, 6, 8)
  plot(a, b)
  trapz(a, b)
  </code></pre>
  
  <p>Answer: 21</p>
  
  <p>Does anyone know how to use R to calculate the iAUC? A guide can be found <a href="https://www.google.se/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=4&amp;ved=0CD0QFjADahUKEwjzoIvEi6LHAhVI_nIKHSO1CgQ&amp;url=http%3A%2F%2Fpeople.bath.ac.uk%2Fjb335%2FY2%2520Research%2520Skills%2520(FH200107)_files%2FCalculation%2520of%2520Incremental%2520Area%2520Under%2520Curve.doc&amp;ei=t3jKVbPpMcj8ywOj6qog&amp;usg=AFQjCNHWIkrXKoQaGO6VNpjyLLim2HDfWA&amp;bvm=bv.99804247,d.bGQ" rel="nofollow">here</a>.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>Update</strong>
  <strong>In response to Cheng-Yow's answer below:</strong>
  Another example</p>
  
  <pre><code># Recall that trapz-function is available in the pracma package
  # Here is the function kindly provided by Cheng-Yow Timothy Lin
  library(pracma) # should be loaded already
  traps &lt;- function(a, b) {
       for (i in 1:length(b)-1){
            iAUC[i] &lt;-((b[i]+b[i+1])/2)*(a[i+1]-a[i])
       }
       return(sum(iAUC))
  }
  
  # Data example
  a &lt;- c(-15,  -5,   1,   2,   3,   5,   8,  10,  15,  20,  30,  45,  60,  90, 120)
  b &lt;- c(50.20604,  49.59338,  47.39944,  56.38831,  69.43493,  73.92512,  61.92072,  67.92632, 115.45669,
  195.03242, 322.15894, 291.30094, 289.20284, 238.70562, 156.23798)
  
  traps(a, b)
  
  trapz(a, b)
  </code></pre>
  
  <p>They yield the same results- is this the incremental area under the curve? It is not how they have explained the trapz-function...</p>
  
  <p>Thanks for any enlightenment!</p>