def _corr_to_table(table, Y, X, categories, levels, printXname=True, label=False, pmax=None, nan=True): r, p, n = _corr(X, Y, categories) if (pmax is None) or (p <= pmax): if nan or (not np.isnan(r)): nstars = np.sum(p <= levels) if printXname: table.cell(X.name) else: table.cell() if label: table.cell(label) table.cells(fmtxt.texstr(r) + fmtxt.Stars(nstars, of=len(levels)), p, n) else: table._my_nan_count += 1
def test(Y, X=None, against=0, match=None, sub=None, par=True, corr='Hochberg', title='{desc}'): """ One-sample tests. kwargs ------ X: perform tests separately for all categories in X. Against: can be - value - string (category in X) """ ct = celltable(Y, X, match, sub) if par: title_desc = "t-tests against %s" % against statistic_name = 't' else: raise NotImplementedError names = []; ts = []; dfs = []; ps = [] if isinstance(against, str): k = len(ct.indexes) - 1 assert against in ct.cells for id in ct.indexes: label = ct.cells[id] if against == label: baseline_id = id baseline = ct.data[id] for id in ct.indexes: if id == baseline_id: continue names.append(ct.cells[id]) if (ct.within is not False) and ct.within[id, baseline_id]: t, p = scipy.stats.ttest_rel(baseline, ct.data[id]) df = len(baseline) - 1 else: data = ct.data[id] t, p = scipy.stats.ttest_ind(baseline, data) df = len(baseline) + len(data) - 2 ts.append(t) dfs.append(df) ps.append(p) elif np.isscalar(against): k = len(ct.cells) for id in ct.indexes: label = ct.cells[id] data = ct.data[id] t, p = scipy.stats.ttest_1samp(data, against) df = len(data) - 1 names.append(label); ts.append(t); dfs.append(df); ps.append(p) if corr: ps_adjusted = mcp_adjust(ps, corr) else: ps_adjusted = np.zeros(len(ps)) stars = star(ps, out=str) # , levels=levels, trend=trend, corr=corr if len(np.unique(dfs)) == 1: df_in_header = True else: df_in_header = False table = fmtxt.Table('l' + 'r' * (3 - df_in_header + bool(corr))) table.title(title.format(desc=title_desc)) if corr: table.caption(_get_correction_caption(corr, k)) # header table.cell("Effect") if df_in_header: table.cell([statistic_name, fmtxt.texstr(dfs[0], property='_'), ], mat=True) else: table.cell(statistic_name, mat=True) table.cell('df', mat=True) table.cell('p', mat=True) if corr: table.cell(fmtxt.symbol('p', df=corr)) table.midrule() # body for name, t, mark, df, p, p_adj in zip(names, ts, stars, dfs, ps, ps_adjusted): table.cell(name) tex_stars = fmtxt.Stars(mark, of=3) tex_t = fmtxt.texstr(t, fmt='%.2f') table.cell([tex_t, tex_stars]) if not df_in_header: table.cell(df) table.cell(fmtxt.p(p)) if corr: table.cell(fmtxt.p(p_adj)) return table
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