class_docstr: Test contingency.py.
docstr: Test function chi2_independence.
code: def test_chi2_independence(self):
      """Test function chi2_independence."""
      # Setup
      np.random.seed(42)
      mean, cov = [0.5, 0.5], [(1, 0.6), (0.6, 1)]
      x, y = np.random.multivariate_normal(mean, cov, 30).T
      data = pd.DataFrame({"x": x, "y": y})
      mask_class_1 = data > 0.5
      data[mask_class_1] = 1
      data[~mask_class_1] = 0
  
      # Comparing results with SciPy
      _, _, stats = pg.chi2_independence(data, x="x", y="y")
      contingency_table = pd.crosstab(data["x"], data["y"])
      for i in stats.index:
          lambda_ = stats.at[i, "lambda"]
          dof = stats.at[i, "dof"]
          chi2 = stats.at[i, "chi2"]
          p = round(stats.at[i, "pval"], 6)
          sp_chi2, sp_p, sp_dof, _ = chi2_contingency(
              contingency_table, lambda_=lambda_
          )
          np.testing.assert_allclose(
              [chi2, p, dof], [sp_chi2, sp_p, sp_dof], rtol=1e-4
          )
  
      # Testing resilience to NaN
      mask_nan = np.random.random(data.shape) > 0.8  # ~20% NaN values
      data[mask_nan] = np.nan
      pg.chi2_independence(data, x="x", y="y")
  
      # Testing validations
      def expect_assertion_error(*params):
          with pytest.raises(AssertionError):
              pg.chi2_independence(*params)
  
      expect_assertion_error(1, "x", "y")  # Not a pd.DataFrame
      expect_assertion_error(data, x, "y")  # Not a string
      expect_assertion_error(data, "x", y)  # Not a string
      expect_assertion_error(data, "x", "z")  # Not a column of data
  
      # Testing "no data" ValueError
      data["x"] = np.nan
      with pytest.raises(ValueError):
          pg.chi2_independence(data, x="x", y="y")
  
      # Testing degenerated case (observed == expected)
      data["x"] = 1
      data["y"] = 1
      expected, observed, stats = pg.chi2_independence(data, "x", "y")
      assert expected.iloc[0, 0] == observed.iloc[0, 0]
      assert stats.at[0, "dof"] == 0
      for i in stats.index:
          chi2 = stats.at[i, "chi2"]
          p = stats.at[i, "pval"]
          assert (chi2, p) == (0.0, 1.0)
  
      # Testing warning on low count
      data.iloc[0, 0] = 0
      with pytest.warns(UserWarning):
          pg.chi2_independence(data, "x", "y")
  
      # Comparing results with R
      # 2 x 2 contingency table (dof = 1)
      # >>> tbl = table(df$sex, df$target)
      # >>> chisq.test(tbl, correct = TRUE)
      # >>> cramersV(tbl)
      _, _, stats = pg.chi2_independence(df_ind, "sex", "target")
      assert round(stats.at[0, "chi2"], 3) == 22.717
      assert stats.at[0, "dof"] == 1
      assert np.isclose(stats.at[0, "pval"], 1.877e-06)
      assert round(stats.at[0, "cramer"], 2) == 0.27
  
      # 4 x 2 contingency table
      _, _, stats = pg.chi2_independence(df_ind, "cp", "target")
      assert round(stats.at[0, "chi2"], 3) == 81.686
      assert stats.at[0, "dof"] == 3.0
      assert stats.at[0, "pval"] < 2.2e-16
      assert round(stats.at[0, "cramer"], 3) == 0.519
      assert np.isclose(stats.at[0, "power"], 1.0)
  
file_path: pingouin/pingouin/tests/test_contingency.py
imports: import pytest
  import numpy
  import pandas
  import pingouin
  from unittest import TestCase
  from scipy.stats import chi2_contingency
label_desc: Use image node_id 1 for calling the TestContingency obj's underlying member method code with example usage: obj.test_chi2_independence() without return types