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

    Function corTest

    • Test for correlation between paired samples. Equivalent to R's cor.test().

      Supports Pearson's product-moment correlation (t test, with a Fisher-z confidence interval), Kendall's tau and Spearman's rho (exact or approximate p-values, chosen by the same rules as R). Pairs containing NaN are dropped; Pearson needs at least 3 pairs and the others at least 2. If the correlation is undefined (constant data) the statistic, p-value and estimate are NaN. Correlations are computed in double-double arithmetic, so results can differ from R's when r is very close to ±1, where R's own value is less accurate.

      As R, warnings has "the standard deviation is zero" where x or y is constant (the correlation is NaN), and "cannot compute exact p-value with ties" (R 4.6's text) where Kendall's or Spearman's exact p-value is asked for or the default but there are ties (the approximation is used), and "NaNs produced" where Spearman's t approximation has 2 pairs (0 degrees of freedom: the p-value is NaN).

      Parameters

      Returns CorTestResult

      The test result; see CorTestResult.

      const r = corTest([1, 2, 3, 4, 5, 6], [2, 1, 4, 3, 7, 8]);
      r.estimate; // 0.9014600868406593
      r.statistic; // 4.165113148435108 (t, df = 4)
      r.pValue; // 0.01408675480909387

      corTest([1, 2, 3, 4, 5, 6], [2, 1, 4, 3, 7, 8], { method: "spearman" });

      There is also a formula form, corTest("~ x + y", data, options) (next overload).

    • Test for correlation from a formula and a data frame. As R's cor.test(formula, data): the one-sided formula "~ x + y" names the two variables. Only they count: the rows where one of them is missing are dropped.

      Parameters

      • formula: string

        "~ x + y" (exactly two variables, no response).

      • data: DataFrame

        The data frame: an object of columns (null or NaN for a missing value).

      • Optionaloptions: CorTestOptions

      Returns CorTestResult