如何在R中找到曲线下的面积?

时间:2018-09-22 20:36:04

标签: r

我有两列X1,X2。我想找到X1下的区域和X2下的区域。

X = c(1,2,3,4,5)
Y1 = c(2,3,4,2,3)
Y2 = c(6,6,6,6,6)

plot(X,Y1) #Need to get area under this curve i.e auc(Y1)
plot(X,Y2) #Need to get area under this curve i.e auc(Y2)

我需要这个,以便我需要通过取AUC(Y1)/ AUC(Y2)之比来比较两个区域,即Y1和Y2

我在每个点都需要一个交流区域。

1 个答案:

答案 0 :(得分:2)

假设auc(Y1)是指

中曲线下方的区域
plot(1:5,Y1,type = "l")

您可以只使用trapezoidal rule,它的步长为1可以这样计算:

auc <- function(y){
  n <- length(y)
  0.5*(y[1]+y[n]+2*sum(y[-c(1,n)]))
}

例如:

> auc(Y1)
[1] 11.5