gIndependenceTest

Performs a G-test (log-likelihood ratio test) of independence for a contingency table.

The null hypothesis is that the row and column variables are independent — that is, knowing the row category does not help predict the column category. This test uses a log-likelihood ratio statistic as an alternative to the chi-squared test of independence.

Example:

// 2x2 contingency table: treatment vs outcome
val table = arrayOf(
intArrayOf(10, 30),
intArrayOf(20, 40)
)
val result = gIndependenceTest(table)
result.statistic // G statistic
result.pValue // p-value
result.degreesOfFreedom // 1.0 ((2-1) * (2-1))

Return

a TestResult containing the G statistic, p-value, and degrees of freedom ((rows - 1) * (columns - 1)).

Parameters

contingencyTable

a matrix of observed frequency counts with at least 2 rows and 2 columns. All rows must have the same number of columns.