当使用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
我应该得到一个值,不是吗?我很难理解对值进行非常轻微的更改会如何导致函数不再集成。
发布于 2019-05-31 04:12:59
首先,我认为你在定义被积函数时会很复杂,因为你写下了整个表达式,使用内置的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)
至于集成问题,当integrate
失败时,package cubature
通常会做得更好。以下所有操作都会生成结果,并且没有错误。
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)
https://stackoverflow.com/questions/56384330
复制相似问题