来自 integrate/uniroot 的奇怪错误

Strange error from integrate/uniroot

所以,我正在尝试计算隐式函数的积分 fx01

fx01<-function(y,z0)    pnorm(z0+y)-pnorm(z0-y)-0.5
fx11<-function(z0){
    uniroot(fx01,interval=c(0,10),z0=z0)$root
}
integrate(fx11,lower=1,upper=1.1)$value

我得到:

Error in uniroot(fx01, interval = c(0, 10), z0 = z0) : 
  f() values at end points not of opposite sign
In addition: Warning messages:
1: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") :
  the condition has length > 1 and only the first element will be used
2: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") :
  the condition has length > 1 and only the first element will be used

现在,我不明白是什么导致了这些错误: 函数 fx01 returns 所有的有界标量 z0 在 -infty +infty 中的值(这是标准正态分布),如果检查:

interval=c(0,10)
fx01(min(interval),z0=1)
fx01(max(interval),z0=1)

fx01(min(interval),z0=1.1)
fx01(max(interval),z0=1.1)

的相反符号。这些错误消息在这种情况下意味着什么?

您的 fx11 函数未正确矢量化。尝试

integrate(Vectorize(fx11),lower=1,upper=1.1)$value

问题是 integrate 会立即将一个值向量传递给您要集成的函数,它不会单独评估每个点。当 z0 的长度为 one

时,您的函数有效
uniroot(fx01,interval=c(0,10),z0=1)$root
# [1] 1.050555

但不是当它是值向量时

uniroot(fx01,interval=c(0,10),z0=c(1,1.05))$root
# Error in uniroot(fx01, interval = c(0, 10), z0 = c(1, 1.05)) : 
#   f() values at end points not of opposite sign
# In addition: Warning messages:
# 1: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") :
#   the condition has length > 1 and only the first element will be used
# 2: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") :
#   the condition has length > 1 and only the first element will be used

Vectorize() 函数将 运行 z0 的每个值分开。