knitr::opts_chunk$set( collapse = TRUE, comment = "# ", fig.path = "tools/README-", dpi = 250 )
chi implements the (d/p/q/r)
statistics functions for the chi distribution in R. It is ideal for using in other packages since it is lightweight and leverages the (d/p/q/r)chisq()
line of functions maintained by CRAN.
There are two ways to get chi. For the CRAN version, use
install.packages("chi")
For the development version, use
# install.packages("devtools") devtools::install_github("dkahle/chi")
(d/p/q/r)chi()
functionsThe defining property of the chi distribution is that it is the square root of a chi random variable.
The PDF (the f(x)) can be evaluated with the dchi()
function:
library(chi) library(ggplot2); theme_set(theme_bw()) x <- seq(0, 6, .01) qplot(x, dchi(x, 7), geom = "line")
The CDF can be evaluated with the pchi()
function:
f <- function(x) dchi(x, 7) q <- 2 integrate(f, 0, q) (p <- pchi(q, 7))
The quantile function can be evaluated with qchi()
:
qchi(p, 7) # = q
And random number generation can be performed with rchi()
:
set.seed(1) rchi(5, 7)
rchi()
can be used to obtain a Monte Carlo estimate of the probability given by pchi()
above:
samples <- rchi(1e5, 7) mean(samples <= q)
Moreover, we can check the consistency and correctness of the implementation with
qplot(samples, geom = "density") + stat_function(fun = f, color = "red")
The Runuran package also implements a sampler from the chi distribution, using the function Runuran::urchi()
. (It also provides udchi()
, but not upchi()
or uqchi()
.) Here's a comparison of how fast the samplers are, with the punch line being that Runuran is about 3x faster for larger numbers of samples, whereas chi is much faster for smaller samples (e.g. 100 or 1000).
library(Runuran) library(microbenchmark) # chi::rchi() is much faster for small datasets microbenchmark( urchi(1e3, 5), rchi(1e3, 5) ) # Runuran::urchi is ~3x faster for larger datasets microbenchmark( urchi(1e5, 5), rchi(1e5, 5) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.