class_docstr: Test multivariate.py.
  Tested against the R package MVN.
docstr: Test function box_m.
  
  Tested against the R package biotools (iris dataset).
code: def test_box_m(self):
      """Test function box_m.
  
      Tested against the R package biotools (iris dataset).
      """
      # Test 1: Iris dataset
      iris = datasets.load_iris()
      df = pd.DataFrame(
          data=np.c_[iris["data"], iris["target"]],
          columns=iris["feature_names"] + ["target"],
      )
      stats = box_m(
          df,
          dvs=[
              "sepal length (cm)",
              "sepal width (cm)",
              "petal length (cm)",
              "petal width (cm)",
          ],
          group="target",
      )
      assert round(stats.at["box", "Chi2"], 3) == 140.943
      assert stats.at["box", "df"] == 20
      assert np.isclose(stats.at["box", "pval"], 3.352034e-20)
  
      # Test 2: Multivariate normal dist with balanced sample size
      # In R:
      # >>> library(biotools)
      # >>> data < - read.csv("data.csv")
      # >>> boxM(data[, c('A', 'B', 'C')], grouping=data[, c('group')])
      from scipy.stats import multivariate_normal as mvn
  
      data = pd.DataFrame(
          mvn.rvs(size=(100, 3), random_state=42),
          columns=["A", "B", "C"],
      )
      data["group"] = [1] * 25 + [2] * 25 + [3] * 25 + [4] * 25
      stats = box_m(data, dvs=["A", "B", "C"], group="group")
      assert round(stats.at["box", "Chi2"], 5) == 11.63419
      assert stats.at["box", "df"] == 18
      assert round(stats.at["box", "pval"], 7) == 0.8655372
  
      # Test 3: Multivariate normal dist with unbalanced sample size
      data = pd.DataFrame(
          mvn.rvs(size=(30, 2), random_state=42), columns=["A", "B"]
      )
      data["group"] = [1] * 20 + [2] * 10
      stats = box_m(data, dvs=["A", "B"], group="group")
      assert round(stats.at["box", "Chi2"], 5) == 0.70671
      assert stats.at["box", "df"] == 3
      assert round(stats.at["box", "pval"], 7) == 0.8716249
  
      # Test 4: with missing values
      # In R, biotools return NA for Chi2 and p-value
      data.loc[[1, 3], ["A", "B"]] = np.nan
      stats = box_m(data, dvs=["A", "B"], group="group")
      assert stats.at["box", "df"] == 3
  
file_path: pingouin/pingouin/tests/test_multivariate.py
imports: import numpy
  import pandas
  from sklearn import datasets
  from unittest import TestCase
  from pingouin import read_dataset
  from pingouin.multivariate import multivariate_normality, multivariate_ttest, box_m
label_desc: Use image node_id 3 for calling the TestMultivariate obj's underlying member method code with example usage: obj.test_box_m() without return types