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

    Function chisqTest

    • Pearson's chi-squared test. Equivalent to R's chisq.test().

      Given an array of counts, tests goodness of fit to options.p. Given a matrix (an array of rows), tests independence in the contingency table; a matrix with a single row or column is treated as a goodness-of-fit test, as in R. Yates' correction is applied to 2 x 2 tables by default; as in R, the amount subtracted is min(0.5, min |O - E|) over all cells. Sums are computed in extended precision, like R.

      With simulatePValue: true the p-value is computed from B simulated samples (multinomial for goodness of fit, random tables with the observed margins otherwise), drawn from the random stream of setSeed exactly as in R, so setSeed(s) before the call gives R's p-value after set.seed(s).

      Given two factor vectors (arrays of labels of the same length), tests independence in their contingency table, as R's chisq.test(x, y); pairs with a missing label are dropped and the levels are sorted by code point.

      With nMinus1: true (not in R) the statistic is Campbell's (2007) 'N - 1' chi-squared, Pearson's statistic without continuity correction times (N - 1) / N, for 2 x 2 and larger tables with a total N above 1. The result also has the Freeman-Tukey deviates ftResiduals (not in R).

      As R, warnings has "Chi-squared approximation may be incorrect" where an expected count is below 5 (unless the p-value is simulated), and "cannot compute simulated p-value with zero marginals" where simulatePValue is asked for a table with a zero row or column sum (the ordinary test is done).

      Parameters

      Returns ChisqTestResult

      The test result, with statisticName "X-squared"; see ChisqTestResult.

      const r = chisqTest([[12, 5], [7, 15]]);
      r.statistic; // 4.322120391218687 (with Yates' continuity correction)
      r.pValue; // 0.03762001663483994

      chisqTest([20, 30, 50]).pValue; // 0.0009118819655545154 (goodness of fit, equal p)

      setSeed(1);
      chisqTest([20, 15, 25], { simulatePValue: true }).pValue; // 0.296351824087956 (R: set.seed(1); chisq.test(...))

      chisqTest(["a", "b", "a", "b"], ["u", "u", "v", "v"]).rowLevels; // ["a", "b"]

      chisqTest([[12, 5], [7, 9]], { nMinus1: true }).statistic; // 2.3569217160548437 (the 'N - 1' test)
    • Pearson's chi-squared test. Equivalent to R's chisq.test().

      Given an array of counts, tests goodness of fit to options.p. Given a matrix (an array of rows), tests independence in the contingency table; a matrix with a single row or column is treated as a goodness-of-fit test, as in R. Yates' correction is applied to 2 x 2 tables by default; as in R, the amount subtracted is min(0.5, min |O - E|) over all cells. Sums are computed in extended precision, like R.

      With simulatePValue: true the p-value is computed from B simulated samples (multinomial for goodness of fit, random tables with the observed margins otherwise), drawn from the random stream of setSeed exactly as in R, so setSeed(s) before the call gives R's p-value after set.seed(s).

      Given two factor vectors (arrays of labels of the same length), tests independence in their contingency table, as R's chisq.test(x, y); pairs with a missing label are dropped and the levels are sorted by code point.

      With nMinus1: true (not in R) the statistic is Campbell's (2007) 'N - 1' chi-squared, Pearson's statistic without continuity correction times (N - 1) / N, for 2 x 2 and larger tables with a total N above 1. The result also has the Freeman-Tukey deviates ftResiduals (not in R).

      As R, warnings has "Chi-squared approximation may be incorrect" where an expected count is below 5 (unless the p-value is simulated), and "cannot compute simulated p-value with zero marginals" where simulatePValue is asked for a table with a zero row or column sum (the ordinary test is done).

      Parameters

      Returns ChisqTestFactorsResult

      The test result, with statisticName "X-squared"; see ChisqTestResult.

      const r = chisqTest([[12, 5], [7, 15]]);
      r.statistic; // 4.322120391218687 (with Yates' continuity correction)
      r.pValue; // 0.03762001663483994

      chisqTest([20, 30, 50]).pValue; // 0.0009118819655545154 (goodness of fit, equal p)

      setSeed(1);
      chisqTest([20, 15, 25], { simulatePValue: true }).pValue; // 0.296351824087956 (R: set.seed(1); chisq.test(...))

      chisqTest(["a", "b", "a", "b"], ["u", "u", "v", "v"]).rowLevels; // ["a", "b"]

      chisqTest([[12, 5], [7, 9]], { nMinus1: true }).statistic; // 2.3569217160548437 (the 'N - 1' test)