The Chi-Square distribution is a continuous probability distribution commonly used in statistical inference, particularly in hypothesis testing and confidence interval estimation. It is defined by the degrees of freedom parameter \(k > 0\).
Usage
Chisq(df = 1)
# S4 method for class 'Chisq,numeric'
d(distr, x, log = FALSE)
# S4 method for class 'Chisq,numeric'
p(distr, q, lower.tail = TRUE, log.p = FALSE)
# S4 method for class 'Chisq,numeric'
qn(distr, p, lower.tail = TRUE, log.p = FALSE)
# S4 method for class 'Chisq,numeric'
r(distr, n)
# S4 method for class 'Chisq'
mean(x)
# S4 method for class 'Chisq'
median(x)
# S4 method for class 'Chisq'
mode(x)
# S4 method for class 'Chisq'
var(x)
# S4 method for class 'Chisq'
sd(x)
# S4 method for class 'Chisq'
skew(x)
# S4 method for class 'Chisq'
kurt(x)
# S4 method for class 'Chisq'
entro(x)
# S4 method for class 'Chisq'
finf(x)
llchisq(x, df)
# S4 method for class 'Chisq,numeric'
ll(distr, x)
echisq(x, type = "mle", ...)
# S4 method for class 'Chisq,numeric'
mle(distr, x, na.rm = FALSE)
# S4 method for class 'Chisq,numeric'
me(distr, x, na.rm = FALSE)
vchisq(df, type = "mle")
# S4 method for class 'Chisq'
avar_mle(distr)
# S4 method for class 'Chisq'
avar_me(distr)
Arguments
- df
numeric. The distribution degrees of freedom parameter.
- distr
an object of class
Chisq
.- x
For the density function,
x
is a numeric vector of quantiles. For the moments functions,x
is an object of classChisq
. For the log-likelihood and the estimation functions,x
is the sample of observations.- log, log.p
logical. Should the logarithm of the probability be returned?
- q
numeric. Vector of quantiles.
- lower.tail
logical. If TRUE (default), probabilities are \(P(X \leq x)\), otherwise \(P(X > x)\).
- p
numeric. Vector of probabilities.
- n
number of observations. If
length(n) > 1
, the length is taken to be the number required.- type
character, case ignored. The estimator type (mle or me).
- ...
extra arguments.
- na.rm
logical. Should the
NA
values be removed?
Value
Each type of function returns a different type of object:
Distribution Functions: When supplied with one argument (
distr
), thed()
,p()
,q()
,r()
,ll()
functions return the density, cumulative probability, quantile, random sample generator, and log-likelihood functions, respectively. When supplied with both arguments (distr
andx
), they evaluate the aforementioned functions directly.Moments: Returns a numeric, either vector or matrix depending on the moment and the distribution. The
moments()
function returns a list with all the available methods.Estimation: Returns a list, the estimators of the unknown parameters. Note that in distribution families like the binomial, multinomial, and negative binomial, the size is not returned, since it is considered known.
Variance: Returns a named matrix. The asymptotic covariance matrix of the estimator.
Details
The probability density function (PDF) of the Chi-Square distribution is given by: $$ f(x; k) = \frac{1}{2^{k/2}\Gamma(k/2)} x^{k/2 - 1} e^{-x/2}, \quad x > 0.$$
Examples
# -----------------------------------------------------
# Chi-Square Distribution Example
# -----------------------------------------------------
# Create the distribution
df <- 4
D <- Chisq(df)
# ------------------
# dpqr Functions
# ------------------
d(D, c(0.3, 2, 20)) # density function
#> [1] 0.0645530982 0.1839397206 0.0002269996
p(D, c(0.3, 2, 20)) # distribution function
#> [1] 0.01018583 0.26424112 0.99950060
qn(D, c(0.4, 0.8)) # inverse distribution function
#> [1] 2.752843 5.988617
x <- r(D, 100) # random generator function
# alternative way to use the function
den <- d(D) ; den(x) # den is a function itself
#> [1] 0.133953207 0.164372580 0.061167945 0.180660927 0.174835417 0.108700553
#> [7] 0.138664831 0.040800770 0.162150332 0.013945291 0.093442703 0.180306346
#> [13] 0.137114795 0.108305576 0.071478724 0.026122074 0.147013834 0.045141412
#> [19] 0.176801357 0.140182239 0.165428195 0.177971246 0.179869433 0.183423316
#> [25] 0.037316723 0.160314631 0.140127465 0.058395116 0.145650196 0.176000243
#> [31] 0.038087718 0.183549972 0.183296800 0.167474672 0.179512445 0.092969911
#> [37] 0.024571943 0.173870921 0.043376505 0.170032217 0.100929314 0.153545515
#> [43] 0.178074797 0.175667801 0.182831762 0.180404260 0.183897326 0.118782037
#> [49] 0.124520757 0.182993616 0.157269677 0.134646397 0.165900547 0.166826100
#> [55] 0.082072134 0.178706289 0.032388255 0.140453386 0.178009998 0.016499929
#> [61] 0.112873222 0.040779485 0.168101057 0.027434484 0.169054027 0.175343119
#> [67] 0.003212128 0.179366331 0.043308014 0.114698982 0.122145926 0.053932259
#> [73] 0.154485631 0.061410215 0.038025940 0.182701756 0.076429640 0.084788087
#> [79] 0.032987205 0.135332434 0.177135342 0.126736451 0.153414844 0.123765081
#> [85] 0.119646588 0.152191981 0.109532390 0.183845108 0.134598051 0.163049617
#> [91] 0.158686700 0.055062468 0.072454775 0.158352837 0.063773699 0.179371874
#> [97] 0.059828101 0.051063159 0.173934256 0.183076805
# ------------------
# Moments
# ------------------
mean(D) # Expectation
#> [1] 4
var(D) # Variance
#> [1] 8
sd(D) # Standard Deviation
#> [1] 2.828427
skew(D) # Skewness
#> [1] 1.414214
kurt(D) # Excess Kurtosis
#> [1] 3
entro(D) # Entropy
#> [1] 2.270363
finf(D) # Fisher Information Matrix
#> df
#> 0.1612335
# List of all available moments
mom <- moments(D)
#> Warning: The median of the Chi-Squared Distribution is not
#> available in closed-form. An approximation is provided.
mom$mean # expectation
#> [1] 4
# ------------------
# Point Estimation
# ------------------
ll(D, x)
#> [1] -226.357
llchisq(x, df)
#> [1] -226.357
echisq(x, type = "mle")
#> $df
#> [1] 3.577199
#>
echisq(x, type = "me")
#> $df
#> [1] 3.693955
#>
mle(D, x)
#> $df
#> [1] 3.577199
#>
me(D, x)
#> $df
#> [1] 3.693955
#>
e(D, x, type = "mle")
#> $df
#> [1] 3.577199
#>
mle("chisq", x) # the distr argument can be a character
#> $df
#> [1] 3.577199
#>
# ------------------
# Estimator Variance
# ------------------
vchisq(df, type = "mle")
#> df
#> 6.202184
vchisq(df, type = "me")
#> df
#> 8
avar_mle(D)
#> df
#> 6.202184
avar_me(D)
#> df
#> 8
v(D, type = "mle")
#> df
#> 6.202184