R中曲线下的增量面积(iAUC)

时间:2015-08-11 22:37:47

标签: r

曲线下面积可以使用R中 pracma 包的 trapz 函数计算。在许多情况下,尤其是在生物学中,iAUC更准确。然而,据我所知,R函数没有计算这个。陷阱功能如下:

示例:

a <- c(1, 4, 5, 6)
b <- c(2, 4, 6, 8)
plot(a, b)
trapz(a, b)

答案:21

有谁知道如何使用R来计算iAUC?可以找到指南here

提前致谢。

更新 回应Cheng-Yow的回答: 另一个例子

# 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 <- function(a, b) {
     for (i in 1:length(b)-1){
          iAUC[i] <-((b[i]+b[i+1])/2)*(a[i+1]-a[i])
     }
     return(sum(iAUC))
}

# Data example
a <- c(-15,  -5,   1,   2,   3,   5,   8,  10,  15,  20,  30,  45,  60,  90, 120)
b <- 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)

它们产生相同的结果 - 这是曲线下的增量区域吗?他们不是如何解释陷阱功能......

感谢任何启蒙!

1 个答案:

答案 0 :(得分:3)

    a <- c(1,2,3,4,5)
b <- c(1,2,-1,-2,2)
plot(a,b)
lines(a, b)
abline(b[1],0)
iAUC <- vector()

for (i in 1:(length(a)-1)){

  if((b[i+1]-b[1] >= 0) && (b[i]-b[1] >= 0))
  {iAUC[i] <-((b[i]-b[1]+b[i+1]-b[1])/2)*(a[i+1]-a[i])} 

  else if((b[i+1]-b[1] < 0) && (b[i]-b[1] >= 0))
  {iAUC[i] <-(b[i]-b[1])*((b[i]-b[1])/(b[i]-b[i+1])*(a[i+1]-a[i])/2)} 

  else if((b[i+1]-b[1] >= 0) && (b[i]-b[1] < 0))
  {iAUC[i] <-(b[i+1]-b[1])*((b[i+1]-b[1])/(b[i+1]-b[i])*(a[i+1]-a[i])/2)} 

  else if((b[i]-b[1] < 0) && (b[i+1]-b[1] < 0))
  {iAUC[i] <- 0}
}
sum(iAUC)
iAUC