Supplement/comparison_tools/stats.py
def summary_stats(x, y, nm=None):
    """
    Compute summary statistics for paired x, y data.

    Tests
    -----

    Parameters
    ----------
    x, y : array-like
        Data to compare
    nm : str (optional)
        Index value of created dataframe.

    Returns
    -------
    pandas dataframe of statistics.
    """
    # create datafrane for results
    if isinstance(nm, str):
        nm = [nm]
    # cols = pd.MultiIndex.from_arrays([['', 'Pairwise', 'Pairwise', cat, cat, cat, cat],
    #                                   ['N', 'W', 'p', 'Median', 'IQR', 'W', 'p']])
#     cols = ['Median', 'IQR', 'CI95', 'L95', 'LQ', 'UQ', 'U95', 'N',
#             'Wilcoxon_stat', 'Wilcoxon_p',
#             'KS_stat', 'KS_p',
#             'LR_slope', 'LR_intercept', 'LR_slope_tvalue', 'LR_intercept_tvalue', 'LR_slope_p', 'LR_intercept_p', 'LR_R2adj']
#     out = pd.DataFrame(index=nm, columns=cols)
    
    cols = pd.MultiIndex.from_tuples([('Residual Summary', 'N'),
                                      ('Residual Summary', 'Median'),
                                      ('Residual Summary', 'LQ'),
                                      ('Residual Summary', 'IQR'),
                                      ('Residual Summary', 'UQ'),
                                      ('Residual Regression', 'Slope'),
                                      ('Residual Regression', 'Slope t'),
                                      ('Residual Regression', 'Slope p'),
                                      ('Residual Regression', 'Intercept'),
                                      ('Residual Regression', 'Intercept t'),
                                      ('Residual Regression', 'Intercept p'),
                                      ('Residual Regression', 'R2'),
                                      ('Kolmogorov-Smirnov', 'KS'),
                                      ('Kolmogorov-Smirnov', 'p')])
    
    out = pd.DataFrame(index=nm, columns=cols)
    

    # remove nan values
    ind = ~(np.isnan(x) | np.isnan(y))
    x = x[ind]
    y = y[ind]

    # calculate residuals
    r = y - x

    # summary statistics
    cat = 'Residual Summary'
    out.loc[:, (cat, 'N')] = len(x)
    out.loc[:, (cat, 'Median')] = np.median(r)
    out.loc[:, [(cat, 'LQ'), (cat, 'UQ')]] = np.percentile(r, [25, 75])
    out.loc[:, (cat, 'IQR')] = out.loc[:, (cat, 'UQ')] - out.loc[:, (cat, 'LQ')]

    # non-paired test for same distribution
    cat = 'Kolmogorov-Smirnov'
    ks = stats.ks_2samp(x, y)
    out.loc[:, (cat, 'KS')] = ks.statistic
    out.loc[:, (cat, 'p')] = ks.pvalue

    # regression analysis of residuals - slope should be 0, intercept should be 0
    cat = 'Residual Regression'
    X = sm.add_constant(x)
    reg = sm.OLS(r, X, missing='drop')
    fit = reg.fit()
    
    out.loc[:, [(cat, 'Intercept'), (cat, 'Slope')]] = fit.params
    out.loc[:, [(cat, 'Intercept t'), (cat, 'Slope t')]] = fit.tvalues
    out.loc[:, (cat, 'R2')] = fit.rsquared
    out.loc[:, [(cat, 'Intercept p'), (cat, 'Slope p')]] = fit.pvalues

    return out
def summary_stats(x, y, nm=None):
    # create datafrane for results
    if isinstance(nm, str):
        nm = [nm]
    # cols = pd.MultiIndex.from_arrays([['', 'Pairwise', 'Pairwise', cat, cat, cat, cat],
    #                                   ['N', 'W', 'p', 'Median', 'IQR', 'W', 'p']])
#     cols = ['Median', 'IQR', 'CI95', 'L95', 'LQ', 'UQ', 'U95', 'N',
#             'Wilcoxon_stat', 'Wilcoxon_p',
#             'KS_stat', 'KS_p',
#             'LR_slope', 'LR_intercept', 'LR_slope_tvalue', 'LR_intercept_tvalue', 'LR_slope_p', 'LR_intercept_p', 'LR_R2adj']
#     out = pd.DataFrame(index=nm, columns=cols)
    
    cols = pd.MultiIndex.from_tuples([('Residual Summary', 'N'),
                                      ('Residual Summary', 'Median'),
                                      ('Residual Summary', 'LQ'),
                                      ('Residual Summary', 'IQR'),
                                      ('Residual Summary', 'UQ'),
                                      ('Residual Regression', 'Slope'),
                                      ('Residual Regression', 'Slope t'),
                                      ('Residual Regression', 'Slope p'),
                                      ('Residual Regression', 'Intercept'),
                                      ('Residual Regression', 'Intercept t'),
                                      ('Residual Regression', 'Intercept p'),
                                      ('Residual Regression', 'R2'),
                                      ('Kolmogorov-Smirnov', 'KS'),
                                      ('Kolmogorov-Smirnov', 'p')])
    
    out = pd.DataFrame(index=nm, columns=cols)
    

    # remove nan values
    ind = ~(np.isnan(x) | np.isnan(y))
    x = x[ind]
    y = y[ind]

    # calculate residuals
    r = y - x

    # summary statistics
    cat = 'Residual Summary'
    out.loc[:, (cat, 'N')] = len(x)
    out.loc[:, (cat, 'Median')] = np.median(r)
    out.loc[:, [(cat, 'LQ'), (cat, 'UQ')]] = np.percentile(r, [25, 75])
    out.loc[:, (cat, 'IQR')] = out.loc[:, (cat, 'UQ')] - out.loc[:, (cat, 'LQ')]

    # non-paired test for same distribution
    cat = 'Kolmogorov-Smirnov'
    ks = stats.ks_2samp(x, y)
    out.loc[:, (cat, 'KS')] = ks.statistic
    out.loc[:, (cat, 'p')] = ks.pvalue

    # regression analysis of residuals - slope should be 0, intercept should be 0
    cat = 'Residual Regression'
    X = sm.add_constant(x)
    reg = sm.OLS(r, X, missing='drop')
    fit = reg.fit()
    
    out.loc[:, [(cat, 'Intercept'), (cat, 'Slope')]] = fit.params
    out.loc[:, [(cat, 'Intercept t'), (cat, 'Slope t')]] = fit.tvalues
    out.loc[:, (cat, 'R2')] = fit.rsquared
    out.loc[:, [(cat, 'Intercept p'), (cat, 'Slope p')]] = fit.pvalues

    return out
def summary_stats ( x , y , nm = None ) : # create datafrane for results if isinstance ( nm , str ) : nm = [ nm ] # cols = pd.MultiIndex.from_arrays([['', 'Pairwise', 'Pairwise', cat, cat, cat, cat], #                                   ['N', 'W', 'p', 'Median', 'IQR', 'W', 'p']]) #     cols = ['Median', 'IQR', 'CI95', 'L95', 'LQ', 'UQ', 'U95', 'N', #             'Wilcoxon_stat', 'Wilcoxon_p', #             'KS_stat', 'KS_p', #             'LR_slope', 'LR_intercept', 'LR_slope_tvalue', 'LR_intercept_tvalue', 'LR_slope_p', 'LR_intercept_p', 'LR_R2adj'] #     out = pd.DataFrame(index=nm, columns=cols) cols = pd . MultiIndex . from_tuples ( [ ( 'Residual Summary' , 'N' ) , ( 'Residual Summary' , 'Median' ) , ( 'Residual Summary' , 'LQ' ) , ( 'Residual Summary' , 'IQR' ) , ( 'Residual Summary' , 'UQ' ) , ( 'Residual Regression' , 'Slope' ) , ( 'Residual Regression' , 'Slope t' ) , ( 'Residual Regression' , 'Slope p' ) , ( 'Residual Regression' , 'Intercept' ) , ( 'Residual Regression' , 'Intercept t' ) , ( 'Residual Regression' , 'Intercept p' ) , ( 'Residual Regression' , 'R2' ) , ( 'Kolmogorov-Smirnov' , 'KS' ) , ( 'Kolmogorov-Smirnov' , 'p' ) ] ) out = pd . DataFrame ( index = nm , columns = cols ) # remove nan values ind = ~ ( np . isnan ( x ) | np . isnan ( y ) ) x = x [ ind ] y = y [ ind ] # calculate residuals r = y - x # summary statistics cat = 'Residual Summary' out . loc [ : , ( cat , 'N' ) ] = len ( x ) out . loc [ : , ( cat , 'Median' ) ] = np . median ( r ) out . loc [ : , [ ( cat , 'LQ' ) , ( cat , 'UQ' ) ] ] = np . percentile ( r , [ 25 , 75 ] ) out . loc [ : , ( cat , 'IQR' ) ] = out . loc [ : , ( cat , 'UQ' ) ] - out . loc [ : , ( cat , 'LQ' ) ] # non-paired test for same distribution cat = 'Kolmogorov-Smirnov' ks = stats . ks_2samp ( x , y ) out . loc [ : , ( cat , 'KS' ) ] = ks . statistic out . loc [ : , ( cat , 'p' ) ] = ks . pvalue # regression analysis of residuals - slope should be 0, intercept should be 0 cat = 'Residual Regression' X = sm . add_constant ( x ) reg = sm . OLS ( r , X , missing = 'drop' ) fit = reg . fit ( ) out . loc [ : , [ ( cat , 'Intercept' ) , ( cat , 'Slope' ) ] ] = fit . params out . loc [ : , [ ( cat , 'Intercept t' ) , ( cat , 'Slope t' ) ] ] = fit . tvalues out . loc [ : , ( cat , 'R2' ) ] = fit . rsquared out . loc [ : , [ ( cat , 'Intercept p' ) , ( cat , 'Slope p' ) ] ] = fit . pvalues return out
Compute summary statistics for paired x, y data.

    Tests
    -----

    Parameters
    ----------
    x, y : array-like
        Data to compare
    nm : str (optional)
        Index value of created dataframe.

    Returns
    -------
    pandas dataframe of statistics.
Compute summary statistics for paired x y data .