DATA = "prolifc_data_combine_num_each_pp.xlsx" DATA2 = "prolifc_data_each_pp.xlsx" winsize = 0.6 # ANOVA within subject clustering (5) * type (2) for each winsize data = pd.read_excel(PATH + DATA) data = data[data["winsize"] == winsize] aov = pg.rm_anova(data=data, dv="mean_deviation_score", within=["percent_triplets", "protectzonetype"], subject="participant") posthocs = pg.pairwise_ttests( dv="mean_deviation_score", within=["percent_triplets", "protectzonetype"], subject="participant", data=data, padjust="fdr_bh", effsize="cohen") # ANOVA within subject data2 = pd.read_excel(PATH + DATA2) data2 = data2[data2["winsize"] == 0.4] # winsize 0.4 unblanced data aov_table = AnovaRM( data=data2, depvar="mean_deviation_score", subject="participant", within=["protectzonetype", "numerosity", "percent_triplets"]).fit() aov_table.summary()
# so we create new columns subs = 3*subjs subs.sort() conds = ["N","P","T"] * len(subjs) laut = [] for i in range(len(subjs)): laut.append(group_df["N_Laut"][i]) laut.append(group_df["P_Laut"][i]) laut.append(group_df["T_Laut"][i]) ang = [] for i in range(len(subjs)): ang.append(group_df["N_Ang"][i]) ang.append(group_df["P_Ang"][i]) ang.append(group_df["T_Ang"][i]) # and throw them in a dict to make a dataframe dict = {"Subject":subs,"Bed":conds,"Laut":laut,"Ang":ang} temo_long = pd.DataFrame(dict) # now we can calculate the rmANOVAs for each Laut & Ang (see how we imported AnovaRM from statsmodels above) anova_laut = AnovaRM(temo_long,depvar="Laut",subject="Subject",within=["Bed"],aggregate_func=None).fit() print(anova_laut.summary()) tukey_hsd_laut = pairwise_tukeyhsd(temo_long.Laut, groups=temo_long.Bed, alpha=0.05) print(tukey_hsd_laut.summary()) anova_ang = AnovaRM(temo_long,depvar="Ang",subject="Subject",within=["Bed"],aggregate_func=None).fit() print(anova_ang.summary()) tukey_hsd_ang = pairwise_tukeyhsd(temo_long.Ang, groups=temo_long.Bed, alpha=0.05) print(tukey_hsd_ang.summary()) ## Example for saving a csv file from dataframe to read with another program (e.g. R or Excel) # temo_df.to_csv("{}TEMO.csv".format(dir)) # define directory and file name to your need...
sub_id = sub_id + 1 for ind_t, task in enumerate(task_list): for ind_c, con in enumerate(condition_list): # generate random value here as example my_val = np.random.normal(ind_c + ind_t, 1, 1)[0] df_full.loc[my_row] = [ group, lan, sub_id, task, con, my_val ] my_row = my_row + 1 # conduct ANOVA using mixedlm my_model_fit = smf.mixedlm("my_value ~ group * language * task * condition", df_full, groups=df_full["sub_id"]).fit() # get fixed effects my_model_fit.summary() # get random effects my_model_fit.random_effects # 4-way analysis possible in ols, but random effects not accounted for! my_model_fit = smf.ols( formula='my_value ~ group * language * task * condition', data=df_full).fit() sm.stats.anova_lm(my_model_fit, typ=2) my_model_fit.summary() # -------- # Save dataframes as csv # -------- df_2way.to_csv('df_2way.csv', index=False) df_full.to_csv('df_full.csv', index=False)