集成函数在先前工作后返回舍入错误
Integrate function returning roundoff error after working previously
当使用 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
我应该得到返回值,不是吗?我很难看到稍微改变这些值会导致函数不再集成。
首先,我相信当您通过写下整个表达式来定义被积函数时会变得复杂,使用 built-in 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)
至于集成问题,包 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)
当使用 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
我应该得到返回值,不是吗?我很难看到稍微改变这些值会导致函数不再集成。
首先,我相信当您通过写下整个表达式来定义被积函数时会变得复杂,使用 built-in 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)
至于集成问题,包 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)