label = ['AHC', 'Bout', 'CO', 'GC', 'IPC', 'LMX', 'LS', 'MF', 'PS', 'RE', 'WES', 'WLC', 'WLSP']
def read_data(construct):
data_B = pd.read_excel(construct + '.xlsx', sheet_name='covid')
data_A = pd.read_excel(construct + '.xlsx', sheet_name='exp')
array_B = data_B.values.reshape(1, -1)[0]
array_B = array_B[~np.isnan(array_B)]
array_A = data_A.values.reshape(1, -1)[0]
array_A = array_A[~np.isnan(array_A)]
return (array_A, array_B)
def combine(array_A, array_B):
x = array_A.tolist()
x += array_B.tolist()
return x
def permutation_A_B(x, n_B, n_A):
random.shuffle(x)
x = pd.Series(x)
n = n_A + n_B
idx_A = set(random.sample(range(n), n_A))
idx_B = set(range(n)) - idx_A
return x.loc[idx_A].mean() - x.loc[idx_B].mean()
def permutation_B_A(x, n_B, n_A):
random.shuffle(x)
x = pd.Series(x)
n = n_A + n_B
idx_A = set(random.sample(range(n), n_A))
idx_B = set(range(n)) - idx_A
return x.loc[idx_B].mean() - x.loc[idx_A].mean()
p_perm_list = []
p_t_test = []
for i in label:
array_A, array_B = read_data(i)
combine(array_A, array_B)
diff = np.mean(array_A) - np.mean(array_B)
x = combine(array_A, array_B)
if (diff < 0):
print('For ' + i + ': Covid > Exp')
diff = np.mean(array_B) - np.mean(array_A)
perm_diffs = [permutation_B_A(x, len(array_B.tolist()), len(array_A.tolist())) for _ in range(1000)]
else:
print('For ' + i + ': Exp > Covid')
perm_diffs = [permutation_A_B(x, len(array_B.tolist()), len(array_A.tolist())) for _ in range(1000)]
fig, ax = plt.subplots()
ax.hist(perm_diffs, bins=11, rwidth=0.9)
ax.axvline(x=diff, color='red', lw=2)
ax.set_xlabel('Differences')
ax.set_ylabel('Frequencies')
plt.legend(['Observed\ndifference'])
ax.set_title(i)
plt.show()
p_perm = np.mean(perm_diffs >= diff)
p_perm_list.append(p_perm)
print(f'p_perm: {p_perm}')
res = stats.ttest_ind(array_A, array_B, equal_var=False)
p_t_test.append(res.pvalue/2)
print(f'p_t_test: {res.pvalue / 2:.4f}')
print('\n')
p_perm: 0.0
p_t_test: 0.0000
For Bout: Covid > Exp
p_perm: 0.0
p_t_test: 0.0000
For CO: Exp > Covid
p_perm: 0.0
p_t_test: 0.0000
For GC: Exp > Covid
p_perm: 0.0
p_t_test: 0.0000
For IPC: Exp > Covid
p_perm: 0.0
p_t_test: 0.0000
For LMX: Exp > Covid
p_perm: 0.096
p_t_test: 0.0846
For LS: Exp > Covid
p_perm: 0.447
p_t_test: 0.4497
For MF: Exp > Covid
p_perm: 0.0
p_t_test: 0.0000
For PS: Covid > Exp
p_perm: 0.383
p_t_test: 0.3950
For RE: Exp > Covid
p_perm: 0.002
p_t_test: 0.0016
For WES: Exp > Covid
p_perm: 0.0
p_t_test: 0.0000
For WLC: Covid > Exp
p_perm: 0.0
p_t_test: 0.0000
For WLSP: Exp > Covid
p_perm: 0.0
p_t_test: 0.0000