r - R中的Uniroot解决方案

标签 r

我想找到以下函数的根:

       x=0.5
       f <- function(y) ((1-pbeta(1-exp(-0.002926543
        *( 107.2592+y)^1.082618 *exp(0.04097536*(107.2592+y))),shape1=0.2640229,shape2=0.1595841)) -
(1-pbeta(1-exp(-0.002926543*(x)^1.082618 *exp(0.04097536*(x))),shape1=0.2640229,shape2=0.1595841))^2)

sroot=uniroot(f, lower=0, upper=1000)$root

Error in uniroot(f, lower = 0, upper = 1000) : f() values at end points not of opposite sign



我该如何解决错误?

最佳答案

uniroot()及其使用注意事项

uniroot 正在实现粗略的bisection method。这种方法比(quasi) Newton's method简单得多,但需要更强的假设条件以确保存在根:f(lower) * f(upper) < 0

这可能是很痛苦的,因为这种假设是充分的条件,但不是必要的条件。实际上,如果使用f(lower) * f(upper) > 0,仍然有可能存在一个根,但是由于不是100%确定,所以二等分法不能冒险。

考虑以下示例:

# a quadratic polynomial with root: -2 and 2
f <- function (x) x ^ 2 - 4

显然,[-5, 5]有根。但
uniroot(f, lower = -5, upper = 5)
#Error in uniroot(f, lower = -5, upper = 5) : 
#  f() values at end points not of opposite sign

实际上,二等分法的使用需要对f进行观察/检查,以便可以提出一个合理的区间,即根所在的位置。在R中,我们可以使用curve():
curve(f, from = -5, to = 5); abline(h = 0, lty = 3)

enter image description here

从图中可以看出,根存在于[-5, 0][0, 5]中。所以这些工作正常:
uniroot(f, lower = -5, upper = 0)
uniroot(f, lower = 0, upper = 5)

您的问题

现在,让我们尝试一下您的函数(为了便于阅读,我将其分为几行;通过这种方式也很容易检查正确性):
f <- function(y) {
  g <- function (u) 1 - exp(-0.002926543 * u^1.082618 * exp(0.04097536 * u))
  a <- 1 - pbeta(g(107.2592+y), 0.2640229, 0.1595841)
  b <- 1 - pbeta(g(x), 0.2640229, 0.1595841)
  a - b^2
  }

x <- 0.5
curve(f, from = 0, to = 1000)

enter image description here

这个函数怎么可能是一条水平线?它不能有根!
  • 检查上面的f,它确实在做您想要的正确操作吗?我怀疑g出了点问题;您可能将括号放在错误的位置?
  • 一旦正确设置了f,请使用curve检查存在根的适当间隔。然后使用uniroot
  • 关于r - R中的Uniroot解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38961221/

    相关文章:

    r - 在数据框列表上使用 rbind.fill 时忽略丢失的数据框

    r - 最初正确的日期作为 R 中的因素

    r - 如何在 for 中使用 arrangement?

    r - 如何在 Shiny 应用程序结束时终止所有进程?

    r - 通过 setdiff/intersect 删除/子集 R 中的行

    r - 如何从大量数据中搜索和检索特定列

    r - 如何将绘图区域裁剪到 R 中数据的确切范围?

    r - dplyr 获取比例

    r - 组内插值

    湖沼学的 R 软件包