-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulations-TestComparisons-NegBin.py
279 lines (238 loc) · 12.4 KB
/
Simulations-TestComparisons-NegBin.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# coding: utf-8
import os
#os.chdir('/Users/louis.cammarata/Documents/ResearchProjects/2018/COMET/COMET-Simulations')
import xlmhg
import hgmd_v1 as hgmd
import GenerateSyntheticExpressionMatrix as gsec
import math
import pandas as pd
import numpy as np
import scipy.stats as ss
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import log_loss
import random
import matplotlib.pyplot as plt
from matplotlib import gridspec
from matplotlib import cm
import time
from tqdm import tqdm
# Likelihood Ratio Test for Logistic Regression
def LRT_LogReg(df):
# Define model matrix and response
X = np.matrix(df.drop('cluster', axis=1))
y = df['cluster']
# Train logistic regression with full model
logreg1 = LogisticRegression().fit(X,y)
ll1 = -log_loss(y,logreg1.predict_proba(X),normalize=False)
# Train logistic regression with null model (only intercept)
logreg0 = LogisticRegression().fit([[0]]*len(X) ,y)
ll0 = -log_loss(y,logreg0.predict_proba(X),normalize=False)
# Likelihood ratio test
stat = 2*(ll1-ll0)
pval = ss.chi2.sf(stat, 1)
return(pval)
# # Visualize Data
# Set seed
np.random.seed(13)
# Visualize Setting
n_cells = 1000
data = gsec.createNBExpressionMat2(n_cells=1000,
cluster_size=100,
c1_n=5, c1_p=0.5,
c0_n=1, c0_p=0.1,
offset=10,
n_outliers=0,
cout_n=1, cout_p=0)
count = np.array(data['gene'])
count1 = np.array(data.loc[data['cluster']==1]['gene'])
count0 = np.array(data.loc[data['cluster']!=1]['gene'])
plt.hist(count0,np.unique(count0))
plt.hist(count1,np.unique(count0))
plt.title('Data Histogram ('+str(n_cells)+' cells)')
plt.xlabel('Transcript count', fontsize = 20)
plt.ylabel('Number of cells', fontsize = 20)
plt.legend( [ "Cluster 1","Cluster 0" ], loc = "upper right", fontsize = 14)
#plt.savefig('pvalueMeanDiff-NB-Hist.eps', format='eps', dpi=1000)
plt.show()
# # I. Fixed high sample size, Running Effect Size
# Set seed
np.random.seed(13)
# Define parameters
n_cells = 200
cluster_size = 20
c1_n, c1_p = 5, 0.5
c0_n, c0_p = 1, 0.1
repeat = 100
# Define range of variation in mean difference and effect size
epsilon_range = epsilon_range = 4+np.arange(0,12,1)
# Initialize record vectors
xlmHG_pv, mHG_pv, t_pv, w_pv, ks_pv, lr_pv = [], [], [], [], [], []
xlmHG_pv_sd, mHG_pv_sd, t_pv_sd, w_pv_sd, ks_pv_sd, lr_pv_sd = [], [], [], [], [], []
# Generate expression matrices and populate memory arrays
for e in tqdm(epsilon_range):
time.sleep( .01 )
# Initialize mean values
xlmHG, mHG, t, w, ks, lr = [], [], [], [], [], []
for k in np.arange(0,repeat,1):
# Generate gene expression matrix
cell_data = gsec.createNBExpressionMat2(n_cells=n_cells,
cluster_size=cluster_size,
c1_n=c1_n, c1_p=c1_p,
c0_n=c0_n, c0_p=c0_p,
offset = e,
n_outliers=0,
cout_n=1, cout_p=1)
# Perform tests
test_results = hgmd.singleton_test(cell_data,1,start=1,X=np.int(0.15*cluster_size),L=np.int(2*cluster_size))
test_results0 = hgmd.singleton_test(cell_data,1,start=1) #XL1
# Fill in vectors
xlmHG.append(float(test_results['mHG_pval']))
mHG.append(float(test_results0['mHG_pval']))
t.append(float(test_results['t_pval']))
w.append(float(test_results['w_pval']))
ks.append(float(test_results['ks_pval']))
lr.append(float(LRT_LogReg(cell_data)))
# Keep track of values
xlmHG_pv.append(np.mean(xlmHG))
xlmHG_pv_sd.append(np.var(xlmHG)**0.5)
mHG_pv.append(np.mean(mHG))
mHG_pv_sd.append(np.var(mHG)**0.5)
t_pv.append(np.mean(t))
t_pv_sd.append(np.var(t)**0.5)
w_pv.append(np.mean(w))
w_pv_sd.append(np.var(w)**0.5)
ks_pv.append(np.mean(ks))
ks_pv_sd.append(np.var(ks)**0.5)
lr_pv.append(np.mean(lr))
lr_pv_sd.append(np.var(lr)**0.5)
meandiff_range = c1_n*(1-c1_p)/c1_p-c0_n*(1-c0_p)/c0_p+epsilon_range
# Plot p-value vs. True Effect Size Across Clusters for the 3 different tests¶
f = plt.figure(figsize=(8, 6))
plt.errorbar(meandiff_range,np.array(t_pv),yerr = [np.minimum(np.array(t_pv_sd),np.array(t_pv)),np.minimum(np.array(t_pv_sd),1-np.array(t_pv))],color='blue',marker='D', linestyle = 'None')
plt.errorbar(meandiff_range,w_pv,yerr = [np.minimum(np.array(w_pv_sd),np.array(w_pv)),np.minimum(np.array(w_pv_sd),1-np.array(w_pv))],color='green',marker='s', linestyle = 'None')
plt.errorbar(meandiff_range,ks_pv,yerr = [np.minimum(np.array(ks_pv_sd),np.array(ks_pv)),np.minimum(np.array(ks_pv_sd),1-np.array(ks_pv))],color='orange',marker='P', linestyle = 'None')
plt.errorbar(meandiff_range,lr_pv,yerr = [np.minimum(np.array(lr_pv_sd),np.array(lr_pv)),np.minimum(np.array(lr_pv_sd),1-np.array(lr_pv))],color='magenta',marker='*', linestyle = 'None')
plt.errorbar(meandiff_range,np.array(mHG_pv),yerr = [np.minimum(np.array(mHG_pv_sd),np.array(mHG_pv)),np.minimum(np.array(mHG_pv_sd),1-np.array(mHG_pv))],color='brown',marker='X', linestyle = 'None')
plt.errorbar(meandiff_range,np.array(xlmHG_pv),yerr = [np.minimum(np.array(xlmHG_pv_sd),np.array(xlmHG_pv)),np.minimum(np.array(xlmHG_pv_sd),1-np.array(xlmHG_pv))],color='red',marker='X', linestyle = 'None')
plt.plot([0, np.max(meandiff_range)], [0.05, 0.05], 'k-', lw=0.5)
#plt.title('p-Value vs. Mean Dispersion ('+str(n_cells)+' cells)')
plt.xlabel('Mean Difference', fontsize = 20)
plt.ylabel('p-Value', fontsize = 20)
plt.ylim(-0.05,1.05)
#plt.xscale('log')
plt.legend( [ "0.05", "t-test",
"Wilcoxon Rank Sum test","Kolmogorov-Smirnov test",
"Likelihood Ratio Test (LR)","mHG test", "XL-mHG test"],bbox_to_anchor=(1.04,0.5), loc="center left", borderaxespad=0, fontsize = 14)
plt.savefig('pvalueMeanDiff-NB-200cells.eps', format='eps', dpi=1000,bbox_inches='tight')
plt.show()
# Figure with broken axes
#f,(ax1,ax2) = plt.subplots(1,2,sharey=True, facecolor='w')
m1, M1 = -0.05,0.75
m2, M2 = 2.75,8
prop = (M1-m1)/(M1+M2-m1-m2)
f = plt.figure(figsize=(8, 6))
gs = gridspec.GridSpec(1, 2, width_ratios=[prop, 1-prop])
meandiff_range = c1_n*(1-c1_p)/c1_p-c0_n*(1-c0_p)/c0_p+epsilon_range
# Plot data on ax1
ax1 = plt.subplot(gs[0])
ax1.errorbar(meandiff_range,np.array(t_pv),yerr = np.array(t_pv_sd),color='blue',marker='D', linestyle = 'None')
ax1.errorbar(meandiff_range,w_pv,yerr = np.array(w_pv_sd),color='green',marker='s', linestyle = 'None')
ax1.errorbar(meandiff_range,ks_pv,yerr = np.array(ks_pv_sd),color='orange',marker='P', linestyle = 'None')
ax1.errorbar(meandiff_range,lr_pv,yerr = np.array(lr_pv_sd),color='magenta',marker='*', linestyle = 'None')
ax1.errorbar(meandiff_range,np.array(mHG_pv),yerr = [np.array(mHG_pv_sd),np.minimum(np.array(mHG_pv_sd),1-np.array(mHG_pv))],color='red',marker='X', linestyle = 'None')
ax1.plot([0, np.max(meandiff_range)], [0.05, 0.05], 'k-', lw=0.5)
# Plot data on ax2
ax2 = plt.subplot(gs[1])
ax2.errorbar(meandiff_range,np.array(t_pv),yerr = np.array(t_pv_sd),color='blue',marker='D', linestyle = 'None')
ax2.errorbar(meandiff_range,w_pv,yerr = np.array(w_pv_sd),color='green',marker='s', linestyle = 'None')
ax2.errorbar(meandiff_range,ks_pv,yerr = np.array(ks_pv_sd),color='orange',marker='P', linestyle = 'None')
ax2.errorbar(meandiff_range,lr_pv,yerr = np.array(lr_pv_sd),color='magenta',marker='*', linestyle = 'None')
ax2.errorbar(meandiff_range,np.array(mHG_pv),yerr = [np.array(mHG_pv_sd),np.minimum(np.array(mHG_pv_sd),1-np.array(mHG_pv))],color='red',marker='X', linestyle = 'None')
ax2.plot([0, np.max(meandiff_range)], [0.05, 0.05], 'k-', lw=0.5)
ax1.set_xlim(m1,M1)
ax2.set_xlim(m2,M2)
# hide the spines between ax and ax2
ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax1.yaxis.tick_left()
ax1.tick_params(labelright='off')
ax2.yaxis.tick_right()
plt.legend( [ "0.05", "t", "WRS","KS", "LRT (LR)","XL-mHG" ],bbox_to_anchor=(1.1,0.5), loc="center left", borderaxespad=0)
plt.xlabel('Mean Difference')
ax1.set_ylabel('p-Value')
#plt.savefig('pvalueMeanDiff-NB-200cells.eps', format='eps', dpi=1000,bbox_inches='tight')
plt.show()
# # II. Running Sample Size, Fixed Small Mean Difference
# Set seed
np.random.seed(13)
# Define parameters
c1_n, c1_p = 5, 0.5
c0_n, c0_p = 1, 0.1
offset = 6
repeat = 100
# Define range of variation in sample size
#nrange = np.arange(100,10000,200)
nrange = np.logspace(1,4,11,dtype='int')
# Initialize record vectors
xlmHG_pv, mHG_pv, t_pv, w_pv, ks_pv, lr_pv = [], [], [], [], [], []
xlmHG_pv_sd, mHG_pv_sd, t_pv_sd, w_pv_sd, ks_pv_sd, lr_pv_sd = [], [], [], [], [], []
# Generate expression matrices and populate memory arrays
for n in tqdm(nrange):
time.sleep( .01 )
# Initialize mean values
xlmHG, mHG, t, w, ks, lr = [], [], [], [], [], []
# Define cluster size and compute pooled standard deviations
cluster_size = np.int(0.1*n)
for k in np.arange(0,repeat):
# Generate gene expression matrix
cell_data = gsec.createNBExpressionMat2(n_cells=n,
cluster_size=cluster_size,
c1_n=c1_n, c1_p=c1_p,
c0_n=c0_n, c0_p=c0_p,
offset = offset,
n_outliers=0,
cout_n=1, cout_p=1)
# Perform tests
test_results = hgmd.singleton_test(cell_data,1,start=1,X=np.int(0.15*cluster_size),L=np.int(2*cluster_size))
test_results0 = hgmd.singleton_test(cell_data,1,start=1) #XL1
# Fill in vectors
xlmHG.append(float(test_results['mHG_pval']))
mHG.append(float(test_results0['mHG_pval']))
t.append(float(test_results['t_pval']))
w.append(float(test_results['w_pval']))
ks.append(float(test_results['ks_pval']))
lr.append(float(LRT_LogReg(cell_data)))
# Keep track of values
xlmHG_pv.append(np.mean(xlmHG))
xlmHG_pv_sd.append(np.var(xlmHG)**0.5)
mHG_pv.append(np.mean(mHG))
mHG_pv_sd.append(np.var(mHG)**0.5)
t_pv.append(np.mean(t))
t_pv_sd.append(np.var(t)**0.5)
w_pv.append(np.mean(w))
w_pv_sd.append(np.var(w)**0.5)
ks_pv.append(np.mean(ks))
ks_pv_sd.append(np.var(ks)**0.5)
lr_pv.append(np.mean(lr))
lr_pv_sd.append(np.var(lr)**0.5)
# # Plot p-value vs. Sample Size for the 3 different tests
# Plot p-value vs. Sample Size for the 3 different tests¶
f = plt.figure(figsize=(8, 6))
plt.plot([np.min(nrange), np.max(nrange)], [0.05, 0.05], 'k-', lw=0.5)
plt.errorbar(nrange,np.array(t_pv),yerr = [np.minimum(np.array(t_pv_sd),np.array(t_pv)),np.minimum(np.array(t_pv_sd),1-np.array(t_pv))],color='blue',marker='D', linestyle = 'None')
plt.errorbar(nrange,w_pv,yerr = [np.minimum(np.array(w_pv_sd),np.array(w_pv)),np.minimum(np.array(w_pv_sd),1-np.array(w_pv))],color='green',marker='s', linestyle = 'None')
plt.errorbar(nrange,ks_pv,yerr = [np.minimum(np.array(ks_pv_sd),np.array(ks_pv)),np.minimum(np.array(ks_pv_sd),1-np.array(ks_pv))],color='orange',marker='P', linestyle = 'None')
plt.errorbar(nrange,lr_pv,yerr = [np.minimum(np.array(lr_pv_sd),np.array(lr_pv)),np.minimum(np.array(lr_pv_sd),1-np.array(lr_pv))],color='magenta',marker='*', linestyle = 'None')
plt.errorbar(nrange,np.array(mHG_pv),yerr = [np.minimum(np.array(mHG_pv_sd),np.array(mHG_pv)),np.minimum(np.array(mHG_pv_sd),1-np.array(mHG_pv))],color='brown',marker='X', linestyle = 'None')
plt.errorbar(nrange,np.array(xlmHG_pv),yerr = [np.minimum(np.array(xlmHG_pv_sd),np.array(xlmHG_pv)),np.minimum(np.array(xlmHG_pv_sd),1-np.array(xlmHG_pv))],color='red',marker='X', linestyle = 'None')
#plt.title('p-Value vs. Sample Size (mean diff = '+str(e)+')')
plt.xlabel('Sample Size', fontsize = 20)
plt.ylabel('p-Value', fontsize = 20)
plt.legend( [ "0.05", "t-test",
"Wilcoxon Rank Sum test","Kolmogorov-Smirnov",
"Likelihood Ratio Test (LR)","mHG test","XL-mHG test" ], bbox_to_anchor=(1.04,0.5), loc="center left", borderaxespad=0, fontsize = 14)
plt.xscale('log')
plt.ylim(-0.05,1.05)
#plt.xlim([np.min(nrange)-10, np.max(nrange)])
#plt.savefig('pvalueSampleSize-NB.eps', format='eps', dpi=1000,bbox_inches='tight')
plt.show()