def test_durbin(a, y_col=None, block_col=None, group_col=None, melted=False, sort=True):

    '''Durbin's test whether k groups (or treatments) in a two-way
    balanced incomplete block design (BIBD) have identical effects. See
    references for additional information [1]_, [2]_.

    Parameters
    ----------
    a : array_like or pandas DataFrame object
        An array, any object exposing the array interface or a pandas
        DataFrame.

        If `melted` is set to False (default), `a` is a typical matrix of block design,
        i.e. rows are blocks, and columns are groups. In this case you do
        not need to specify col arguments.

        If `a` is an array and `melted` is set to True,
        y_col, block_col and group_col must specify the indices of columns
        containing elements of correspondary type.

        If `a` is a Pandas DataFrame and `melted` is set to True,
        y_col, block_col and group_col must specify columns names (string).

    y_col : str or int
        Must be specified if `a` is a pandas DataFrame object.
        Name of the column that contains y data.

    block_col : str or int
        Must be specified if `a` is a pandas DataFrame object.
        Name of the column that contains block names.

    group_col : str or int
        Must be specified if `a` is a pandas DataFrame object.
        Name of the column that contains group names.

    melted : bool, optional
        Specifies if data are given as melted columns "y", "blocks", and
        "groups".

    sort : bool, optional
        If True, sort data by block and group columns.

    Returns
    -------
    pval : float


    References
    ----------
    .. [1] N. A. Heckert, J. J. Filliben. (2003) NIST Handbook 148: Dataplot Reference
        Manual, Volume 2: Let Subcommands and Library Functions. National Institute of
        Standards and Technology Handbook Series, June 2003.
    .. [2] W. J. Conover (1999), Practical nonparametric Statistics,
        3rd. edition, Wiley.

    Examples
    --------
    >>> x = np.array([[31,27,24],[31,28,31],[45,29,46],[21,18,48],[42,36,46],[32,17,40]])
    >>> sp.test_durbin(x)

    '''

    if melted and not all([block_col, group_col, y_col]):
        raise ValueError('block_col, group_col, y_col should be explicitly specified if using melted data')

    x, _y_col, _group_col, _block_col = __convert_to_block_df(a, y_col, group_col, block_col, melted)

    groups = x[_group_col].unique()
    blocks = x[_block_col].unique()
    if not sort:
        x[_group_col] = Categorical(x[_group_col], categories=groups, ordered=True)
        x[_block_col] = Categorical(x[_block_col], categories=blocks, ordered=True)
    x.sort_values(by=[_block_col, _group_col], ascending=True, inplace=True)
    x.dropna(inplace=True)

    t = len(groups)
    b = len(blocks)
    r = np.unique(x.groupby(_group_col).count())
    k = np.unique(x.groupby(_group_col).count())
    if r.size > 1 and k.size > 1:
        raise ValueError('Data appear to be unbalanced. Please correct your input data')
    else:
        r = r.item()
        k = k.item()
    x['y_ranks'] = x.groupby(_block_col)[_y_col].rank()
    x['y_ranks_sum'] = x.groupby(_group_col)['y_ranks'].sum()

    A = np.sum(x['y_ranks'] ** 2.)
    C = (b * k * (k + 1)**2.) / 4.
    D = np.sum(x['y_ranks_sum'] ** 2.) - r * C
    T1 = (t - 1.) / (A - C) * D

    stat = T1
    df = t - 1
    pval = ss.chi2.sf(stat, df)

    return pval, stat, df
def test_durbin ( a , y_col = None , block_col = None , group_col = None , melted = False , sort = True ) : if melted and not all ( [ block_col , group_col , y_col ] ) : raise ValueError ( 'block_col, group_col, y_col should be explicitly specified if using melted data' ) x , _y_col , _group_col , _block_col = __convert_to_block_df ( a , y_col , group_col , block_col , melted ) groups = x [ _group_col ] . unique ( ) blocks = x [ _block_col ] . unique ( ) if not sort : x [ _group_col ] = Categorical ( x [ _group_col ] , categories = groups , ordered = True ) x [ _block_col ] = Categorical ( x [ _block_col ] , categories = blocks , ordered = True ) x . sort_values ( by = [ _block_col , _group_col ] , ascending = True , inplace = True ) x . dropna ( inplace = True ) t = len ( groups ) b = len ( blocks ) r = np . unique ( x . groupby ( _group_col ) . count ( ) ) k = np . unique ( x . groupby ( _group_col ) . count ( ) ) if r . size > 1 and k . size > 1 : raise ValueError ( 'Data appear to be unbalanced. Please correct your input data' ) else : r = r . item ( ) k = k . item ( ) x [ 'y_ranks' ] = x . groupby ( _block_col ) [ _y_col ] . rank ( ) x [ 'y_ranks_sum' ] = x . groupby ( _group_col ) [ 'y_ranks' ] . sum ( ) A = np . sum ( x [ 'y_ranks' ] ** 2. ) C = ( b * k * ( k + 1 ) ** 2. ) / 4. D = np . sum ( x [ 'y_ranks_sum' ] ** 2. ) - r * C T1 = ( t - 1. ) / ( A - C ) * D stat = T1 df = t - 1 pval = ss . chi2 . sf ( stat , df ) return pval , stat , df
Durbin's test whether k groups (or treatments) in a two-way
    balanced incomplete block design (BIBD) have identical effects. See
    references for additional information [1]_, [2]_.

    Parameters
    ----------
    a : array_like or pandas DataFrame object
        An array, any object exposing the array interface or a pandas
        DataFrame.

        If `melted` is set to False (default), `a` is a typical matrix of block design,
        i.e. rows are blocks, and columns are groups. In this case you do
        not need to specify col arguments.

        If `a` is an array and `melted` is set to True,
        y_col, block_col and group_col must specify the indices of columns
        containing elements of correspondary type.

        If `a` is a Pandas DataFrame and `melted` is set to True,
        y_col, block_col and group_col must specify columns names (string).

    y_col : str or int
        Must be specified if `a` is a pandas DataFrame object.
        Name of the column that contains y data.

    block_col : str or int
        Must be specified if `a` is a pandas DataFrame object.
        Name of the column that contains block names.

    group_col : str or int
        Must be specified if `a` is a pandas DataFrame object.
        Name of the column that contains group names.

    melted : bool, optional
        Specifies if data are given as melted columns "y", "blocks", and
        "groups".

    sort : bool, optional
        If True, sort data by block and group columns.

    Returns
    -------
    pval : float


    References
    ----------
    .. [1] N. A. Heckert, J. J. Filliben. (2003) NIST Handbook 148: Dataplot Reference
        Manual, Volume 2: Let Subcommands and Library Functions. National Institute of
        Standards and Technology Handbook Series, June 2003.
    .. [2] W. J. Conover (1999), Practical nonparametric Statistics,
        3rd. edition, Wiley.

    Examples
    --------
    >>> x = np.array([[31,27,24],[31,28,31],[45,29,46],[21,18,48],[42,36,46],[32,17,40]])
    >>> sp.test_durbin(x)