code: def homoscedasticity(
      data, dv=None, group=None, method="levene", alpha=0.05, **kwargs
  ):
      """Test equality of variance.
  
      Parameters
      ----------
      data : :py:class:`pandas.DataFrame`, list or dict
          Iterable. Can be either a list / dictionnary of iterables
          or a wide- or long-format pandas dataframe.
      dv : str
          Dependent variable (only when ``data`` is a long-format dataframe).
      group : str
          Grouping variable (only when ``data`` is a long-format dataframe).
      method : str
          Statistical test. `'levene'` (default) performs the Levene test
          using :py:func:`scipy.stats.levene`, and `'bartlett'` performs the
          Bartlett test using :py:func:`scipy.stats.bartlett`.
          The former is more robust to departure from normality.
      alpha : float
          Significance level.
      **kwargs : optional
          Optional argument(s) passed to the lower-level :py:func:`scipy.stats.levene` function.
  
      Returns
      -------
      stats : :py:class:`pandas.DataFrame`
  
          * ``'W/T'``: Test statistic ('W' for Levene, 'T' for Bartlett)
          * ``'pval'``: p-value
          * ``'equal_var'``: True if ``data`` has equal variance
  
      See Also
      --------
      normality : Univariate normality test.
      sphericity : Mauchly's test for sphericity.
  
      Notes
      -----
      The **Bartlett** :math:`T` statistic [1]_ is defined as:
  
      .. math::
  
          T = \\frac{(N-k) \\ln{s^{2}_{p}} - \\sum_{i=1}^{k}(N_{i} - 1)
          \\ln{s^{2}_{i}}}{1 + (1/(3(k-1)))((\\sum_{i=1}^{k}{1/(N_{i} - 1))}
          - 1/(N-k))}
  
      where :math:`s_i^2` is the variance of the :math:`i^{th}` group,
      :math:`N` is the total sample size, :math:`N_i` is the sample size of the
      :math:`i^{th}` group, :math:`k` is the number of groups,
      and :math:`s_p^2` is the pooled variance.
  
      The pooled variance is a weighted average of the group variances and is
      defined as:
  
      .. math:: s^{2}_{p} = \\sum_{i=1}^{k}(N_{i} - 1)s^{2}_{i}/(N-k)
  
      The p-value is then computed using a chi-square distribution:
  
      .. math:: T \\sim \\chi^2(k-1)
  
      The **Levene** :math:`W` statistic [2]_ is defined as:
  
      .. math::
  
          W = \\frac{(N-k)} {(k-1)}
          \\frac{\\sum_{i=1}^{k}N_{i}(\\overline{Z}_{i.}-\\overline{Z})^{2} }
          {\\sum_{i=1}^{k}\\sum_{j=1}^{N_i}(Z_{ij}-\\overline{Z}_{i.})^{2} }
  
      where :math:`Z_{ij} = |Y_{ij} - \\text{median}({Y}_{i.})|`,
      :math:`\\overline{Z}_{i.}` are the group means of :math:`Z_{ij}` and
      :math:`\\overline{Z}` is the grand mean of :math:`Z_{ij}`.
  
      The p-value is then computed using a F-distribution:
  
      .. math:: W \\sim F(k-1, N-k)
  
      .. warning:: Missing values are not supported for this function.
          Make sure to remove them before using the
          :py:meth:`pandas.DataFrame.dropna` or :py:func:`pingouin.remove_na`
          functions.
  
      References
      ----------
      .. [1] Bartlett, M. S. (1937). Properties of sufficiency and statistical
             tests. Proc. R. Soc. Lond. A, 160(901), 268-282.
  
      .. [2] Brown, M. B., & Forsythe, A. B. (1974). Robust tests for the
             equality of variances. Journal of the American Statistical
             Association, 69(346), 364-367.
  
      Examples
      --------
      1. Levene test on a wide-format dataframe
  
      >>> import numpy as np
      >>> import pingouin as pg
      >>> data = pg.read_dataset('mediation')
      >>> pg.homoscedasticity(data[['X', 'Y', 'M']])
                     W      pval  equal_var
      levene  1.173518  0.310707       True
  
      2. Same data but using a long-format dataframe
  
      >>> data_long = data[['X', 'Y', 'M']].melt()
      >>> pg.homoscedasticity(data_long, dv="value", group="variable")
                     W      pval  equal_var
      levene  1.173518  0.310707       True
  
      3. Same but using a mean center
  
      >>> pg.homoscedasticity(data_long, dv="value", group="variable", center="mean")
                     W      pval  equal_var
      levene  1.572239  0.209303       True
  
      4. Bartlett test using a list of iterables
  
      >>> data = [[4, 8, 9, 20, 14], np.array([5, 8, 15, 45, 12])]
      >>> pg.homoscedasticity(data, method="bartlett", alpha=.05)
                       T      pval  equal_var
      bartlett  2.873569  0.090045       True
      """
      assert isinstance(data, (pd.DataFrame, list, dict))
      assert method.lower() in ["levene", "bartlett"]
      func = getattr(scipy.stats, method)
      if isinstance(data, pd.DataFrame):
          # Data is a Pandas DataFrame
          if dv is None and group is None:
              # Wide-format
              # Get numeric data only
              numdata = data._get_numeric_data()
              assert (
                  numdata.shape[1] > 1
              ), "Data must have at least two columns."
              statistic, p = func(*numdata.to_numpy().T, **kwargs)
          else:
              # Long-format
              assert group in data.columns
              assert dv in data.columns
              grp = data.groupby(group, observed=True)[dv]
              assert (
                  grp.ngroups > 1
              ), "Data must have at least two columns."
              statistic, p = func(*grp.apply(list), **kwargs)
      elif isinstance(data, list):
          # Check that list contains other list or np.ndarray
          assert all(isinstance(el, (list, np.ndarray)) for el in data)
          assert len(data) > 1, "Data must have at least two iterables."
          statistic, p = func(*data, **kwargs)
      else:
          # Data is a dict
          assert all(
              isinstance(el, (list, np.ndarray)) for el in data.values()
          )
          assert len(data) > 1, "Data must have at least two iterables."
          statistic, p = func(*data.values(), **kwargs)
  
      equal_var = True if p > alpha else False
      stat_name = "W" if method.lower() == "levene" else "T"
      stats = pd.DataFrame(
          {stat_name: statistic, "pval": p, "equal_var": equal_var},
          index=[method],
      )
  
      return _postprocess_dataframe(stats)
  
file_path: pingouin/pingouin/distribution.py
imports: import warnings
  import scipy.stats
  import numpy
  import pandas
  from collections import namedtuple
  from pingouin.utils import _flatten_list
  from pingouin.utils import remove_na, _postprocess_dataframe
label_desc: Use image node_id 3 for calling a global function with example usage: homoscedasticity(data, dv, group, method, alpha) and returns: _postprocess_dataframe