qgeneric: Generic function to find quantiles of a distribution

Description Usage Details Note Author(s) Examples

View source: R/qgeneric.R

Description

Computes quantiles.

Usage

1
qgeneric(pdist, p, ...)

Details

For internal use

Note

Taken from msm R-package.

Author(s)

Christopher Jackson

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
## The function is currently defined as
function (pdist, p, ...) 
{
    args <- list(...)
    if (is.null(args$log.p)) 
        args$log.p <- FALSE
    if (is.null(args$lower.tail)) 
        args$lower.tail <- TRUE
    if (is.null(args$lbound)) 
        args$lbound <- -Inf
    if (is.null(args$ubound)) 
        args$ubound <- Inf
    if (args$log.p) 
        p <- exp(p)
    if (!args$lower.tail) 
        p <- 1 - p
    ret <- numeric(length(p))
    ret[p == 0] <- args$lbound
    ret[p == 1] <- args$ubound
    args[c("lower.tail", "log.p", "lbound", "ubound")] <- NULL
    maxlen <- max(sapply(c(args, p = list(p)), length))
    for (i in seq(along = args)) args[[i]] <- rep(args[[i]], 
        length.out = maxlen)
    p <- rep(p, length.out = maxlen)
    ret[p < 0 | p > 1] <- NaN
    ind <- (p > 0 & p < 1)
    if (any(ind)) {
        hind <- seq(along = p)[ind]
        h <- function(y) {
            args <- lapply(args, function(x) x[hind[i]])
            p <- p[hind[i]]
            args$q <- y
            (do.call(pdist, args) - p)
        }
        ptmp <- numeric(length(p[ind]))
        for (i in 1:length(p[ind])) {
            interval <- c(-1, 1)
            while (h(interval[1]) * h(interval[2]) >= 0) {
                interval <- interval + c(-1, 1) * 0.5 * (interval[2] - 
                  interval[1])
            }
            ptmp[i] <- uniroot(h, interval, tol = .Machine$double.eps)$root
        }
        ret[ind] <- ptmp
    }
    if (any(is.nan(ret))) 
        warning("NaNs produced")
    ret
  }

BNPdensity documentation built on May 29, 2017, 9:33 p.m.