Chisquare {stats}  R Documentation 
Density, distribution function, quantile function and random
generation for the chisquared (\chi^2
) distribution with
df
degrees of freedom and optional noncentrality parameter
ncp
.
dchisq(x, df, ncp = 0, log = FALSE)
pchisq(q, df, ncp = 0, lower.tail = TRUE, log.p = FALSE)
qchisq(p, df, ncp = 0, lower.tail = TRUE, log.p = FALSE)
rchisq(n, df, ncp = 0)
x , q 
vector of quantiles. 
p 
vector of probabilities. 
n 
number of observations. If 
df 
degrees of freedom (nonnegative, but can be noninteger). 
ncp 
noncentrality parameter (nonnegative). 
log , log.p 
logical; if TRUE, probabilities p are given as log(p). 
lower.tail 
logical; if TRUE (default), probabilities are

The chisquared distribution with df
= n \ge 0
degrees of freedom has density
f_n(x) = \frac{1}{{2}^{n/2} \Gamma (n/2)} {x}^{n/21} {e}^{x/2}
for x > 0
, where f_0(x) := \lim_{n \to 0} f_n(x) =
\delta_0(x)
, a point mass at zero, is not a density function proper, but
a “\delta
distribution”.
The mean and variance are n
and 2n
.
The noncentral chisquared distribution with df
= n
degrees of freedom and noncentrality parameter ncp
= \lambda
has density
f(x) = f_{n,\lambda}(x) = e^{\lambda / 2}
\sum_{r=0}^\infty \frac{(\lambda/2)^r}{r!}\, f_{n + 2r}(x)
for x \ge 0
. For integer n
, this is the distribution of
the sum of squares of n
normals each with variance one,
\lambda
being the sum of squares of the normal means; further,
E(X) = n + \lambda
, Var(X) = 2(n + 2*\lambda)
, and
E((X  E(X))^3) = 8(n + 3*\lambda)
.
Note that the degrees of freedom df
= n
, can be
noninteger, and also n = 0
which is relevant for
noncentrality \lambda > 0
,
see Johnson et al (1995, chapter 29).
In that (noncentral, zero df) case, the distribution is a mixture of a
point mass at x = 0
(of size pchisq(0, df=0, ncp=ncp)
) and
a continuous part, and dchisq()
is not a density with
respect to that mixture measure but rather the limit of the density
for df \to 0
.
Note that ncp
values larger than about 1e5 (and even smaller) may give inaccurate
results with many warnings for pchisq
and qchisq
.
dchisq
gives the density, pchisq
gives the distribution
function, qchisq
gives the quantile function, and rchisq
generates random deviates.
Invalid arguments will result in return value NaN
, with a warning.
The length of the result is determined by n
for
rchisq
, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than n
are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
Supplying ncp = 0
uses the algorithm for the noncentral
distribution, which is not the same algorithm used if ncp
is
omitted. This is to give consistent behaviour in extreme cases with
values of ncp
very near zero.
The code for nonzero ncp
is principally intended to be used
for moderate values of ncp
: it will not be highly accurate,
especially in the tails, for large values.
The central cases are computed via the gamma distribution.
The noncentral dchisq
and rchisq
are computed as a
Poisson mixture of central chisquares (Johnson et al, 1995, p.436).
The noncentral pchisq
is for ncp < 80
computed from
the Poisson mixture of central chisquares and for larger ncp
via a C translation of
Ding, C. G. (1992) Algorithm AS275: Computing the noncentral chisquared distribution function. Appl.Statist., 41 478–482.
which computes the lower tail only (so the upper tail suffers from cancellation and a warning will be given when this is likely to be significant).
The noncentral qchisq
is based on inversion of pchisq
.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) Continuous Univariate Distributions, chapters 18 (volume 1) and 29 (volume 2). Wiley, New York.
Distributions for other standard distributions.
A central chisquared distribution with n
degrees of freedom
is the same as a Gamma distribution with shape
\alpha =
n/2
and scale
\sigma = 2
. Hence, see
dgamma
for the Gamma distribution.
The central chisquared distribution with 2 d.f. is identical to the
exponential distribution with rate 1/2: \chi^2_2 = Exp(1/2)
, see
dexp
.
require(graphics)
dchisq(1, df = 1:3)
pchisq(1, df = 3)
pchisq(1, df = 3, ncp = 0:4) # includes the above
x < 1:10
## Chisquared(df = 2) is a special exponential distribution
all.equal(dchisq(x, df = 2), dexp(x, 1/2))
all.equal(pchisq(x, df = 2), pexp(x, 1/2))
## noncentral RNG  df = 0 with ncp > 0: Z0 has point mass at 0!
Z0 < rchisq(100, df = 0, ncp = 2.)
graphics::stem(Z0)
## visual testing
## do PP plots for 1000 points at various degrees of freedom
L < 1.2; n < 1000; pp < ppoints(n)
op < par(mfrow = c(3,3), mar = c(3,3,1,1)+.1, mgp = c(1.5,.6,0),
oma = c(0,0,3,0))
for(df in 2^(4*rnorm(9))) {
plot(pp, sort(pchisq(rr < rchisq(n, df = df, ncp = L), df = df, ncp = L)),
ylab = "pchisq(rchisq(.),.)", pch = ".")
mtext(paste("df = ", formatC(df, digits = 4)), line = 2, adj = 0.05)
abline(0, 1, col = 2)
}
mtext(expression("PP plots : Noncentral "*
chi^2 *"(n=1000, df=X, ncp= 1.2)"),
cex = 1.5, font = 2, outer = TRUE)
par(op)
## "analytical" test
lam < seq(0, 100, by = .25)
p00 < pchisq(0, df = 0, ncp = lam)
p.0 < pchisq(1e300, df = 0, ncp = lam)
stopifnot(all.equal(p00, exp(lam/2)),
all.equal(p.0, exp(lam/2)))