class_docstr: Test regression.py.
docstr: Test function mediation_analysis.
docstr_tok: ["Test","function","mediation_analysis","."]
code: def test_mediation_analysis(self):
      """Test function mediation_analysis."""
      df = read_dataset("mediation")
      df["Zero"], df["One"] = 0, 1
      ma = mediation_analysis(data=df, x="X", m="M", y="Y", n_boot=500)
  
      # Compare against R package mediation
      assert_equal(
          ma["coef"].round(4).to_numpy(),
          [0.5610, 0.6542, 0.3961, 0.0396, 0.3565],
      )
  
      _, dist = mediation_analysis(
          data=df, x="X", m="M", y="Y", n_boot=1000, return_dist=True
      )
      assert dist.size == 1000
      mediation_analysis(data=df, x="X", m="M", y="Y", alpha=0.01)
  
      # Check with a binary mediator
      ma = mediation_analysis(
          data=df, x="X", m="Mbin", y="Y", n_boot=2000
      )
      assert_almost_equal(ma["coef"][0], -0.0208, decimal=2)
  
      # Indirect effect
      assert_almost_equal(ma["coef"][4], 0.0033, decimal=2)
      assert ma["sig"][4] == "No"
  
      # Direct effect
      assert_almost_equal(ma["coef"][3], 0.3956, decimal=2)
      assert_almost_equal(ma["CI[2.5%]"][3], 0.1714, decimal=2)
      assert_almost_equal(ma["CI[97.5%]"][3], 0.617, decimal=1)
      assert ma["sig"][3] == "Yes"
  
      # Check if `logreg_kwargs` is being passed on to `LogisticRegression`
      with pytest.raises(ValueError):
          mediation_analysis(
              data=df,
              x="X",
              m="Mbin",
              y="Y",
              n_boot=2000,
              logreg_kwargs=dict(max_iter=-1),
          )
      # Solve with 0 iterations and make sure that the results are different
      ma = mediation_analysis(
          data=df,
          x="X",
          m="Mbin",
          y="Y",
          n_boot=2000,
          logreg_kwargs=dict(max_iter=0),
      )
      with pytest.raises(AssertionError):
          assert_almost_equal(ma["coef"][0], -0.0208, decimal=2)
      with pytest.raises(AssertionError):
          assert_almost_equal(ma["coef"][4], 0.0033, decimal=3)
  
      # With multiple mediator
      np.random.seed(42)
      df.rename(columns={"M": "M1"}, inplace=True)
      df["M2"] = np.random.randint(0, 10, df.shape[0])
      ma2 = mediation_analysis(
          data=df, x="X", m=["M1", "M2"], y="Y", seed=42
      )
      assert ma["coef"][2] == ma2["coef"][4]
  
      # With covariate
      mediation_analysis(data=df, x="X", m="M1", y="Y", covar="M2")
      mediation_analysis(data=df, x="X", m="M1", y="Y", covar=["M2"])
      mediation_analysis(
          data=df, x="X", m=["M1", "Ybin"], y="Y", covar=["Mbin", "M2"]
      )
  
      # Test helper function _pval_from_bootci
      np.random.seed(123)
      bt2 = np.random.normal(loc=2, size=1000)
      bt3 = np.random.normal(loc=3, size=1000)
      assert _pval_from_bootci(bt2, 0) == 1
      assert _pval_from_bootci(bt2, 0.9) < 0.10
      assert _pval_from_bootci(bt3, 0.9) < _pval_from_bootci(bt2, 0.9)
  
file_path: pingouin/pingouin/tests/test_regression.py
imports: import pytest
  import numpy
  import pandas
  from unittest import TestCase
  from scipy.stats import linregress, zscore
  from sklearn.linear_model import LinearRegression
  import statsmodels.api
  from pandas.testing import assert_frame_equal
  from numpy.testing import assert_almost_equal, assert_equal
  from pingouin import read_dataset
  from pingouin.regression import linear_regression, logistic_regression, mediation_analysis, _pval_from_bootci
label_desc: Use image node_id 3 for calling the TestRegression obj's underlying member method code with example usage: obj.test_mediation_analysis() without return types