dgamma.R: Gamma Density Function Alternatives

View source: R/dgamma.R

dgamma.RR Documentation

Gamma Density Function Alternatives

Description

dgamma.R() is aimed to be an R level “clone” of R's C level implementation dgamma (from package stats).

Usage

dgamma.R(x, shape, scale = 1, log,
         dpois_r_args = list())

Arguments

x

non-negative numeric vector.

shape

non-negative shape parameter of the Gamma distribution.

scale

positive scale parameter; note we do not see the need to have a rate parameter as the standard R function.

log

logical indicating if the result is desired on the log scale.

dpois_r_args

a list of optional arguments for dpois_raw(); not much checked, must be specified correctly.

Value

numeric vector of the same length as x (which may have to be thought of recycled along shape and/or scale.

Author(s)

Martin Maechler

See Also

(As R's C code) this depends crucially on the “workhorse” function dpois_raw().

Examples


xy  <- curve(dgamma  (x, 12), 0,30) # R's dgamma()
xyR <- curve(dgamma.R(x, 12, dpois_r_args = list(verbose=TRUE)), add=TRUE,
             col = adjustcolor(2, 1/3), lwd=3)
stopifnot(all.equal(xy, xyR, tolerance = 4e-15)) # seen 7.12e-16
## TODO: check *vectorization* in x --> add tests/*.R				___ TODO ___


## From R's  <R>/tests/d-p-q-r-tst-2.R -- replacing dgamma() w/ dgamma.R()
## PR#17577 - dgamma(x, shape)  for shape < 1 (=> +Inf at x=0) and very small x
stopifnot(exprs = {
    all.equal(dgamma.R(2^-1027, shape = .99 , log=TRUE), 7.1127667376, tol=1e-10)
    all.equal(dgamma.R(2^-1031, shape = 1e-2, log=TRUE), 702.8889158,  tol=1e-10)
    all.equal(dgamma.R(2^-1048, shape = 1e-7, log=TRUE), 710.30007699, tol=1e-10)
    all.equal(dgamma.R(2^-1048, shape = 1e-7, scale = 1e-315, log=TRUE),
              709.96858768, tol=1e-10)
})
## R's dgamma() gave all Inf in R <= 3.6.1 [and still there in 32-bit Windows !]

DPQ documentation built on Sept. 11, 2024, 8:37 p.m.