Chisquare {base} | R Documentation |
Density, distribution function, quantile function and random
generation for the chi-squared (chi^2) distribution with
df
degrees of freedom and optional non-centrality 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 length(n) > 1 , the length
is taken to be the number required. |
df |
degrees of freedom (non-negative, but can be non-integer). |
ncp |
non-centrality parameter (non-negative). |
log, log.p |
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are P[X <= x], otherwise, P[X > x]. |
The chi-squared distribution with df
= n degrees of
freedom has density
f_n(x) = 1 / (2^(n/2) Gamma(n/2)) x^(n/2-1) e^(-x/2)
for x > 0. The mean and variance are n and 2n.
The non-central chi-squared distribution with df
= n
degrees of freedom and non-centrality parameter ncp
= λ has density
f(x) = exp(-lambda/2) SUM_{r=0}^infty ((lambda/2)^r / r!) dchisq(x, df + 2r)
for x >= 0. For integer n, this is the distribution of
the sum of squares of n normals each with variance one,
λ being the sum of squares of the normal means.
Note that the the degrees of freedom df
= n, can be
non-integer, and for non-centrality λ > 0, even n = 0;
see the reference, chapter 29.
Warning: The code for pchisq
and
qchisq
is unreliable for
values of ncp
above approximately 290.
dchisq
gives the density, pchisq
gives the distribution
function, qchisq
gives the quantile function, and rchisq
generates random deviates.
Johnson, Kotz and Balakrishnan (1995). Continuous Univariate Distributions, Vol 2; Wiley NY;
dgamma
for the Gamma distribution which generalizes the
chi-squared one.
dchisq(1, df=1:3) pchisq(1, df= 3) pchisq(1, df= 3, ncp = 0:4)# includes the above x <- 1:10 ## Chi-squared(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)) ## non-central RNG -- df=0 is ok for ncp > 0: Z0 has point mass at 0! Z0 <- rchisq(100, df = 0, ncp = 2.) stem(Z0) ## visual testing ## do P-P 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("P-P plots : Noncentral "* chi^2 *"(n=1000, df=X, ncp= 1.2)"), cex = 1.5, font = 2, outer=TRUE) par(op)