考虑到R中的分位数,求解qcauchy()中的x和y

时间:2017-04-24 16:53:14

标签: r function optimization distribution nlm

背景

qcauchy(p, location, scale)是一个内置的基本R函数。在此功能中,“位置”表示中心,“比例”表示 speadoutness 对称钟形曲线(就像正态分布一样)。 “location”可以是任意数字(负数,正数,非整数等)。 “scale”可以是大于“0”的任何数字。此外,“p”是概率,因此0 <= p <= 1。

编码问题:

仅作为一个示例,假设我知道qcauchy(p = c(.025, .975), location = x, scale = y ) = c(-12.7062, 12.7062 ),那么,有没有办法可以找到 x y 可以合理地(即在某个误差范围内)?

P.S。:作为一个小小的可能开始,nlm()(即非线性最小化)可以帮助吗?或者最右侧[即c(-12.7062, 12.7062 )]的事实是相同的数字,但符号相反。

2 个答案:

答案 0 :(得分:1)

I used a package for solving a system of nonlinear equations nleqslv. I tried the following

library(nleqslv)

f <- function(x) {
    y <- c(-12.7062, 12.7062) - qcauchy(c(.025,.975), location=x[1],  scale=x[2]) 
    y
}

nleqslv(c(1,1), f)

and got this answer

$x
[1] 5.773160e-15 9.999996e-01

$fvec
[1]  1.421085e-14 -1.421085e-14

$termcd
[1] 1

$message
[1] "Function criterion near zero"

$scalex
[1] 1 1

$nfcnt
[1] 1

$njcnt
[1] 1

$iter
[1] 1

答案 1 :(得分:0)

In addition to Bhas answer, you can first use your intuition and recognise that location must be zero, since the distribution is symmetric and - as you pointed out - the two values are the same up to their sign. So in this case the distribution is symmetric around zero.

To find the scale, use Bhas answer or

find_scale_template <- function(q)
  function(y) {
    (qcauchy(p = .975, location = 0, scale = y) - q)^2
  }
}
find_scale <- find_scale_template(12.7062)
optimize(find_scale, interval = c(0, 10))