예제 #1
0
    def perform_kappa_anova(self):

        # MIXED ANOVA  ------------------------------------------------------------------------------------------------
        print(
            "\nPerforming Group x Comparison mixed ANOVA on Cohen's Kappa values."
        )

        # Group x Intensity mixed ANOVA
        self.kappa_aov = pg.mixed_anova(dv="Kappa",
                                        within="Comparison",
                                        between="Group",
                                        subject="ID",
                                        data=self.df_kappa_long,
                                        correction=True)
        pg.print_table(self.kappa_aov)

        # POST HOC ----------------------------------------------------------------------------------------------------
        self.kappa_posthoc = pg.pairwise_ttests(dv="Kappa",
                                                subject='ID',
                                                within="Comparison",
                                                between='Group',
                                                data=self.df_kappa_long,
                                                padjust="bonf",
                                                effsize="hedges",
                                                parametric=True)
예제 #2
0
    def mixed_anova(self, verbose=True, group_tukey=True, day_tukey=True):
        '''

        :param verbose:
        :param group_tukey:
        :param day_tukey:
        :return:
        '''

        df = {'ko_ctrl': [], 'day': [], 'sum': [], 'mouse': []}

        for mouse in range(len(self.ko_mice)):
            for day in self.days:
                df['ko_ctrl'].append(0)
                df['day'].append(day)
                df['sum'].append(self.ko_sums[mouse, day])
                df['mouse'].append(mouse)

        for mouse in range(len(self.ctrl_mice)):
            for day in self.days:
                df['ko_ctrl'].append(1)
                df['day'].append(day)
                df['sum'].append(self.ctrl_sums[mouse, day])
                df['mouse'].append(mouse + 5)

        df = pd.DataFrame(df)
        results = {}
        aov = mixed_anova(data=df,
                          dv='sum',
                          between='ko_ctrl',
                          within='day',
                          subject='mouse')
        results['anova'] = aov
        if verbose:
            print('Mixed design ANOVA results')
            print(aov)

        if group_tukey:
            ko_ctrl_tukey = pairwise_tukey(data=df,
                                           dv='sum',
                                           between='ko_ctrl')
            results['ko_ctrl_tukey'] = ko_ctrl_tukey
            if verbose:
                print('PostHoc Tukey: KO vs Ctrl')
                print(ko_ctrl_tukey)

        if day_tukey:
            day_stats = []
            print('PostHov Tukey on each day')
            for day in self.days:
                print('Day %d' % day)
                stats = pairwise_tukey(data=df[df['day'] == day],
                                       dv='sum',
                                       between='ko_ctrl')
                day_stats.append(stats)
                if verbose:
                    print(stats)
            results['day_tukey'] = day_stats

        return results
예제 #3
0
def twoMixANOVA(adaptation, var):
    """ Calculates and prints 2-way Mix ANOVA results for every light intensity in a specified light-adaptation series
		Group is the between-subject factor: male wildtype (mWT), female Wildtype (fWT), male knockout(mKO), female knockout (fKO) male heterozygous (mHT), female heterozygous (fHT)
		Time is the within-subject factor: the different time points at which the ERG was recorded for the same animal (TP1, TP2, TP3)
	
	User specifies:
	First parameter (str): Adaptation condition in which the ERG was recorded (Dark-adapted ('DA'), Light-adapted ('LA'), and Mesopic-adapted ('MA'))
	Second Parameter (var): Dependent variable to calculate ('a_amp', 'b_amp','a_time', 'b_time')
	
	Returns 2-way mix ANOVA table

	"""
    df_adaptation = depvar.loc[depvar['Adaptation'] == adaptation]
    grouped = df_adaptation.groupby('Light_intensity')
    results = pd.DataFrame()
    #light= []

    for name, group in df_adaptation.groupby('Light_intensity'):
        light_df = pd.DataFrame(
            data=group, columns=group.columns
        )  #Place the tuples created with groupby into a new Dataframe
        #Results.append(name)
        aov = pg.mixed_anova(
            data=light_df,
            dv=var,
            between='Group',
            within='Timepoint',
            subject='Animal',
            correction=False
        )  # correction true/false depends on whether you have a balanced design or not
        results = results.append(aov)
        pg.print_table(aov)

    results.to_excel(savestatsto + adaptation + '_' + '_' + var + '.xlsx')
    return results
예제 #4
0
def trial_time_anova(dframe):
    return [pg.mixed_anova(data = dframe.loc[dframe.taste==taste,:],
                    dv = 'firing_rate', \
                    within = 'time_bin',
                    subject = 'trial',
                    between = 'trial_bin') \
                    for taste in dframe.taste.unique()]
    def comparison_by_group_anova(self, dependent_var):
        """Performs a Group x Comparison mixed ANOVA on the dependent variable that is passed in.
           Performs pairwise T-test comparisons for post-hoc analysis.
           Plots group means using Seaborn package.

        :argument
        -dependent_var: name of column in self.df to use as dependent variable

        :returns
        -data objects from pingouin ANOVA and posthoc objects
        """

        print("\nPerforming Group x Comparison mixed ANOVA for"
              "dependent variable {}.".format(dependent_var.capitalize()))

        aov = pg.mixed_anova(dv=dependent_var, within="COMPARISON", between="GROUP", subject="ID", data=self.df)
        pg.print_table(aov.iloc[:, 0:8])
        print()
        pg.print_table(aov.iloc[:, 9:])

        sns.pointplot(data=self.df, x='GROUP', y=dependent_var, hue='COMPARISON',
                      dodge=False, markers='o', capsize=.1, errwidth=1, palette='Set1')
        plt.title("Group x Comparison Mixed ANOVA: {}".format(dependent_var.capitalize()))

        posthoc = pg.pairwise_ttests(dv=dependent_var, within="COMPARISON", between='GROUP',
                                     subject='ID', data=self.df)
        pg.print_table(posthoc)

        return aov, posthoc
예제 #6
0
    def run_stats(self):

        self.aov = pg.mixed_anova(dv="Kappa",
                                  within="Method",
                                  between="Group",
                                  subject="ID",
                                  data=self.data_long,
                                  correction=True)

        print(self.aov)
예제 #7
0
def mixed_anova_synergy_index_z(dataframe):
    """ 3 x (3) Two-way split-plot ANOVA with between-factor condition and within-factor block.
    
    :param dataframe: Aggregated data containing Fisher-z-transformed synergy index.
    :type dataframe: pandas.DataFrame
    :return: mixed-design ANOVA results.
    :rtype: pandas.DataFrame
    """
    if dataframe['condition'].nunique() <= 1:
        raise ValueError("ERROR: Between factor has insufficient number of levels.")
        #ToDo: If there's only 1 condition, run ANOVA with one within factor instead.
    if dataframe['block'].nunique() <= 1:
        raise ValueError("ERROR: Between factor has insufficient number of levels.")
        #ToDo: If there's only 1 block, run ANOVA with one between factor instead.
    aov = pg.mixed_anova(data=dataframe, dv='dVz', within='block', subject='user', between='condition', correction=True)
    return aov
예제 #8
0
    def test_pandas(self):
        """Test pandas method.
        """
        # Test the ANOVA (Pandas)
        aov = df.anova(dv='Scores', between='Group', detailed=True)
        assert aov.equals(
            pg.anova(dv='Scores', between='Group', detailed=True, data=df))

        # Test the Welch ANOVA (Pandas)
        aov = df.welch_anova(dv='Scores', between='Group')
        assert aov.equals(pg.welch_anova(dv='Scores', between='Group',
                                         data=df))

        # Test the repeated measures ANOVA (Pandas)
        aov = df.rm_anova(dv='Scores',
                          within='Time',
                          subject='Subject',
                          detailed=True)
        assert aov.equals(
            pg.rm_anova(dv='Scores',
                        within='Time',
                        subject='Subject',
                        detailed=True,
                        data=df))

        # FDR-corrected post hocs with Hedges'g effect size
        ttests = df.pairwise_ttests(dv='Scores',
                                    within='Time',
                                    subject='Subject',
                                    padjust='fdr_bh',
                                    effsize='hedges')
        assert ttests.equals(
            pg.pairwise_ttests(dv='Scores',
                               within='Time',
                               subject='Subject',
                               padjust='fdr_bh',
                               effsize='hedges',
                               data=df))

        # Test two-way mixed ANOVA
        aov = df.mixed_anova(dv='Scores',
                             between='Group',
                             within='Time',
                             subject='Subject',
                             correction=False)
        assert aov.equals(
            pg.mixed_anova(dv='Scores',
                           between='Group',
                           within='Time',
                           subject='Subject',
                           correction=False,
                           data=df))

        # Test parwise correlations
        corrs = data.pairwise_corr(columns=['X', 'M', 'Y'], method='spearman')
        corrs2 = pg.pairwise_corr(data=data,
                                  columns=['X', 'M', 'Y'],
                                  method='spearman')
        assert corrs['r'].equals(corrs2['r'])

        # Test partial correlation
        corrs = data.partial_corr(x='X', y='Y', covar='M', method='spearman')
        corrs2 = pg.partial_corr(x='X',
                                 y='Y',
                                 covar='M',
                                 method='spearman',
                                 data=data)
        assert corrs['r'].equals(corrs2['r'])

        # Test partial correlation matrix (compare with the ppcor package)
        corrs = data.pcorr().round(3)
        np.testing.assert_array_equal(corrs.iloc[0, :].values,
                                      [1, 0.392, 0.06, -0.014, -0.149])
        # Now compare against Pingouin's own partial_corr function
        corrs = data[['X', 'Y', 'M']].pcorr()
        corrs2 = data.partial_corr(x='X', y='Y', covar='M')
        assert round(corrs.loc['X', 'Y'], 3) == corrs2.loc['pearson', 'r']

        # Test mediation analysis
        med = data.mediation_analysis(x='X', m='M', y='Y', seed=42, n_boot=500)
        np.testing.assert_array_equal(med.loc[:, 'coef'].values,
                                      [0.5610, 0.6542, 0.3961, 0.0396, 0.3565])
예제 #9
0
              ncol=2, fancybox=True, shadow=True)
err_ax.set_ylabel('Mean Errors')
err_ax.set_xlabel('')

sns.pointplot(x='Block', y='Latency mean', hue='Group', hue_order=['FKO', 'MKO', 'FWT', 'MWT'], data=HWK_short_lat, kind='point', scale=0.8, ci=68, capsize=.1, palette=[
    'g', 'g', 'b', 'b'], linestyles=['--', '-', '--', '-'], markers=['v', 'o', 'v', 'o'], errwidth=2, dodge=True, ax=lat_ax)
sns.despine()
leg_handles = lat_ax.get_legend_handles_labels()[0]
lat_ax.legend_.remove()
lat_ax.set_ylabel('Mean Latency (s)')
lat_ax.set_xlabel('Trial Block')
#%%
# Now pingouin ANOVA
# pg.friedman(data = HWK_err_gpd, dv = 'Error mean', within = 'Block', subject = 'Subject')
# pg.rm_anova(data=HWK_err_gpd, dv='Error mean', within=['Block', 'Group'],  subject='Subject', detailed = True)
pg.mixed_anova(data=HWK_short, dv='Error mean', within='Block',
               between='Group', subject='Subject')


bonf = pg.pairwise_ttests(data=HWK_short, dv='Error mean', within='Block',
                          between='Group', subject='Subject', alpha=0.05, padjust='holm', return_desc=True)

bonf

#
# Kesner indexes with mean erros per block
subjects = list(HWK_errors['Subject'].unique())
index_dict = {'Sex': [], 'Genotype': [], 'Group': [],
              'Subject': [], 'Acquisition': [], 'Retrival': []}

for ii in subjects:
    index_dict['Group'].append(HWK_errors['Group'][HWK_errors['Subject'] == ii].unique().item())
예제 #10
0
파일: 04_fig4.py 프로젝트: A-Fisk/04_ageing
marker_ph_df = pd.concat(marker_ph_dict)
marker_ph_df.set_index(["A", "B"], append=True, inplace=True)

# Q2 Does the condition affect the mean activity profile?
# Two way mixed anova of activity ~ condition*hour
# followed by post-hoc test of activity ~ condition | hour
hour_col = split_relabel.columns[2]
mean_test_dir = save_test_dir / "02_mean_wave"
if not os.path.exists(mean_test_dir):
    os.mkdir(mean_test_dir)

mean_anova = pg.mixed_anova(
    dv=dep_var,
    between=condition_col,
    within=hour_col,
    subject=anim_col,
    data=split_relabel
)
pg.print_table(mean_anova)
mean_anova_str = mean_test_dir / anova_str
mean_anova.to_csv(mean_anova_str)

mean_posthoc = prep.tukey_pairwise_ph(
    split_relabel,
    protocol_col=condition_col
)
mean_ph_str = mean_test_dir / ph_str
mean_posthoc.to_csv(mean_ph_str)

    def mixed_anova(self,
                    stat_key,
                    verbose=True,
                    group_tukey=True,
                    day_tukey=True):
        '''

        :param stat_key:
        :return:
        '''

        ko_sum_stat, ctrl_sum_stat = self.summary_stat_matrices(stat_key)

        df = {'ko_ctrl': [], 'day': [], stat_key: [], 'mouse': []}

        for m, mouse in enumerate(self.ko_mice):
            for day in self.days:
                df['ko_ctrl'].append(0)
                df['day'].append(day)
                df[stat_key].append(ko_sum_stat[m, day])
                df['mouse'].append(mouse)

        for m, mouse in enumerate(self.ctrl_mice):
            for day in self.days:
                df['ko_ctrl'].append(1)
                df['day'].append(day)
                df[stat_key].append(ctrl_sum_stat[m, day])
                df['mouse'].append(mouse)

        df = pd.DataFrame(df)
        results = {}
        aov = mixed_anova(data=df,
                          dv=stat_key,
                          between='ko_ctrl',
                          within='day',
                          subject='mouse')
        results['anova'] = aov
        if verbose:
            print('Mixed design ANOVA results')
            print(aov)

        if group_tukey:
            ko_ctrl_tukey = pairwise_tukey(data=df,
                                           dv=stat_key,
                                           between='ko_ctrl')
            results['ko_ctrl_tukey'] = ko_ctrl_tukey
            if verbose:
                print('PostHoc Tukey: KO vs Ctrl')
                print(ko_ctrl_tukey)

        if day_tukey:
            day_stats = []
            print('PostHov Tukey on each day')
            for day in self.days:
                print('Day %d' % day)
                stats = pairwise_tukey(data=df[df['day'] == day],
                                       dv=stat_key,
                                       between='ko_ctrl')
                day_stats.append(stats)
                if verbose:
                    print(stats)
            results['day_tukey'] = day_stats

        return results
## the data and group labels

data = np.loadtxt(open("TwoWayMixedANOVA_data.csv"), delimiter=",")

timepoint = ['1'] * 45 + ['2'] * 45 + ['3'] * 45
groups = ['1'] * 15 + ['2'] * 15 + ['3'] * 15
s = []
for i in range(45):
    s += [str(i)]

# # convert to pandas
df = pd.DataFrame(data=np.matrix.flatten(data, 'F'), columns=['TheData'])
df['Group'] = np.tile(groups, 3)
df['TimePoint'] = timepoint
df['Subject'] = np.tile(s, 3)

pd.set_option("display.max_rows", None, "display.max_columns", None)
df

# In[ ]:

pg.mixed_anova(data=df,
               dv='TheData',
               between='Group',
               within='TimePoint',
               subject='Subject')

# In[ ]:

sns.boxplot(data=df, hue="Group", y="TheData", x='TimePoint')
예제 #13
0
    np.r_[np.tile(np.arange(n), 3),
          np.tile(np.arange(n, n + n), 3)]
})
#%%
import seaborn as sns
sns.pointplot(data=df,
              x='Time',
              y='Scores',
              hue='Group',
              dodge=True,
              markers=['o', 's'],
              size=.1,
              errwidth=1,
              palette='colorblind')
#%%
import pingouin as pg
# Compute the two-way mixed-design ANOVA
aov = pg.mixed_anova(dv='Scores',
                     within='Time',
                     between='Group',
                     subject='Subject',
                     data=df)
# Pretty printing of ANOVA summary
pg.print_table(aov)
#%%
posthocs = pg.pairwise_ttests(dv='Scores',
                              within='Time',
                              between='Group',
                              subject='Subject',
                              data=df)
pg.print_table(posthocs)
예제 #14
0
    def test_pandas(self):
        """Test pandas method.
        """
        # Test the ANOVA (Pandas)
        aov = df.anova(dv='Scores', between='Group', detailed=True)
        assert aov.equals(
            pg.anova(dv='Scores', between='Group', detailed=True, data=df))
        aov3_ss1 = df_aov3.anova(dv='Cholesterol',
                                 between=['Sex', 'Drug'],
                                 ss_type=1)
        aov3_ss2 = df_aov3.anova(dv='Cholesterol',
                                 between=['Sex', 'Drug'],
                                 ss_type=2)
        aov3_ss2_pg = pg.anova(dv='Cholesterol',
                               between=['Sex', 'Drug'],
                               data=df_aov3,
                               ss_type=2)
        assert not aov3_ss1.equals(aov3_ss2)
        assert aov3_ss2.round(3).equals(aov3_ss2_pg.round(3))

        # Test the Welch ANOVA (Pandas)
        aov = df.welch_anova(dv='Scores', between='Group')
        assert aov.equals(pg.welch_anova(dv='Scores', between='Group',
                                         data=df))

        # Test the ANCOVA
        aov = df_anc.ancova(dv='Scores', covar='Income',
                            between='Method').round(3)
        assert (aov.equals(
            pg.ancova(data=df_anc,
                      dv='Scores',
                      covar='Income',
                      between='Method').round(3)))

        # Test the repeated measures ANOVA (Pandas)
        aov = df.rm_anova(dv='Scores',
                          within='Time',
                          subject='Subject',
                          detailed=True)
        assert (aov.equals(
            pg.rm_anova(dv='Scores',
                        within='Time',
                        subject='Subject',
                        detailed=True,
                        data=df)))

        # FDR-corrected post hocs with Hedges'g effect size
        ttests = df.pairwise_tests(dv='Scores',
                                   within='Time',
                                   subject='Subject',
                                   padjust='fdr_bh',
                                   effsize='hedges')
        assert (ttests.equals(
            pg.pairwise_tests(dv='Scores',
                              within='Time',
                              subject='Subject',
                              padjust='fdr_bh',
                              effsize='hedges',
                              data=df)))

        # Pairwise Tukey
        tukey = df.pairwise_tukey(dv='Scores', between='Group')
        assert tukey.equals(
            pg.pairwise_tukey(data=df, dv='Scores', between='Group'))

        # Test two-way mixed ANOVA
        aov = df.mixed_anova(dv='Scores',
                             between='Group',
                             within='Time',
                             subject='Subject',
                             correction=False)
        assert (aov.equals(
            pg.mixed_anova(dv='Scores',
                           between='Group',
                           within='Time',
                           subject='Subject',
                           correction=False,
                           data=df)))

        # Test parwise correlations
        corrs = data.pairwise_corr(columns=['X', 'M', 'Y'], method='spearman')
        corrs2 = pg.pairwise_corr(data=data,
                                  columns=['X', 'M', 'Y'],
                                  method='spearman')
        assert corrs['r'].equals(corrs2['r'])

        # Test partial correlation
        corrs = data.partial_corr(x='X', y='Y', covar='M', method='spearman')
        corrs2 = pg.partial_corr(x='X',
                                 y='Y',
                                 covar='M',
                                 method='spearman',
                                 data=data)
        assert corrs['r'].equals(corrs2['r'])

        # Test partial correlation matrix (compare with the ppcor package)
        corrs = data.iloc[:, :5].pcorr().round(3)
        np.testing.assert_array_equal(corrs.iloc[0, :].to_numpy(),
                                      [1, 0.392, 0.06, -0.014, -0.149])
        # Now compare against Pingouin's own partial_corr function
        corrs = data[['X', 'Y', 'M']].pcorr()
        corrs2 = data.partial_corr(x='X', y='Y', covar='M')
        assert np.isclose(corrs.at['X', 'Y'], corrs2.at['pearson', 'r'])

        # Test rcorr (correlation matrix with p-values)
        # We compare against Pingouin pairwise_corr function
        corrs = df_corr.rcorr(padjust='holm', decimals=4)
        corrs2 = df_corr.pairwise_corr(padjust='holm').round(4)
        assert corrs.at['Neuroticism', 'Agreeableness'] == '*'
        assert (corrs.at['Agreeableness',
                         'Neuroticism'] == str(corrs2.at[2, 'r']))
        corrs = df_corr.rcorr(padjust='holm', stars=False, decimals=4)
        assert (corrs.at['Neuroticism',
                         'Agreeableness'] == str(corrs2.at[2,
                                                           'p-corr'].round(4)))
        corrs = df_corr.rcorr(upper='n', decimals=5)
        corrs2 = df_corr.pairwise_corr().round(5)
        assert corrs.at['Extraversion', 'Openness'] == corrs2.at[4, 'n']
        assert corrs.at['Openness', 'Extraversion'] == str(corrs2.at[4, 'r'])
        # Method = spearman does not work with Python 3.5 on Travis?
        # Instead it seems to return the Pearson correlation!
        df_corr.rcorr(method='spearman')
        df_corr.rcorr()

        # Test mediation analysis
        med = data.mediation_analysis(x='X', m='M', y='Y', seed=42, n_boot=500)
        np.testing.assert_array_equal(med.loc[:, 'coef'].round(4).to_numpy(),
                                      [0.5610, 0.6542, 0.3961, 0.0396, 0.3565])
예제 #15
0
    res['cpg'].append(cpg)

    raw_genes = ann.loc[cpg, 'UCSC_RefGene_Name']
    genes = ''
    if isinstance(raw_genes, str):
        if raw_genes != '':
            genes = ';'.join(list(set(raw_genes.split(';'))))
    res['genes'].append(genes)

    dep_bef = betas_bef.loc[cpg, :].values.tolist()
    dep_aft = betas_aft.loc[cpg, :].values.tolist()

    anova_df.loc[:, 'betas'] = dep_bef + dep_aft
    aov = pg.mixed_anova(dv='betas',
                         within='time',
                         between='group',
                         subject='subject',
                         data=anova_df)
    res['mixed_anova_pval'].append(aov.loc[aov['Source'] == 'Interaction',
                                           'p-unc'].values[0])

    kw_df.loc[:, 'betas_bef'] = list(dep_bef)
    kw_df.loc[:, 'betas_aft'] = list(dep_aft)
    kw_df.loc[:, 'betas_diff'] = list(map(sub, dep_aft, dep_bef))
    vals_bef_ctrl = kw_df.loc[kw_df['group'] == 'no', 'betas_bef'].values
    vals_aft_ctrl = kw_df.loc[kw_df['group'] == 'no', 'betas_aft'].values
    vals_bef_case = kw_df.loc[kw_df['group'] == 'yes', 'betas_bef'].values
    vals_aft_case = kw_df.loc[kw_df['group'] == 'yes', 'betas_aft'].values
    res['mean_bef_ctrl'].append(np.mean(vals_bef_ctrl))
    res['mean_aft_ctrl'].append(np.mean(vals_aft_ctrl))
    res['mean_bef_case'].append(np.mean(vals_bef_case))
예제 #16
0
sns.set()
fig_name = f'{band}.png'
fig, ax = plt.subplots(1, 1, figsize=(12, 10))
sns.pointplot(data=df_tfr,
              x='Condition',
              y='Power',
              hue='Group',
              dodge=True,
              markers=['o', 's'],
              capsize=1,
              errwidth=1,
              palette='colorblind',
              ax=ax)
ax.set_title(f'Mixed ANOVA for {band} {freq[band]}')
filename = f'/Users/senthilp/Desktop/{band}_Mixed_ANOVA'
plt.savefig(filename, dpi=300)
sd = df_tfr.groupby(['Condition', 'Group'])['Power'].agg(['mean',
                                                          'std']).round(2)

aov = pg.mixed_anova(dv='Power',
                     within='Condition',
                     between='Group',
                     subject='Electrode',
                     data=df_tfr)

posthocs = pg.pairwise_ttests(dv='Power',
                              within='Condition',
                              between='Group',
                              subject='Electrode',
                              data=df_tfr)
예제 #17
0
                                dv=x_var,
                                within=y_var,
                                subject=subject_var,
                                boxplot_in_front=True,
                                ax=ax1,
                                order=groups_selection)
            st.pyplot(fig)
        except:
            st.error("Please specify at least one within level!")

    else:
        st.success("Repeated measures mixed ANOVA results")
        st.write(
            pg.mixed_anova(dv=x_var,
                           within=y_var,
                           subject=subject_var,
                           between=y_var2,
                           data=df))
        st.success("Post-hoc tests results")
        st.write(
            pg.pairwise_ttests(dv=x_var,
                               within=y_var,
                               subject=subject_var,
                               between=y_var2,
                               data=df))
        st.success("Plots are being generated")
        fig = plt.figure(figsize=(12, 6))

        try:
            ax = sns.pointplot(data=df,
                               x=y_var,
    def group_by_intensity_anova(self, model_comparison, data_type="percent", use_normed=False):
        """Performs a Group x Intensity mixed ANOVA on the dependent variable that is passed in.
           Performs pairwise T-test comparisons for post-hoc analysis.
           Plots group means using Seaborn package.

        :argument
        -model_comparison: name of column in self.df to use as dependent variable
        -data_types: 'minutes' or 'percent'; type of data to use
        -use_norm: whether or not to use normed data

        :returns
        -data objects from pingouin ANOVA and posthoc objects
        """

        # DATA FORMATTING ---------------------------------------------------------------------------------------------
        if use_normed:
            df = self.norm_df
        if not use_normed:
            df = self.df

        # Pulls rows from self.df for desired model comparison
        comp_names = ["Wrist-Ankle", "Wrist-HR", "Wrist-HRAcc", "Ankle-HR", "Ankle-HRAcc", "HR-HRAcc"]
        row_int = comp_names.index(model_comparison)
        df2 = df.iloc[0::6]

        # df for minutes data
        mins_df = df2[["SEDENTARY", "LIGHT", "MODERATE", "VIGOROUS"]]

        # df for % data
        perc_df = df2[["SEDENTARY%", "LIGHT%", "MODERATE%", "VIGOROUS%"]]

        # Sets df to correct data type
        if data_type == "percent":
            df = perc_df
        if data_type == "minutes":
            df = mins_df

        df["ID"] = self.high_active_ids + self.low_active_ids

        # Creates column in df of IDs
        df_long = pd.melt(frame=df, id_vars="ID", var_name="INTENSITY", value_name="VALUE")

        high_list = ["HIGH" for i in range(5)]
        low_list = ["LOW" for i in range(5)]
        group_list = high_list + low_list
        df_long["GROUP"] = (group_list * 4)

        print(df_long)

        # DATA VISUALIZATION -----------------------------------------------------------------------------------------

        # Creates 2x1 subplots of group means
        plt.subplots(1, 2, figsize=(12, 7))
        plt.subplots_adjust(wspace=0.20)
        plt.suptitle("Group x Intensity Mixed ANOVA: {} "
                     "(normalized={})".format(model_comparison.capitalize(), use_normed))

        # Two activity level groups: one line for each intensity
        plt.subplot(1, 2, 1)
        sns.pointplot(data=df_long, x="GROUP", y="VALUE", hue="INTENSITY",
                      dodge=False, markers='o', capsize=.1, errwidth=1, palette='Set1')
        plt.ylabel("Difference ({})".format(data_type))
        plt.axhline(y=0, linestyle="dashed", color='black')

        # Four intensity groups: one line for each activity level group
        plt.subplot(1, 2, 2)
        sns.pointplot(data=df_long, x="INTENSITY", y="VALUE", hue="GROUP",
                      dodge=False, markers='o', capsize=.1, errwidth=1, palette='Set1')
        plt.ylabel("")
        plt.axhline(y=0, linestyle="dashed", color='black')

        # STATISTICAL ANALYSIS ---------------------------------------------------------------------------------------
        print("\nPerforming Group x Comparison mixed ANOVA using {} data "
              "for the {} model.".format(data_type, model_comparison))

        # Group x Intensity mixed ANOVA
        aov = pg.mixed_anova(dv="VALUE", within="INTENSITY", between="GROUP", subject="ID", data=df_long)
        pg.print_table(aov.iloc[:, 0:8])
        pg.print_table(aov.iloc[:, 9:])

        posthoc = pg.pairwise_ttests(dv="VALUE", within="INTENSITY", between='GROUP', subject='ID', data=df_long)
        pg.print_table(posthoc)

        return aov, posthoc
예제 #19
0
	def analyse(self, parameter_list={"all"}, between_factor_list=["Subject_type"], within_factor_list=["Stimuli_type"], statistical_test="Mixed_anova", file_creation=True, ttest_type=1):
		"""This function carries out the required statistical analysis.

		 The analysis is carried out on the specified indicators/parameters using the data extracted from all the subjects that were mentioned in the json file. There are 4 different tests that can be run, namely - Mixed ANOVA, Repeated Measures ANOVA, T Test and Simple ANOVA (both 1 and 2 way)

		Parameters
		----------
		parameter_list: set (optional)
			Set of the different indicators/parameters (Pupil_size, Blink_rate) on which statistical analysis is to be performed, by default it will be "all" so that all the parameter are considered.
		between_factor_list: list(str) (optional)
			List of between group factors, by default it will only contain "Subject_type".
			If any additional parameter (eg: Gender) needs to be considered, then the list will be: between_factor_list = ["Subject_type", "Gender"].
			DO NOT FORGET TO INCLUDE "Subject_type", if you wish to consider "Subject_type" as a between group factor.
			Eg: between_factor_list = ["factor_x"] will no longer consider "Subject_type" as a factor.
			Please go through the README FILE to understand how the JSON FILE is to be written for between group factors to be considered.
		within_factor_list: list(str) (optional)
			List of within group factors, by default it will only contain "Stimuli_type"
			If any additional parameter, needs to be considered, then the list will be: between_factor_list = ["Subject_type", "factor_X"].
			DO NOT FORGET TO INCLUDE "Stimuli_type", if you wish to consider "Stimuli_type" as a within group factor.
			Eg: within_factor_list = ["factor_x"] will no longer consider "Stimuli_type" as a factor.
			Please go through how the README FILE to understand how the JSON FILE is to be written for within group factors to be considered.
		statistical_test: str {"Mixed_anova","RM_anova","ttest","anova","None"} (optional)
			Name of the statistical test that has to be performed.
				NOTE:

				- ttest: There are 3 options for ttest, and your choice of factors must comply with one of those options, for more information, please see description of `ttest_type` variable given below.
				- Welch_ttest: There are 2 options for Welch Ttest, and your choice of factors must comply with one of those options, for more information, please see description of `ttest_type` variable given below.
				- Mixed_anova: Only 1 between group factor and 1 within group factor can be considered at any point of time
				- anova: Any number of between group factors can be considered for analysis
				
				- RM_anova: Upto 2 within group factors can be considered at any point of time
		file_creation: bool (optional)
			Indicates whether a csv file containing the statistical results should be created.
				NOTE:
				The name of the csv file created will be by the name of the statistical test that has been chosen.
				A directory called "Results" will be created within the Directory whose path is mentioned in the json file and the csv files will be stored within "Results" directory.
				If any previous file by the same name exists, it will be overwritten.
		ttest_type: int {1,2,3} (optional)
			Indicates what type of parameters will be considered for the ttest and Welch Ttest
				NOTE:
				For ttest-

				- 1: Upto 2 between group factors will be considered for ttest
				- 2: 1 within group factor will be considered for ttest
				
				- 3: 1 within group and 1 between group factor will be considered for ttest

				For Welch ttest-

				- 1: Will consider the first factor in 'between_factor_list'

				- 2: Will consider the first factor in 'within_factor_list' 

		Examples
		--------

		For calculating Mixed ANOVA, on all the parameters, with standardisation, NOT averaging across stimuli of the same type
		and considering Subject_type and Stimuli_type as between and within group factors respectively

		>>> analyse(self, standardise_flag=False, average_flag=False, parameter_list={"all"}, between_factor_list=["Subject_type"], within_factor_list=["Stimuli_type"], statistical_test="Mixed_anova", file_creation = True)
		OR
		>>> analyse(self, standardise_flag=True) (as many of the option are present by default)

		For calculating 2-way ANOVA, for "blink_rate" and "avg_blink_duration", without standardisation with averaging across stimuli of the same type
		and considering Subject_type and Gender as the between group factors while NOT creating a new csv file with the results

		>>> analyse(self, average_flag=True, parameter_list={"blink_rate", "avg_blink_duration"}, between_factor_list=["Subject_type", "Gender"], statistical_test="anova", file_creation = False)

		"""

		with open(self.json_file, "r") as json_f:
			json_data = json.load(json_f)

		csvFile = None
		if file_creation:
			directory_path = json_data["Path"] + "/Results"
			if not os.path.isdir(directory_path):
				os.mkdir(directory_path)

			if not os.path.isdir(directory_path + '/Data/'):
				os.mkdir(directory_path + '/Data/')

			if statistical_test != None:
				file_path = directory_path + "/" + statistical_test + ".csv"
				csvFile = open(file_path, 'w')
				writer = csv.writer(csvFile)


		meta_not_to_be_considered = ["pupil_size", "pupil_size_downsample"]

		sacc_flag=0
		ms_flag=0

		for sen in self.sensors:
			for meta in Sensor.meta_cols[sen]:
				if meta in meta_not_to_be_considered:
					continue

				if ('all' not in parameter_list) and (meta not in parameter_list):
					continue

				print("\n\n")
				print("\t\t\t\tAnalysis for ",meta)

				#For the purpose of statistical analysis, a pandas dataframe needs to be created that can be fed into the statistical functions
				#The columns required are - meta (indicator), the between factors (eg: Subject type or Gender), the within group factor (eg: Stimuli Type), Subject name/id

				#Defining the list of columns required for the statistical analysis
				column_list = [meta]

				column_list.extend(between_factor_list)
				column_list.extend(within_factor_list)
				column_list.append("subject")
				column_list.append("stimuli_name")

				data =  pd.DataFrame(columns=column_list)

				#For each subject
				for sub_index, sub in enumerate(self.subjects):
					#For each Question Type
					for stimuli_index, stimuli_type in enumerate(sub.aggregate_meta):

						if meta in ["sacc_duration", "sacc_vel", "sacc_amplitude", "ms_duration", "ms_vel", "ms_amplitude"]:
							summation_array = self.summationArrayCalculation(meta, sub_index, stimuli_index)
						
						value_array = self.meta_matrix_dict[1][meta][sub_index,stimuli_index]

						index_extra = 0

						for value_index, _ in enumerate(value_array):

							if meta in ["sacc_duration", "sacc_vel", "sacc_amplitude", "ms_duration", "ms_vel", "ms_amplitude"]:

								if value_array[value_index] == 0:
									index_extra += 1
									continue

								proper_index = self.return_index(value_index-index_extra, summation_array)
								stimulus_name = self.stimuli[stimuli_type][proper_index]
							else:
								stimulus_name = self.stimuli[stimuli_type][value_index]

							row = []
							row.append(value_array[value_index])

							#Add the between group factors (need to be defined in the json file)
							for param in between_factor_list:

								if param == "Subject_type":
									row.append(sub.subj_type)
									continue

								try:
									row.append(json_data["Subjects"][sub.subj_type][sub.name][param])
								except:
									print("Between subject paramter: ", param, " not defined in the json file")

							for param in within_factor_list:

								if param == "Stimuli_type":
									row.append(stimuli_type)
									continue

								try:
									stimulus_name = self.stimuli[stimuli_type][value_index]
									row.append(json_data["Stimuli"][stimuli_type][stimulus_name][param])
								except:
									print("Within stimuli parameter: ", param, " not defined in the json file")

							row.append(sub.name)
							row.append(stimulus_name)

							if np.isnan(value_array[value_index]):
								print("The data being read for analysis contains null value: ", row)

							#Instantiate into the pandas dataframe
							data.loc[len(data)] = row

				data.to_csv(directory_path + '/Data/' + meta + "_data.csv")

				#print(data)

				#Depending on the parameter, choose the statistical test to be done
				if statistical_test == "Mixed_anova":

					if len(within_factor_list)>1:
						print("Error: Too many within group factors,\nMixed ANOVA can only accept 1 within group factor\n")
					elif len(between_factor_list)>1:
						print("Error: Too many between group factors,\nMixed ANOVA can only accept 1 between group factor\n")

					print(meta, ":\tMixed ANOVA")
					aov = pg.mixed_anova(dv=meta, within=within_factor_list[0], between=between_factor_list[0], subject='subject', data=data)
					pg.print_table(aov)

					if file_creation:

						values_list = ["Mixed Anova: "]
						values_list.append(meta)
						self.fileWriting(writer, csvFile, aov, values_list)

					posthocs = pg.pairwise_ttests(dv=meta, within=within_factor_list[0], between=between_factor_list[0], subject='subject', data=data)
					pg.print_table(posthocs)

					if file_creation:

						values_list = ["Post Hoc Analysis"]
						self.fileWriting(writer, csvFile, posthocs, values_list)

				elif statistical_test == "RM_anova":

					if len(within_factor_list)>2 or len(within_factor_list)<1:
						print("Error: Too many or too few within group factors,\nRepeated Measures ANOVA can only accept 1 or 2 within group factors\n")

					print(meta, ":\tRM ANOVA")
					aov = pg.rm_anova(dv=meta, within= within_factor_list, subject = 'subject', data=data)
					pg.print_table(aov)

					if file_creation:

						values_list = ["Repeated Measures Anova: "]
						values_list.append(meta)
						self.fileWriting(writer, csvFile, aov, values_list)

				elif statistical_test == "anova":

					print(meta, ":\tANOVA")
					length = len(between_factor_list)
					model_equation = meta + " ~ C("

					for factor_index, _ in enumerate(between_factor_list):
						if(factor_index<length-1):
							model_equation = model_equation + between_factor_list[factor_index] + ")*C("
						else:
							model_equation = model_equation + between_factor_list[factor_index] + ")"

					print("Including interaction effect")
					print(model_equation)
					model = ols(model_equation, data).fit()
					res = sm.stats.anova_lm(model, typ= 2)
					print(res)

					if file_creation:

						values_list = ["Anova including interaction effect: "]
						values_list.append(meta)
						self.fileWriting(writer, csvFile, res, values_list)

					print("\nExcluding interaction effect")
					model_equation = model_equation.replace("*", "+")
					print(model_equation)
					model = ols(model_equation, data).fit()
					res = sm.stats.anova_lm(model, typ= 2)
					print(res)

					if file_creation:

						values_list = ["Anova excluding interaction effect: "]
						values_list.append(meta)
						self.fileWriting(writer, csvFile, res, values_list)

				elif statistical_test == "ttest":

					print(meta, ":\tt test")

					if ttest_type==1:
						aov = pg.pairwise_ttests(dv=meta, between=between_factor_list, subject='subject', data=data)
						pg.print_table(aov)
					elif ttest_type==2:
						aov = pg.pairwise_ttests(dv=meta, within=within_factor_list, subject='subject', data=data)
						pg.print_table(aov)
					elif ttest_type==3:
						aov = pg.pairwise_ttests(dv=meta, between=between_factor_list, within=within_factor_list, subject='subject', data=data)
						pg.print_table(aov)
					else:
						print("The value given to ttest_type is not acceptable, it must be either 1 or 2 or 3")


					if file_creation:

						values_list = ["Pairwise ttest: "]
						values_list.append(meta)
						self.fileWriting(writer, csvFile, aov, values_list)

				elif statistical_test == "welch_ttest":

					print(meta, ":\tWelch t test")

					if ttest_type==1:
						normality,aov = self.welch_ttest(dv=meta, factor=between_factor_list[0], subject='subject', data=data)
						pg.print_table(normality)
						pg.print_table(aov)
					elif ttest_type==2:
						normality,aov = self.welch_ttest(dv=meta, factor=within_factor_list[0], subject='subject', data=data)
						pg.print_table(normality)
						pg.print_table(aov)
					else:
						print("The value given to ttest_type for welch test is not acceptable, it must be either 1 or 2")

					if file_creation:

						values_list = ["Welch Pairwise ttest: "]
						values_list.append(meta)
						self.fileWriting(writer, csvFile, normality, values_list)
						self.fileWriting(writer, csvFile, aov, values_list)


		if csvFile != None:
			csvFile.close()
예제 #20
0
def efficiency(data):
    # out paths
    func_name = sys._getframe().f_code.co_name
    out_prefix = func_name + "_"
    out_csv = config.OUT_EVALS_DIR + "/" + out_prefix
    out_png = config.OUT_PLOT_DIR + "/" + out_prefix

    plot_list = []
    norm_time_dict = {}
    var = "time"
    for c in config.CalcByType:
        # box plots
        plot_list.append(
            create_plot(data, c, var, plots.saveBoxPlot, out_png + "box"))
        # statistics + out
        norm_time_dict[c.name] = create_stat(data, c, var, shapiro, out_csv,
                                             config.OUT_NORM_FILE)
        # qq plots
        plot_list.append(
            create_plot(data, c, var, plots.saveQQPlot, out_png + "qq"))

    # var by no calctype
    norm_time_dict["None"] = create_stat(data, None, var, shapiro, out_csv,
                                         config.OUT_NORM_FILE)
    plot_list.append(
        create_plot(data, None, var, plots.saveQQPlot, out_png + "qq"))

    # ONE WAY ANOVA w repeated measurements
    out_one_way_anova = out_csv + var + "_" + config.OUT_ONE_WAY_ANOVA_FILE + "." + config.OUT_CSV_EXT
    data_log = data.deep_copy()
    data_log[var] = np.log10(data_log[var])
    # Remove outliers
    q = data_log['time'].quantile(0.96)
    data_log = data_log[data_log["time"] < q]
    one_way_anova_aov = pg.rm_anova(dv=var,
                                    data=data_log,
                                    subject='user',
                                    within='video',
                                    detailed=True)
    one_way_anova_aov.to_csv(out_one_way_anova, index=False)

    # Pairwise T-test
    out_ttest = out_csv + var + "_" + config.OUT_TTEST_FILE + "." + config.OUT_CSV_EXT
    ttest_result = pg.pairwise_ttests(dv=var,
                                      within='video',
                                      subject='user',
                                      data=data_log,
                                      padjust='bonferroni',
                                      effsize='hedges',
                                      tail='one-sided',
                                      return_desc=True)
    ttest_result.to_csv(out_ttest, index=False)

    # MIXED_ANOVA
    out_mixed_anova = out_csv + var + "_" + config.OUT_MIXED_ANOVA_FILE + "." + config.OUT_CSV_EXT
    m_anova = pg.mixed_anova(dv=var,
                             within='video',
                             between='tool',
                             subject='user',
                             data=data.df)
    m_anova.to_csv(out_mixed_anova, index=False)

    # Friedmann/Kruskal and Dunn
    types = [config.CalcByType.VIDEO, config.CalcByType.TOOL]
    tests = [friedmanchisquare, kruskal]
    pfx = [config.OUT_FRIEDMAN_FILE, config.OUT_KRUSKAL_FILE]
    stat_dict = {}
    for i in range(len(types)):
        res, plt = create_var_stats(data, [var], types[i], tests[i],
                                    out_prefix + pfx[i], False)
        plot_list += plt
        stat_dict[types[i].name] = res

    return {
        "success": True,
        "message": {
            'norm': str(norm_time_dict),
            'stats': str(stat_dict),
            'one_way_anova': str(one_way_anova_aov),
            'plots': str(plot_list)
        }
    }
예제 #21
0
#%% loading
input_data_file = 'alcohol_depth5_cleaned.csv'
dane = pd.read_csv(input_data_file)

#%% cleanup. More than one value per group needed for calculating Standard Deviation
counts = dane[['group_label', 'abbrev',
               'signal_density']].groupby(['group_label',
                                           'abbrev']).count().reset_index()
dane = dane[dane.abbrev.isin(
    counts[counts['signal_density'] > 1]['abbrev'].to_list())]
print('brain structures left: ' + str(len(pd.unique(dane['abbrev']))))

#%% anova to check whether significant differences exist
aov = pg.mixed_anova(data=dane,
                     dv='signal_density',
                     between='group_label',
                     within='abbrev',
                     subject='case_id',
                     correction=True)
print(aov[['Source', 'DF1', 'DF2', 'p-unc']])

#%% pairwise t tests
#see https://pingouin-stats.org/generated/pingouin.pairwise_ttests.html#pingouin.pairwise_ttests
post_hocs = pg.pairwise_ttests(data=dane,
                               dv='signal_density',
                               within='abbrev',
                               between='group_label',
                               subject='case_id',
                               parametric=True,
                               marginal=True,
                               tail='two-sided',
                               padjust='fdr_bh',
예제 #22
0
파일: 04_fig4.py 프로젝트: A-Fisk/03_lleeg
# Q1 Does power of the spectrum change between days
# Repeated two way ANOVA of Power ~ Freq*days | anim

stats_spec_df = np.log10(nrem_mean)
stats_spec_df.index = stats_spec_df.index.droplevel(2)
stats_spec_df = stats_spec_df.stack().reset_index()

anim_col = stats_spec_df.columns[0]
day_col = stats_spec_df.columns[1]
freq_col = stats_spec_df.columns[2]
power_col = stats_spec_df.columns[3]

spec_rm = pg.mixed_anova(dv=power_col,
                         within=day_col,
                         between=freq_col,
                         subject=anim_col,
                         data=stats_spec_df)
pg.print_table(spec_rm)

spec_name = save_test_dir / "01_spec_anova.csv"
spec_rm.to_csv(spec_name)

# Q2 Does the Number of episodes change between day?
# Rpeated two way anova of Count ~ Time*day | anim

count_stats_df = long_frag.copy()

anim_col = count_stats_df.columns[0]
time_col = count_stats_df.columns[1]
day_col = count_stats_df.columns[2]
'Pca100_ridge','Pca300_ridge','Pca300_cca30']
subjects_ = [np.tile(el,pnts4subj) for el in subjects]
subjects_= np.concatenate(subjects_)
subjects_=subjects_.tolist()*len(models)
models_=[np.tile(el, pnts4subj*len(subjects)) for el in models]
models_=np.concatenate(models_)
model_types_=[np.tile(el,pnts4subj*len(subjects)) for el in model_types]
model_types_=np.concatenate(model_types_)
data= {'model':models_, 'model_type':model_types_, 'subject':subjects_,'data':data}
df = pd.DataFrame.from_dict(data)
df.to_csv('/data/akitaitsev/decoding_model_bids/decoding_data/statistics/df_long_cor_spectr.csv',\
    index=False)

# violin plot
fig, ax = plt.subplots(figsize=(16,9))
sea.violinplot(ax=ax, x='model',y='data', hue='model_type', kind='violin',inner='quartile',hue_order=['SM','STM'],data=df)
plt.show()
fig.savefig('/data/akitaitsev/decoding_model_bids/decoding_data/violinplots_spectr.png', dpi=300)

### statistical analyis
aov=pg.mixed_anova(dv='data', between='model_type',within='model',subject='subject', data=df)
aov.to_csv('/data/akitaitsev/decoding_model_bids/decoding_data/statistics/anova_rep_measures_spectr.csv')
print(aov)

pg.print_table(aov)
model=ols('data~C(model)+C(model_type)+C(subject)', data=df).fit()
anova=sm.stats.anova_lm(model, typ=2)
anova.to_csv('/data/akitaitsev/decoding_model_bids/decoding_data/statistics/anova_3way_spect.csv')
print(anova)

예제 #24
0
    def perform_activity_anova(self, activity_intensity, data_type="percent"):

        if data_type == "percent":
            df = self.df_percent
            activity_intensity = activity_intensity + "%"
        if data_type == "minutes":
            df = self.df_mins

        # PLOTTING ---------------------------------------------------------------------------------------------------
        # Creates 2x1 subplots of group means
        plt.title("Group x Model Mixed ANOVA: {} Activity".format(
            activity_intensity))

        # Two activity level groups: one line for each intensity
        sns.pointplot(data=df,
                      x="Group",
                      y=activity_intensity,
                      hue="Model",
                      ci=95,
                      dodge=False,
                      markers='o',
                      capsize=.1,
                      errwidth=1,
                      palette='Set1')
        plt.ylabel("{}".format(data_type.capitalize()))

        # STATISTICAL ANALYSIS ---------------------------------------------------------------------------------------
        print("\nPerforming Group x Model mixed ANOVA on {} activity.".format(
            activity_intensity))

        # Group x Intensity mixed ANOVA
        self.aov = pg.mixed_anova(dv=activity_intensity,
                                  within="Model",
                                  between="Group",
                                  subject="ID",
                                  data=df,
                                  correction='auto')
        pg.print_table(self.aov)

        group_p = self.aov.loc[self.aov["Source"] == "Group"]["p-unc"]
        group_sig = group_p.values[0] <= 0.05

        model_p = self.aov.loc[self.aov["Source"] == "Model"]["p-unc"]
        model_sig = model_p.values[0] <= 0.05

        interaction_p = self.aov.loc[self.aov["Source"] ==
                                     "Interaction"]["p-unc"]
        interaction_sig = interaction_p.values[0] <= 0.05

        print("ANOVA quick summary:")
        if model_sig:
            print("-Main effect of Model (p = {})".format(
                round(model_p.values[0], 3)))
        if not model_sig:
            print("-No main effect of Model")
        if group_sig:
            print("-Main effect of Group (p = {})".format(
                round(group_p.values[0], 3)))
        if not group_sig:
            print("-No main effect of Group")
        if interaction_sig:
            print("-Signficiant Group x Model interaction (p = {})".format(
                round(interaction_p.values[0], 3)))
        if not interaction_sig:
            print("-No Group x Model interaction")

        posthoc_para = pg.pairwise_ttests(dv=activity_intensity,
                                          subject='ID',
                                          within="Model",
                                          between='Group',
                                          data=df,
                                          padjust="bonf",
                                          effsize="hedges",
                                          parametric=True)
        posthoc_nonpara = pg.pairwise_ttests(dv=activity_intensity,
                                             subject='ID',
                                             within="Model",
                                             between='Group',
                                             data=df,
                                             padjust="bonf",
                                             effsize="hedges",
                                             parametric=False)

        self.posthoc_para = posthoc_para
        self.posthoc_nonpara = posthoc_nonpara

        pg.print_table(posthoc_para)
예제 #25
0
# Plot variables
plot_number = 1  # Start with first plot
plot_colors = [['#ff4f42', '#ff1100', '#ffb0ab'],
               ['#5980ff', '#0039f5', '#a1b7ff'],
               ['#666666', '#333333', '#999999'],
               ['#62d96d', '#009c0f', '#94fc8b']]  # Red, blue, grey, green
plot_markers = ['o', 's', '^']  #DE, NDE, OU

plt.hist(x=dataFrame.slope)
#plt.show()

if withImage_anova == 'yes':
    # 2-way ANOVA
    aov = pg.mixed_anova(dv='slope',
                         between='group',
                         within='eye',
                         subject='subject',
                         data=dataFrame)
    aov.round(3)
    aov

    # Bonferroni correction
    pvals = [aov['p-unc'][0], aov['p-unc'][1], aov['p-unc'][2]]
    reject, pvals_corr = pg.multicomp(pvals, method='fdr_bh')
    print(reject, pvals_corr)

    for sub in subjects:

        data = cs_data.loc[cs_data.id == sub]

        y, slope, eye = getRegressionCoeff(data)
예제 #26
0
# we put all the feature names in uppercase for clarity on the figure
data.columns = data.columns.str.upper()

#%%
### STATISTICAL TESTS ###

# For subplots A and B we perform a Mixed-design ANOVA to look at the interaction
# between a within-subject factor (here E:T ratio) for which all the donors
# have been tested and should be considered related (repeated measures ANOVA)
# and a 'between-subject' factor (here the FCGR3A allotype)

# !pip install openpyxl
MixedANOVA_top = mixed_anova(
    data=data_tx,
    dv='top',  # dependent variable
    between='FCGR3A',  # between-subject identifier
    within='E:T',  # within-subject identifier
    subject='donor')  # subject identifier
MixedANOVA_top.to_excel('../stats/MixedANOVA_top_figure2.xlsx')

MixedANOVA_top_posthoc = pairwise_ttests(data=data_tx,
                                         padjust='fdr_bh',
                                         dv='top',
                                         between='FCGR3A',
                                         within='E:T',
                                         subject='donor')
MixedANOVA_top_posthoc.to_excel('../stats/MixedANOVA_top_posthoc_figure2.xlsx')

MixedANOVA_ec50 = mixed_anova(
    data=data_tx,
    dv='EC50',  # dependent variable
    statDF = statDF[statDF['participantsType'] == 'Human']
    # statDF = statDF[statDF['participantsType'] == 'RL Agent']

    crosstab, res = researchpy.crosstab(dfExpTrail['hasAvoidPoint'],
                                        dfExpTrail['decisionSteps'],
                                        test="chi-square")

    print(crosstab)

    # Compute the two-way mixed-design ANOVA
    calAnova = 0
    if calAnova:
        import pingouin as pg
        aov = pg.mixed_anova(dv='ShowCommitmentPercent',
                             within='decisionSteps',
                             between='participantsType',
                             subject='name',
                             data=statDF)
        pg.print_table(aov)

        posthocs = pg.pairwise_ttests(dv='ShowCommitmentPercent',
                                      within='decisionSteps',
                                      between='participantsType',
                                      subject='name',
                                      data=statDF,
                                      within_first=0)
        pg.print_table(posthocs)

    VIZ = 0
    if VIZ:
        import seaborn as sns
예제 #28
0
stats_df_all = pd.read_pickle('data/{}'.format(stats_file))
# stats_df = stats_df.loc[stats_df.subj_id!=28]
stats_df_all = stats_df_all.loc[stats_df_all['block_number'].isin(
    [BASELINE_AFTER, BASELINE_BEFORE])]
unique_blocks = list(stats_df_all['block_number'].unique())
stats_df_all = stats_df_all.loc[stats_df_all['threshold_factor'] == threshold]
stats_df_all['baseline'] = stats_df_all['block_number'].apply(
    lambda x: 'After' if x > 10 else 'Before')

fb_types = ['FB0', 'FB250', 'FB500', 'FBMock']
stats_df_all = stats_df_all.loc[stats_df_all['fb_type'].isin(fb_types)]

metric_type = 'n_spindles'
res = mixed_anova(stats_df_all.query('metric_type=="{}"'.format(metric_type)),
                  dv='metric',
                  within='baseline',
                  subject='subj_id',
                  between='fb_type')

res2 = pairwise_ttests(stats_df_all.query(
    'metric_type=="{}"'.format(metric_type)),
                       dv='metric',
                       within='baseline',
                       subject='subj_id',
                       between='fb_type',
                       padjust='fdr_bh')

fig, axes = plt.subplots(2, 2, figsize=(9, 4))
metric_types = ['magnitude', 'n_spindles', 'amplitude', 'duration']

p_all = np.zeros((4, 4))