来自集成/uniroot 的奇怪错误 [英] Strange error from integrate/uniroot

查看:84
本文介绍了来自集成/uniroot 的奇怪错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, I'm trying to compute the integral of the implicit function 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 为所有返回一个有界标量-infty +infty 中 z0 的值(这是标准正态分布),如果检查:

Now, I don't understand what is causing these errors: the function fx01 returns a bounded scalar for all the values of z0 in -infty +infty (it is the standard normal distribution) and if one checks:

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)

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

The values are of opposite sign. What do these error messages mean in this context?

推荐答案

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

Your fx11 function isn't properly vectorized. Try

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

问题在于 integrate 会立即将值向量传递给您尝试集成的函数,它不会单独评估每个点.当 z0 的长度为 1

The problem is that integrate will pass in a vector of values at once to the function you are trying to integrate, it does not evaluate each point separately. Your function works when z0 is of length 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 的每个值.

The Vectorize() function will run each value of z0 separately.

这篇关于来自集成/uniroot 的奇怪错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆