Example #1
0
def oneway(Y, X, match=None, sub=None, par=True, title=None):
    "data: for should iter over groups/treatments"
    data, datalabels, names, within = _split_Y(Y, X, match=match, sub=sub)
    test = _oneway(data, parametric=par, within=within)
    template = "{test}: {statistic}={value}{stars}, p={p}"
    out = template.format(**test)
    return out
Example #2
0
def pairwise(Y, X, match=None, sub=None,            # data in
             par=True, corr='Hochberg', trend=True, # stats
             title='{desc}', mirror=False,        # layout
             ):
    """
    pairwise comparison according to factor structure
    
    """
#    ct = celltable(Y, X, match=match, sub=sub)
    # test
    data, datalabels, names, within = _split_Y(Y, X, match=match, sub=sub)
    test = _pairwise(data, within=within, parametric=par, corr=corr, #levels=levels, 
                     trend=trend)
    # extract test results
    k = len(data)
    indexes = test['pw_indexes']
    statistic = test['statistic']
    _K = test[statistic]
    _P = test['p']
    if corr:
        _Pc = mcp_adjust(_P, corr)
    _df = test['df']
    _NStars = test['stars']
    symbols = test['symbols']
    
    # create TABLE
    table = textab.Table('l'+'l'*(k-1+mirror))
    title_desc = "Pairwise {0}".format(test['test'])
    table.title(title.format(desc=title_desc))
    table.caption(test['caption'])
    
    # headings
    table.cell()
    for name in names[1-mirror:]:
        table.cell(name)
    table.midrule()
    
    tex_peq = textab.texstr("p=")
    #tex_df = textab.Element(df, "_", digits=0)
    if corr and not mirror:
        subrows = range(3)
    else:
        subrows = range(2)
    
    for row in range(0, k-1+mirror):
        for subrow in subrows: # contains t/p
            # names column
            if subrow is 0:
                table.cell(names[row], r"\textbf")
            else:
                table.cell()
            # rows
            for col in range(1-mirror, k):
                if row == col:
                    table.cell()
                elif col > row:
                    index = indexes[(row, col)]
                    #(col-1) + ((k-2)*row) sum(range(k-1, k-1-row, -1))
                    K = _K[index]
                    p = _P[index]
                    df = _df[index]
#                    nstars = _NStars[index]
                    if subrow is 0:
                        tex_cell = textab.eq(statistic, K, df=df, 
                                             stars=symbols[index],   
                                             of=3+trend)
                    elif subrow is 1:
                        tex_cell = textab.texstr([tex_peq, texstr(p, fmt='%.3f')], 
                                                 mat=True)
                    elif subrow is 2:
                        tex_cell = textab.eq('p', _Pc[index], df='c', 
                                             fmt='%.3f', drop0=True)
                    table.cell(tex_cell)
                else:
                    if mirror and corr and subrow==0:
                        index = indexes[(col, row)]
                        p = _Pc[index]
                        table.cell(p, fmt='%.3f', drop0=True)
                    else:
                        table.cell()
    return table
Example #3
0
def data(Y, X=None, match=None, cov=[], sub=None, fmt=None, labels=True, 
          showcase=True):
    """
    return a textab.table (printed as tsv table by default)
    
    parameters
    ----------
    Y: variable to display (can be model with several dependents)

    X: categories defining cells (factorial model)

    match: factor to match values on and return repeated-measures table

    cov: covariate to report (WARNING: only works with match, where each value
         on the matching variable corresponds with one value in the covariate)

    sub: boolean array specifying which values to include (generate e.g. 
         with 'sub=T==[1,2]')

    fmt: Format string  
            
    labels: display labels for nominal variables (otherwise display codes)

    """
    if hasattr(Y, '_items'): # dataframe
        Y = Y._items
    Y = _data.asmodel(Y)
    if _data.isfactor(cov) or _data.isvar(cov):
        cov = [cov]
    
    data = []
    names_yname = [] # names including Yi.name for matched table headers
    ynames = [] # names of Yi for independent measures table headers
    within_list = []
    for Yi in Y.effects:
        _data, datalabels, names, _within = _data._split_Y(Yi, X, match=match, 
                                                     sub=sub, datalabels=match)
        data += _data
        names_yname += ['({c})'.format(c=n) for n in names]
        ynames.append(Yi.name)
        within_list.append(_within)
    within = within_list[0]
    assert all([w==within for w in within_list])
    
    # table
    n_dependents = len(Y.effects)
    n_cells = int(len(data) / n_dependents)
    if within:
        n, k = len(data[0]), len(data)
        table = textab.Table('l' * (k + showcase + len(cov)))
        
        # header line 1
        if showcase:
            table.cell(match.name)
            case_labels = datalabels[0]
            assert all([np.all(case_labels==l) for l in datalabels[1:]])
        for i in range(n_dependents):
            for name in names:        
                table.cell(name.replace(' ','_'))
        for c in cov:
            table.cell(c.name)
        
        # header line 2
        if n_dependents > 1:
            if showcase:
                table.cell()
            for name in ynames:
                [table.cell('(%s)'%name) for i in range(n_cells)]
            for c in cov:
                table.cell()
        
        # body
        table.midrule()
        for i in range(n):
            case = case_labels[i]
            if showcase:
                table.cell(case)
            for j in range(k):
                table.cell(data[j][i], fmt=fmt)
            # covariates
            indexes = match==case
            for c in cov:
                # test it's all the same values
                case_cov = c[indexes]
                if len(np.unique(case_cov.x)) != 1: 
                    msg = 'covariate for case "%s" has several values'%case
                    raise ValueError(msg)
                # get value
                first_i = np.nonzero(indexes)[0][0]
                cov_value = c[first_i]
                if _data.isfactor(c) and labels:
                    cov_value = c.cells[cov_value]
                table.cell(cov_value, fmt=fmt)
    else:
        table = textab.Table('l'*(1 + n_dependents))
        table.cell(X.name)
        [table.cell(y) for y in ynames]
        table.midrule()
        # data is now sorted: (cell_i within dependent_i)
        # sort data as (X-cell, dependent_i)
        data_sorted = []
        for i_cell in range(n_cells):
            data_sorted.append([data[i_dep*n_cells + i_cell] for i_dep in \
                               range(n_dependents)])
        # table
        for name, cell_data in zip(names, data_sorted):
            for i in range(len(cell_data[0])):
                table.cell(name)
                for dep_data in cell_data:
                    v = dep_data[i]
                    table.cell(v, fmt=fmt)
    return table