
Density, distribution function, quantile function and random
generation for the chi-squared (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)
vector of quantiles.
vector of probabilities.
number of observations. If length(n) > 1
, the length
is taken to be the number required.
degrees of freedom (non-negative, but can be non-integer).
non-centrality parameter (non-negative).
logical; if TRUE, probabilities p are given as log(p).
logical; if TRUE (default), probabilities are
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.
The chi-squared distribution with df
The non-central chi-squared distribution with df
ncp
Note that the degrees of freedom df
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
Note that ncp
values larger than about 1e5 may give inaccurate
results with many warnings for pchisq
and qchisq
.
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 chi-squared distribution with shape
scale
dgamma
for the Gamma distribution.
# NOT RUN {
require(graphics)
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 with ncp > 0: Z0 has point mass at 0!
Z0 <- rchisq(100, df = 0, ncp = 2.)
graphics::stem(Z0)
# }
# NOT RUN {
## 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)
# }
# NOT RUN {
## "analytical" test
lam <- seq(0, 100, by = .25)
p00 <- pchisq(0, df = 0, ncp = lam)
p.0 <- pchisq(1e-300, df = 0, ncp = lam)
stopifnot(all.equal(p00, exp(-lam/2)),
all.equal(p.0, exp(-lam/2)))
# }
Run the code above in your browser using DataLab