先前工作后,集成函数返回舍入错误

时间:2019-05-30 19:14:07

标签: r integrate

使用积分从2000-> Inf积分对数正态密度函数时,返回错误。我以前使用的方程非常相似,没有问题。

我尝试禁用错误停止功能,并将rel.tol设置得较低。我对r很陌生,不熟悉,因此,如果不能期望这两个都没有做任何事情,我深表歉意。

> integrand = function(x) {(x-2000)*(1/x)*(1/(.99066*((2*pi)^.5)))*exp(-((log(x)-7.641)^2)/((2*(.99066)^2)))}
> integrate(integrand,lower=2000,upper=Inf)
1854.002 with absolute error < 0.018
#returns value fine

> integrand = function(x) {(x-2000)*(1/x)*(1/(1.6247*((2*pi)^.5)))*exp(-((log(x)-9.0167)^2)/((2*(1.6247)^2)))}
> integrate(integrand,lower=2000,upper=Inf)
Error in integrate(integrand, lower = 2000, upper = Inf) : 
  roundoff error is detected in the extrapolation table
#small change in the mu and sigma in the lognormal density function results in roundoff error

> integrate(integrand,lower=1293,upper=Inf)
29005.08 with absolute error < 2
#integrating on lower bound works fine, but having lower=1294 returns a roundoff error again
> integrate(integrand,lower=1294,upper=Inf)
Error in integrate(integrand, lower = 1294, upper = Inf) : 
  roundoff error is detected in the extrapolation table

我应该得到一个值,不是吗?我很难看到稍微改变这些值会导致该功能不再集成。

1 个答案:

答案 0 :(得分:0)

首先,我相信当您通过写下整个表达式来定义被积分数时会感到复杂,使用内置的dlnorm函数似乎更好。

g <- function(x, deduce, meanlog, sdlog){
  (x - deduce) * dlnorm(x, meanlog = meanlog, sdlog = sdlog)
}

curve(g(x, deduce = 2000, meanlog = 9.0167, sdlog = 1.6247), 
      from = 1294, to = 1e4)

enter image description here

对于集成问题,cubature失败时,软件包integrate通常会做得更好。以下所有内容均会产生结果,而不会出错。

library(cubature)

cubintegrate(g, lower = 1293, upper = Inf, method = "pcubature", 
             deduce = 2000, meanlog = 9.0167, sdlog = 1.6247)

cubintegrate(g, lower = 1294, upper = Inf, method = "pcubature", 
             deduce = 2000, meanlog = 9.0167, sdlog = 1.6247)

cubintegrate(g, lower = 2000, upper = Inf, method = "pcubature", 
             deduce = 2000, meanlog = 9.0167, sdlog = 1.6247)