我正在尝试使用 Rintegrate
函数计算两个图之间的面积。我有两条预测曲线,我可以将它们放置在同一个图上,并在两条曲线之间的区域着色,以进行可视化:
x1 = seq(1,200,1)
y1 = # 200 numbers from 0.02 - 1.000
y2 = # 200 numbers from 0.00 - 0.95
plot(x1,y1,type="l",bty="L",xlab="Forecast Days",ylab="Curve Actuals")
points(x1,y2,type="l",col="red")
polygon(c(x1,rev(x1)),c(y2,rev(y1)),col="skyblue")
按照此处的示例https://stat.ethz.ch/pipermail/r-help/2010-September/251756.html我尝试为我的数据执行相同的代码来计算两条曲线之间的距离。如示例中所述,“两条曲线之间的面积与这两条曲线之间的差值的积分相同(分别是其绝对值)”:
f1 = approxfun(x1, y1-y2) # piecewise linear function
f2 = function(x) abs(f1(x)) # take the positive value
integrate(f2, 1, 200)
但是我收到以下错误:
Error in integrate(f2, 1, 200) : maximum number of subdivisions reached
赞赏任何可以阐明的清晰度。谢谢!