Chi Squared
TanayLabUtilities.ChiSquared
—
Module
Chi-squared test for 2x2 contingency tables derived from two count vectors.
TanayLabUtilities.ChiSquared.chi_squared
—
Function
chi_squared(
first_column::AbstractVector{<:Real},
second_column::AbstractVector{<:Real};
yates::Bool = true,
)::Matrix{Float64}
chi_squared(
matrix::AbstractMatrix{<:Real};
yates::Bool = true,
)::Matrix{Float64}
Compute chi-squared statistics and p-values for 2x2 contingency tables. The two input vectors (or the two columns of the input matrix) contain non-negative counts. Each entry defines one 2x2 table together with the column sums:
| First | Second | |
|---|---|---|
| Current entry |
a
|
c
|
| Other entries |
sum1 - a
|
sum2 - c
|
If
yates
is
true
(the default), Yates' continuity correction is applied. The inputs must not contain
NaN
values. The function works with both dense and sparse inputs.
Returns an
n x 3
matrix where the first column contains the chi-squared statistics, the second column contains the p-values (for chi-squared with 1 degree of freedom, computed as
erfc(sqrt(chi2 / 2))
), and the third column contains the FDR-corrected p-values (Benjamini-Hochberg procedure).
result = chi_squared([10, 30, 50], [20, 40, 60])
@assert size(result) == (3, 3)
@views @assert all(result[:, 1] .>= 0)
@views @assert all(0 .<= result[:, 2] .<= 1)
@views @assert all(0 .<= result[:, 3] .<= 1)
@views @assert all(result[:, 3] .>= result[:, 2] .- 1e-12)
# output
result_yates = chi_squared([10, 30], [20, 40]; yates = true)
result_no_yates = chi_squared([10, 30], [20, 40]; yates = false)
@views @assert all(result_yates[:, 1] .<= result_no_yates[:, 1] .+ 1e-12)
@views @assert all(result_yates[:, 2] .>= result_no_yates[:, 2] .- 1e-12)
@views @assert all(result_yates[:, 3] .>= result_yates[:, 2] .- 1e-12)
# output
result = chi_squared([10 20; 30 40; 50 60])
@assert size(result) == (3, 3)
# output