/
11_dPTE_label_stats.py
109 lines (101 loc) · 4.15 KB
/
11_dPTE_label_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# calculate and examine Linear Mixed Models on dPTE trial-based data #
import mne
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.ion()
import statsmodels.api as sm
import statsmodels.formula.api as smf
# set directory
save_dir = "/home/cora/hdd/MEG/NEMO_analyses_new/proc/dPTE/"
# load the dPTE dataframe
df_NEM_dPTE_alpha = pd.read_csv("{}NEMO_dPTE_alpha.csv".format(save_dir))
df_NEM_dPTE_beta = pd.read_csv("{}NEMO_dPTE_beta.csv".format(save_dir))
# the dPTE label connections to look at
columns = ["Subject","Emo","Connection","dPTE"]
emos = ["neg","pos"]
conxs = ["LO-IP","LO-IT","LO-SM","LO-TT","IP-IT",
"IP-SM","IP-TT","IT-SM","IT-TT","SM-TT"]
# separate LMMs for each connection
# setup LMM
# for Alpha band
print("Doing stats on ALPHA dPTE connections")
for conx in conxs:
print("Calculating LMM for connection: {}".format(conx))
df = df_NEM_dPTE_alpha[df_NEM_dPTE_alpha.Connection == conx].infer_objects()
try:
res_0 = smf.mixedlm('dPTE ~ 1', data=df, groups=df['Subject']).fit(reml=False)
print("Null model:")
print(res_0.params)
print(res_0.summary())
print("AIC: {}".format(res_0.aic))
except:
print("Null Model could not converge...")
continue
try:
res_emo = smf.mixedlm('dPTE ~ Emo', data=df, groups=df['Subject']).fit(reml=False)
except:
print("Emotion Model could not converge...")
continue
print("Emotion model:")
print(res_emo.params)
print(res_emo.summary())
print("AIC: {}".format(res_emo.aic))
if res_emo.aic < res_0.aic:
print("Is AIC_Emo sign. better than 0_AIC? -- est. p-value: {}".format(np.exp((res_emo.aic - res_0.aic)/2)))
# for Beta band
print("Doing stats on BETA dPTE connections")
for conx in conxs:
print("Calculating LMM for connection: {}".format(conx))
df = df_NEM_dPTE_beta[df_NEM_dPTE_beta.Connection == conx].infer_objects()
try:
res_0 = smf.mixedlm('dPTE ~ 1', data=df, groups=df['Subject']).fit(reml=False)
print("Null model:")
print(res_0.params)
print(res_0.summary())
print("AIC: {}".format(res_0.aic))
except:
print("Null Model could not converge...")
continue
try:
res_emo = smf.mixedlm('dPTE ~ Emo', data=df, groups=df['Subject']).fit(reml=False)
except:
print("Emotion Model could not converge...")
continue
print("Emotion model:")
print(res_emo.params)
print(res_emo.summary())
print("AIC: {}".format(res_emo.aic))
if res_emo.aic < res_0.aic:
print("Is AIC_Emo sign. better than 0_AIC? -- est. p-value: {}".format(np.exp((res_emo.aic - res_0.aic)/2)))
# full LMM models with connection as factor, and an rm_ANOVA for comparison
# ALPHA models
# full interaction model
res_int_a = smf.mixedlm('dPTE ~ Emo * Connection', data=dfa, groups=dfa['Subject']).fit(reml=False)
print(res_int_a.summary())
print(res_int_a.aic)
# null model
res_0_a = smf.mixedlm('dPTE ~ 1', data=dfa, groups=dfa['Subject']).fit(reml=False)
print(res_0_a.summary())
print(res_0_a.aic)
# single factor 'emotion'
res_emo_a = smf.mixedlm('dPTE ~ Emo', data=dfa, groups=dfa['Subject']).fit(reml=False)
print(res_emo_a.summary())
print(res_emo_a.aic)
# single factor 'connection'
res_con_a = smf.mixedlm('dPTE ~ Connection', data=dfa, groups=dfa['Subject']).fit(reml=False)
print(res_con_a.summary())
print(res_con_a.aic)
# two independent factors model
res_2f_a = smf.mixedlm('dPTE ~ Emo + Connection', data=dfa, groups=dfa['Subject']).fit(reml=False)
print(res_2f_a.summary())
print(res_2f_a.aic)
# rm ANOVA & Tukey HSD post-hoc tests
anova_a = statsmodels.stats.anova.AnovaRM(df_NEM_dPTE_alpha,depvar="dPTE",subject="Subject",within=["Emo","Connection"],aggregate_func='mean')
anova_a_res = anova_a.fit()
print(anova_a_res.summary())
res_tukey_emo_a = statsmodels.stats.multicomp.pairwise_tukeyhsd(df_NEM_dPTE_alpha.dPTE, groups=df_NEM_dPTE_alpha.Emo, alpha=0.05)
print(res_tukey_emo_a.summary())
res_tukey_con_a = statsmodels.stats.multicomp.pairwise_tukeyhsd(df_NEM_dPTE_alpha.dPTE, groups=df_NEM_dPTE_alpha.Connection, alpha=0.05)
print(res_tukey_con_a.summary())
# AIC diff significance test for best 2 models