rStats.js v1.3.0
    Preparing search index...

    Function gTest

    • Log-likelihood ratio (G) test of independence in a contingency table, or of goodness of fit of counts to given probabilities. Equivalent to DescTools::GTest() in R.

      For a table (an array of rows), G = 2 sum x log(x / E) / q with E the products of the margins over the total and (r - 1)(c - 1) degrees of freedom, counting empty rows and columns as DescTools; for a vector (or a table with one row or column), E = n p and k - 1 degrees of freedom. correct: "williams" divides by Williams' q (1 + (n sum 1/R_i - 1)(n sum 1/C_j - 1) / (6 n (r - 1)(c - 1)) for a table, which is not defined with an empty row or column: an error, where DescTools gives G = 0 and p = 1; and 1 + (k + 1) / (6n) for a vector). correct: "yates" moves the counts of a 2 x 2 table by 0.5 toward independence as DescTools does: the off-diagonal cells up and the diagonal down when x11 x22 - x12 x21 > 0, and the other way otherwise, also when it is 0 (so that a table of exact independence gets a positive G), and without stopping at 0 (a cell that becomes negative makes G NaN, with the warning "NaNs produced"). For two counts it moves them by 0.5 toward E when they differ from it by more than 0.25.

      The p-value is the upper tail of chi-squared (DescTools computes 1 - pchisq for a table, which is 0 below about 1e-16). G is summed as a deviance, without the cancellation of the plain sum. The result also has the Freeman-Tukey deviates ftResiduals.

      Parameters

      Returns GTestResult

      The test result, with statisticName "G"; see GTestResult.

      RangeError for negative or non-finite counts, a total of 0, fewer than 2 counts, p of the wrong length, negative or not summing to 1, "yates" for anything but a 2 x 2 table or two counts, and "williams" for a table with an empty row or column.

      gTest([[12, 5], [7, 9]]).statistic;                      // 2.4600430077307167
      gTest([[12, 5], [7, 9]], { correct: "williams" }).pValue; // 0.12530270793206313
      gTest([10, 20, 30], { p: [0.2, 0.3, 0.5] }).pValue; // 0.7527706003419717