Solving for x and y in qcauchy() given its quantiles in R

113 Views Asked by At

Background:

qcauchy(p, location, scale) is an built-in base R function. In this function, "location" indicates the center and "scale" indicates the speadoutness of a symmetric bell-like curve (just like a normal distribution). "location" can be any number (negative, positive, non-integer etc.). And "scale" can be any number larger than "0". Also, "p" is probability thus 0 <= p <= 1.

Coding Question:

Only as 1 example, suppose I know qcauchy(p = c(.025, .975), location = x, scale = y ) = c(-12.7062, 12.7062 ), THEN, is there a way I can find out what x and y could reasonably be (i.e., within some margin of error)?

P.S.: As a small possible start, can nlm() (i.e., non-linear minimazation) help here? Or the fact the most right-hand side [i.e., c(-12.7062, 12.7062 ) ], are the same number with opposite signs.

2

There are 2 best solutions below

5
Bhas On BEST ANSWER

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
0
Niels On

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))