invgamma implements the
(d/p/q/r) statistics functions for the inverse gamma distribution in R. It is ideal for using in other packages since it is lightweight and leverages the
(d/p/q/r)gamma() line of functions maintained by CRAN.
There are two ways to get invgamma. For the CRAN version, use
For the development version, use
# install.packages("devtools") devtools::install_github("dkahle/invgamma")
The functions in invgamma match those for the gamma distribution provided by the stats package. Namely, it uses as its density f(x) = (b^a / Gamma(a)) x^-(a+1) e^(-b/x), where a =
shape and b =
The PDF (the f(x) above) can be evaluated with the
library(invgamma) library(ggplot2); theme_set(theme_bw()) x <- seq(0, 5, .01) qplot(x, dinvgamma(x, 7, 10), geom = "line") # Warning: Removed 1 rows containing missing values (geom_path).
The CDF can be evaluated with the
f <- function(x) dinvgamma(x, 7, 10) q <- 2 integrate(f, 0, q) # 0.7621835 with absolute error < 7.3e-05 (p <- pinvgamma(q, 7, 10)) #  0.7621835
The quantile function can be evaluated with
qinvgamma(p, 7, 10) # = q #  2
And random number generation can be performed with
set.seed(1) rinvgamma(5, 7, 10) #  1.9996157 0.9678268 0.9853343 1.3157697 3.1578177
rinvgamma() can be used to obtain a Monte Carlo estimate of the probability given by
samples <- rinvgamma(1e5, 7, 10) mean(samples <= q) #  0.7621
Moreover, we can check the consistency and correctness of the implementation with
qplot(samples, geom = "density") + stat_function(fun = f, color = "red")
The gamma distribution subsumes the chi-squared and exponential distributions, so it makes sense to include the
*invexp() functions in invgamma. Their implementations, however, wrap
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.