def test_post_hoc(): np.random.seed(1) df = pd.read_csv(os.path.join(get_resource_path(), "sample_data.csv")) model = Lmer("DV ~ IV1*IV3*DV_l + (IV1|Group)", data=df, family="gaussian") model.fit( factors={"IV3": ["0.5", "1.0", "1.5"], "DV_l": ["0", "1"]}, summarize=False ) marginal, contrasts = model.post_hoc(marginal_vars="IV3", p_adjust="dunnet") assert marginal.shape[0] == 3 assert contrasts.shape[0] == 3 marginal, contrasts = model.post_hoc(marginal_vars=["IV3", "DV_l"]) assert marginal.shape[0] == 6 assert contrasts.shape[0] == 15
def test_post_hoc(): np.random.seed(1) df = pd.read_csv(os.path.join(get_resource_path(), 'sample_data.csv')) model = Lmer('DV ~ IV1*IV3*DV_l + (IV1|Group)', data=df, family='gaussian') model.fit(factors={ 'IV3': ['0.5', '1.0', '1.5'], 'DV_l': ['0', '1'] }, summarize=False) marginal, contrasts = model.post_hoc(marginal_vars='IV3', p_adjust='dunnet') assert marginal.shape[0] == 3 assert contrasts.shape[0] == 3 marginal, contrasts = model.post_hoc(marginal_vars=['IV3', 'DV_l']) assert marginal.shape[0] == 6 assert contrasts.shape[0] == 15
}, ordered=True, summarize=False, ) # Get ANOVA table # We can ignore the note in the output because # we manually specified polynomial contrasts print(model.anova()) ################################################################################ # Example 1 # ~~~~~~~~~ # Compare each level of IV3 to each other level of IV3, *within* each level of IV4. Use default Tukey HSD p-values. # Compute post-hoc tests marginal_estimates, comparisons = model.post_hoc(marginal_vars="IV3", grouping_vars="IV4") # "Cell" means of the ANOVA print(marginal_estimates) ################################################################################ # Pairwise comparisons print(comparisons) ################################################################################ # Example 2 # ~~~~~~~~~ # Compare each unique IV3,IV4 "cell mean" to every other IV3,IV4 "cell mean" and used FDR correction for multiple comparisons: # Compute post-hoc tests
#x0 = curve[:15].mean() curve = curve/x0 - 1 y_df = y_df.append(pd.DataFrame({'metric_type':metric_type, 'fb_type': fb_type, 'subj_id': 's'+str(subj_id), 'channel': ch, 'k': np.linspace(0, 1, 30), 'env': curve+0.0001, 'band': band}), ignore_index=True) from pymer4.models import Lm, Lmer from pymer4.utils import get_resource_path for b, band in enumerate(['alpha', 'beta', 'theta']): for c, ch in enumerate(CHANNELS): for m, metric_type in enumerate(['magnitude', 'n_spindles', 'duration', 'amplitude']): data = y_df.query('metric_type=="{}" & channel=="{}" & band=="{}"'.format(metric_type, ch, band)) model = Lmer('env ~ k:fb_type + (1 |subj_id)', data=data, ) model.fit(factors={'fb_type': ['FB0', 'FB250', 'FB500', 'FBMock']}) a = model.post_hoc('k', 'fb_type')[1] a['channel'] = ch a['metric_type'] = metric_type a['band'] = band a['P-val-full'] = stats.t.sf(a['T-stat'], 9) if c==0 and m==0 and b==0: all_stats_df = a.copy() else: all_stats_df = all_stats_df.append(a, ignore_index=True) print(ch, metric_type) from mne.stats import fdr_correction data = np.zeros((3, 6, 4, 32)) data_t = np.zeros((3, 6, 4, 32))
stats_df['k'] = stats_df['block_number'].apply(lambda x: unique_blocks.index(x)) stats_df['subj_id_str'] = 's' + stats_df['subj_id'].astype('str') stats_df = stats_df.query('k < 15') import seaborn as sns sns.catplot('fb_type', 'metric', kind='box', col='metric_type', sharey='col', data=stats_df.query('threshold_factor==2.75').groupby(['subj_id', 'fb_type', 'metric_type']).mean().reset_index()) stats_df = pd.read_pickle('release/data/{}.pkl'.format('channels1_bands1_splitedTrue_thresholds17')) stats_df = stats_df.query('block_number==4 | block_number==36') stats_df['block_name'] = stats_df['block_number'].apply(lambda x: 0 if x ==4 else 1) stats_df['subj_id_str'] = 's' + stats_df['subj_id'].astype('str') sns.catplot('block_name', 'metric', 'fb_type', kind='point', col='metric_type', sharey='col', data=stats_df.query('threshold_factor==2.75'), dodge=True) from pymer4.models import Lm, Lmer metric_type = 'duration' threshold_factor = 2.75 data = stats_df.query('metric_type=="{}" & threshold_factor=={}'.format(metric_type, threshold_factor)).copy() print(len(data)) data = data.replace([np.inf, -np.inf], np.nan) data.loc[:, 'metric'] = data['metric'].fillna(data['metric'].min()).values model = Lmer('metric ~ block_name:fb_type + (1 |subj_id_str)', data=data) model.fit(factors={'fb_type': ['FB0', 'FB250', 'FB500', 'FBMock']}) model.post_hoc('block_name', 'fb_type')
'magnitude', 'n_spindles', 'amplitude', 'duration' ]: data = stats_df.query( 'metric_type=="{}" & threshold_factor=={}'.format( metric_type, threshold_factor)).copy() if LOG and metric_type in ['magnitude', 'amplitude']: model = Lmer('log(metric) ~ k:fb_type + (1 |subj_id_str)', data=data) else: model = Lmer('metric ~ k:fb_type + (1 |subj_id_str)', data=data) model.fit(factors={'fb_type': ['FB0', 'FB250', 'FB500', 'FBMock']}, ordered=True, summarize=False) print('***', metric_type) res = model.post_hoc('fb_type')[1][['Contrast', 'T-stat']] res['Stat'] = res['T-stat'] res['P-value'] = stats.t.sf(res['T-stat'], 9) res['metric_type'] = metric_type res['threshold_factor'] = threshold_factor t_stat_df = t_stat_df.append(res, ignore_index=True) else: contrasts = [ 'FB0 - FBMock', 'FB250 - FBMock', 'FB500 - FBMock', 'FB0 - FB500', 'FB250 - FB500', 'FB0 - FB250' ] for threshold_factor in stats_df.threshold_factor.unique(): for metric_type in [ 'magnitude', 'n_spindles', 'amplitude', 'duration' ]: