class_docstr: Test nonparametric.py.
docstr: Test function friedman
docstr_tok: ["Test","function","friedman"]
code: def test_friedman(self):
      """Test function friedman"""
      df = pd.DataFrame(
          {
              "white": {
                  0: 10,
                  1: 8,
                  2: 7,
                  3: 9,
                  4: 7,
                  5: 4,
                  6: 5,
                  7: 6,
                  8: 5,
                  9: 10,
                  10: 4,
                  11: 7,
              },
              "red": {
                  0: 7,
                  1: 5,
                  2: 8,
                  3: 6,
                  4: 5,
                  5: 7,
                  6: 9,
                  7: 6,
                  8: 4,
                  9: 6,
                  10: 7,
                  11: 3,
              },
              "rose": {
                  0: 8,
                  1: 5,
                  2: 6,
                  3: 4,
                  4: 7,
                  5: 5,
                  6: 3,
                  7: 7,
                  8: 6,
                  9: 4,
                  10: 4,
                  11: 3,
              },
          }
      )
  
      # Compare R and SciPy
      # >>> friedman.test(data)
      Q, p = scipy.stats.friedmanchisquare(*df.to_numpy().T)
      assert np.isclose(Q, 2)
      assert np.isclose(p, 0.3678794)
  
      # Wide-format
      stats = friedman(df)
      assert np.isclose(stats.at["Friedman", "Q"], Q)
      assert np.isclose(stats.at["Friedman", "p-unc"], p)
      assert np.isclose(stats.at["Friedman", "ddof1"], 2)
  
      # Long format
      df_long = df.melt(ignore_index=False).reset_index()
      stats = friedman(
          data=df_long, dv="value", within="variable", subject="index"
      )
      assert np.isclose(stats.at["Friedman", "Q"], Q)
      assert np.isclose(stats.at["Friedman", "p-unc"], p)
      assert np.isclose(stats.at["Friedman", "ddof1"], 2)
  
      # Compare Kendall's W
      # WARNING: I believe that the value in JASP is wrong (as of Oct 2021), because the W is
      # calculated on the transposed dataset. Indeed, to get the correct W / Q / p, one must use:
      # >>> library(DescTools)
      # >>> KendallW(t(data), correct = T, test = T)
      # Which gives the following output:
      # Kendall chi - squared = 2, df = 2, subjects = 3, raters = 12, p - value = 0.3679
      # alternative hypothesis: Wt is greater 0
      # sample estimates: 0.08333333
      assert np.isclose(stats.at["Friedman", "W"], 0.08333333)
  
      # Using the F-test method, which is more conservative
      stats_f = friedman(df, method="f")
      assert (
          stats_f.at["Friedman", "p-unc"]
          > stats.at["Friedman", "p-unc"]
      )
  
file_path: pingouin/pingouin/tests/test_nonparametric.py
imports: import pytest
  import scipy
  import numpy
  import pandas
  from unittest import TestCase
  from pingouin.nonparametric import mad, madmedianrule, mwu, wilcoxon, kruskal, friedman, cochran, harrelldavis
label_desc: Use image node_id 6 for calling the TestNonParametric obj's underlying member method code with example usage: obj.test_friedman() without return types