-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCR Logistic Reg Fit.py
362 lines (263 loc) · 12 KB
/
PCR Logistic Reg Fit.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 22 16:01:27 2022
@author: Gustavo Reyes
"""
#%%
import sys
sys.path
sys.path.append('C:\\Users\Gustavo Reyes\Documents\GitHubFiles\CPS-Farm-To-Facility-Cilantro')
sys.path.append('C:\\Users\gareyes3\Documents\GitHub\CPS-Farm-To-Facility-Cilantro')
sys.path.append('C:\\Users\\reyes\\Documents\\GitHub\\CPS-Farm-To-Facility-Cilantro')
import numpy as np
import pandas as pd
from numpy.random import Generator, PCG64
rng = Generator(PCG64())
import matplotlib.pyplot as plt
import seaborn as sns
import pickle
import statsmodels.api as smf
import math
#%%
###############################################################################
#Product Testing Model Logistic nPCR FDA, not used old method.
R1=np.concatenate([np.ones(0), np.zeros(39)])
R2=np.concatenate([np.ones(12), np.zeros(28)])
R3=np.concatenate([np.ones(29), np.zeros(11)])
R4=np.concatenate([np.ones(40), np.zeros(0)])
T1=np.repeat(0,39)
T2=np.repeat(5,40)
T3=np.repeat(10,40)
T4=np.repeat(200,40)
Df_PD=pd.DataFrame({
"Cont": np.concatenate([T1,T2,T3,T4]),
"Results": np.concatenate([R1,R2,R3,R4]),
})
Df_PD.to_csv("C:\\Users\\Gustavo Reyes\\Documents\\GitHubFiles\\CPS-Farm-To-Facility-Cilantro\\Data_Cilantro_Outputs\\Product_Testing_Data.csv")
sns.scatterplot(data =Df_PD, x ="Cont", y= "Results" )
from sklearn import linear_model
logr = linear_model.LogisticRegression()
logr.fit(np.array(Df_PD["Cont"]).reshape(-1,1),np.array(Df_PD["Results"]))
logr.score(np.array(Df_PD["Cont"]).reshape(-1,1),np.array(Df_PD["Results"]))
logr.predict_proba(np.array([0]).reshape(-1,1))[0][1]
logr.intercept_
logr.coef_
def deviance(X, y, model):
return 2*metrics.log_loss(y, model.predict_proba(X), normalize=False)
def loglike(X, y, model):
return metrics.log_loss(y, model.predict_proba(X), normalize=False)
#chisqauered for the null model vs the full model
deviance(np.array(Df_PD["Cont"]).reshape(-1,1),np.array(Df_PD["Results"]), logr)
from sklearn.dummy import DummyClassifier
from sklearn import metrics
import scipy
logr_D = DummyClassifier()
logr_D.fit(np.array(Df_PD["Cont"]).reshape(-1,1),np.array(Df_PD["Results"]))
deviance(np.array(Df_PD["Cont"]).reshape(-1,1),np.array(Df_PD["Results"]), logr_D)
#chi sqaured.
scipy.stats.chi2.sf((220.36419628552224-99.34764524320522), 1)
#2.2328359100593915e-10
# save the model to disk
filename = 'C:\\Users\gareyes3\Documents\GitHub\CPS-Farm-To-Facility-Cilantro\logistic_Prod_Test_nPCR_FDA.sav'
pickle.dump(logr, open(filename, 'wb'))
np.arange(0, 200, 0.01)
probs_detect = []
for i in (np.arange(0, 200, 0.01)):
prob_detect = logr.predict_proba(np.array([i]).reshape(-1,1))[0][1]
probs_detect.append(prob_detect)
sns.scatterplot(data =Df_PD ,x ="Cont", y= "Results" )
sns.lineplot(y =probs_detect, x = np.arange(0, 200, 0.01))
plt.xlabel("OOcyst per 25g sample")
plt.ylabel("Probability of Detection")
plt.title("Product Testing nPCR Method")
##############################################################################
#Product Testing Model Logistic qPCR FDA USED in Model**** From Murphy 2018, new method, Lab A and B Data combined.
R1_qPCR=np.concatenate([np.ones(0), np.zeros(80)])
R2_qPCR=np.concatenate([np.ones(25), np.zeros(55)])
R3_qPCR=np.concatenate([np.ones(64), np.zeros(16)])
R4_qPCR=np.concatenate([np.ones(80), np.zeros(0)])
T1_qPCR=np.repeat(0,80)
T2_qPCR=np.repeat(5,80)
T3_qPCR=np.repeat(10,80)
T4_qPCR=np.repeat(200,80)
Df_PD_qPCR=pd.DataFrame({
"Cont": np.concatenate([T1_qPCR,T2_qPCR,T3_qPCR,T4_qPCR]),
"Results": np.concatenate([R1_qPCR,R2_qPCR,R3_qPCR,R4_qPCR]),
})
#Fitting the logistic regression model
from sklearn import linear_model
#Logistic Regression
logr_qPCR = linear_model.LogisticRegression()
logr_qPCR.fit(np.array(Df_PD_qPCR["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR["Results"]))
logr_qPCR.score(np.array(Df_PD_qPCR["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR["Results"]))
logr_qPCR.intercept_
logr_qPCR.coef_
from sklearn import metrics
def deviance(X, y, model):
return 2*metrics.log_loss(y, model.predict_proba(X), normalize=False)
def loglike(X, y, model):
return metrics.log_loss(y, model.predict_proba(X), normalize=False)
#chisqauered for the null model vs the full model
deviance(np.array(Df_PD_qPCR["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR["Results"]), logr_qPCR)
from sklearn.dummy import DummyClassifier
import scipy
logr_D_qPRC = DummyClassifier()
logr_D_qPRC.fit(np.array(Df_PD_qPCR["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR["Results"]))
deviance(np.array(Df_PD_qPCR["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR["Results"]), logr_D_qPRC)
#chi sqaured.
scipy.stats.chi2.sf(( 442.6011609459082-185.00983117735439), 1)
#5.7485453799292104e-58 goodness of fit satisfied.
# save the model to disk
filename = 'C://Users/gareyes3/Documents/GitHub/CPS-Farm-To-Facility-Cilantro/logistic_Prod_Test_qPCR_FDA.sav'
pickle.dump(logr_qPCR, open(filename, 'wb'))
#Trying other classifiers, not relevant for manuscript
'''
from sklearn.naive_bayes import GaussianNB
nbayes_qPCR = GaussianNB()
nbayes_qPCR.fit(np.array(Df_PD_qPCR["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR["Results"]))
nbayes_qPCR.score(np.array(Df_PD_qPCR["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR["Results"]))
from sklearn.ensemble import RandomForestClassifier
clf_qPCR = RandomForestClassifier()
clf_qPCR.fit(np.array(Df_PD_qPCR["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR["Results"]))
clf_qPCR.score(np.array(Df_PD_qPCR["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR["Results"]))
clf_qPCR.predict_proba(np.array([7]).reshape(-1,1))[0][1]
from sklearn import tree
tree_qPCR = tree.DecisionTreeClassifier()
tree_qPCR.fit(np.array(Df_PD_qPCR["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR["Results"]))
tree_qPCR.score(np.array(Df_PD_qPCR["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR["Results"]))
tree_qPCR.predict_proba(np.array([7]).reshape(-1,1))[0][1]
from sklearn import svm
svm_qPCR = svm.SVC(probability=True)
svm_qPCR.fit(np.array(Df_PD_qPCR["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR["Results"]))
svm_qPCR.score(np.array(Df_PD_qPCR["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR["Results"]))
svm_qPCR.predict_proba(np.array([7]).reshape(-1,1))[0][1]
logr_qPCR.predict_proba(np.array([10]).reshape(-1,1))[0][1]
'''
probs_detect = []
for i in (range(200)):
prob_detect = logr_qPCR.predict_proba(np.array([i]).reshape(-1,1))[0][1]
probs_detect.append(prob_detect)
sns.scatterplot(data =Df_PD_qPCR, x ="Cont", y= "Results" ,x_jitter=75)
sns.lineplot(y =probs_detect, x = range(200))
plt.xlabel("Oocyst per 25g sample")
plt.ylabel("Probability of Detection")
plt.title("Product Testing qPCR Method")
Df_PD_qPCR.to_csv("C:\\Users\\Gustavo Reyes\Documents\GitHubFiles\\CPS-Farm-To-Facility-Cilantro\\Data\\qPCR_Fit_Product_Testing.csv")
Df_probs_qPCR = pd.DataFrame({"Prob Detect":probs_detect,
"Cont" : range(200)})
Df_probs_qPCR.to_csv("C:\\Users\\Gustavo Reyes\Documents\GitHubFiles\\CPS-Farm-To-Facility-Cilantro\\Data\\qPCR_Fit_Product_Testing_Probs.csv")
probs_detect = []
for i in (range(200)):
prob_detect =clf_qPCR.predict_proba(np.array([i]).reshape(-1,1))[0][1]
probs_detect.append(prob_detect)
sns.scatterplot(data =Df_PD_qPCR, x ="Cont", y= "Results" )
sns.lineplot(y =probs_detect, x = range(200))
plt.xlabel("OOcyst per 25g sample")
plt.ylabel("Probability of Detection")
plt.title("Product Testing qPCR Method")
#Product Testing Model Logistic QPRCR 2021 Paper 2. Costa
R1_qPCR_2=np.concatenate([np.ones(0), np.zeros(5)])
R2_qPCR_2=np.concatenate([np.ones(1), np.zeros(5)])
R3_qPCR_2=np.concatenate([np.ones(3), np.zeros(1)])
R4_qPCR_2=np.concatenate([np.ones(5), np.zeros(0)])
T1_qPCR_2=np.repeat(0,5)
T2_qPCR_2=np.repeat(5,6)
T3_qPCR_2=np.repeat(10,4)
T4_qPCR_2=np.repeat(200,5)
Df_PD_qPCR_2=pd.DataFrame({
"Cont": np.concatenate([T1_qPCR,T2_qPCR,T3_qPCR,T4_qPCR]),
"Results": np.concatenate([R1_qPCR,R2_qPCR,R3_qPCR,R4_qPCR]),
})
#from statsmodels.discrete.discrete_model import Probit
from sklearn import linear_model
logr_qPCR_2 = linear_model.LogisticRegression()
logr_qPCR_2.fit(np.array(Df_PD_qPCR_2["Cont"]).reshape(-1,1),np.array(Df_PD_qPCR_2["Results"]))
#Provit_Model= Probit(Df_PD_qPCR_2["Results"], Df_PD_qPCR_2["Cont"]).fit()
#print(Provit_Model.summary())
#print(Provit_Model.predict(11.19))
# save the model to disk
filename = 'C:\\Users\gareyes3\Documents\GitHub\CPS-Farm-To-Facility-Cilantro\logistic_Prod_Test_qPCR_2_Costa.sav'
pickle.dump(logr_qPCR, open(filename, 'wb'))
probs_detect = []
for i in (range(200)):
# prob_detect = logr_qPCR_2.predict_proba(np.array([i]).reshape(-1,1))[0][1]
prob_detect = logr_qPCR_2.predict(i)[0]
probs_detect.append(prob_detect)
sns.scatterplot(data =Df_PD_qPCR, x ="Cont", y= "Results" )
sns.lineplot(y =probs_detect, x = range(200))
plt.xlabel("OOcyst per 25g sample")
plt.ylabel("Probability of Detection")
plt.title("Product Testing qPCR Method Paper 2")
#%%
#Agricultura Water Logisitc
R1_AW=np.concatenate([np.ones(0), np.zeros(12)])
R2_AW=np.concatenate([np.ones(8), np.zeros(4)])
R3_AW=np.concatenate([np.ones(3), np.zeros(0)])
R4_AW=np.concatenate([np.ones(6), np.zeros(0)])
R5_AW=np.concatenate([np.ones(3), np.zeros(0)])
R6_AW=np.concatenate([np.ones(6), np.zeros(0)])
T1_AW=np.repeat(0,12)
T2_AW=np.repeat(6,12)
T3_AW=np.repeat(12,3)
T4_AW=np.repeat(25,6)
T5_AW=np.repeat(100,3)
T6_AW=np.repeat(200,6)
Df_PD_AW=pd.DataFrame({
"Cont": np.concatenate([T1_AW,T2_AW,T3_AW,T4_AW,T5_AW,T6_AW]),
"Results": np.concatenate([R1_AW,R2_AW,R3_AW,R4_AW,R5_AW,R6_AW]),
})
Df_PD_AW.to_csv("C:\\Users\\gareyes3\\Documents\\GitHub\\CPS-Farm-To-Facility-Cilantro\\Data\\Water_Testing_Data.csv")
Df_PD_AW['intercept'] = 1.0
from sklearn import linear_model
from sklearn import metrics
import scipy
logr_AW = linear_model.LogisticRegression()
logr_AW.fit(np.array(Df_PD_AW["Cont"]).reshape(-1,1),np.array(Df_PD_AW["Results"]))
logr_AW.score(np.array(Df_PD_AW["Cont"]).reshape(-1,1),np.array(Df_PD_AW["Results"]))
logr_AW.intercept_
logr_AW.coef_
def deviance(X, y, model):
return 2*metrics.log_loss(y, model.predict_proba(X), normalize=False)
def loglike(X, y, model):
return metrics.log_loss(y, model.predict_proba(X), normalize=False)
#chisqauered for the null model vs the full model
deviance(np.array(Df_PD_AW["Cont"]).reshape(-1,1), np.array(Df_PD_AW["Results"]), logr_AW)
from sklearn.dummy import DummyClassifier
logr_AW_D = DummyClassifier()
logr_AW_D.fit(np.array(Df_PD_AW["Cont"]).reshape(-1,1),np.array(Df_PD_AW["Results"]))
deviance(np.array(Df_PD_AW["Cont"]).reshape(-1,1), np.array(Df_PD_AW["Results"]), logr_AW_D)
#chi sqaured.
scipy.stats.chi2.sf((55.82038884701287-15.568886670709356), 1)
#2.2328359100593915e-10
# importing libraries
import statsmodels.api as sm
import pandas as pd
# defining the dependent and independent variables
Xtrain = Df_PD_AW[['intercept',"Cont"]]
ytrain = Df_PD_AW[['Results']]
# building the model and fitting the data
log_reg = sm.Logit(ytrain, Xtrain).fit()
log_reg.predict(6)
print(log_reg.summary())
(math.exp(-4.4879239+0.85580838*0))/(1+(math.exp(-4.4879239+0.85580838*0)))
0.02577*32
1-(1-0.02577)**16
import statsmodels.api as sm
log_reg = sm.Logit(Df_PD_AW["Results"],Df_PD_AW["Cont"]).fit()
print(log_reg.summary())
probs_detect = []
for i in (range(200)):
prob_detect = logr_AW.predict_proba(np.array([i]).reshape(-1,1))[0][1]
probs_detect.append(prob_detect)
sns.scatterplot(data =Df_PD_AW, x ="Cont", y= "Results" )
sns.lineplot(y =probs_detect, x = range(200))
plt.xlabel("OOcyst per 10L sample")
plt.ylabel("Probability of Detection")
plt.title("Agricultural Water Fit")
Df_PD_AW.to_csv("C:\\Users\\gareyes3\\Documents\\GitHub\\CPS-Farm-To-Facility-Cilantro\\Data_Cilantro_Outputs\\qPCR_Fit_Water_Testing.csv")
Df_probs_logr_AW = pd.DataFrame({"Prob Detect":probs_detect,
"Cont" : range(200)})
Df_probs_logr_AW.to_csv("C:\\Users\\gareyes3\\Documents\\GitHub\\CPS-Farm-To-Facility-Cilantro\\Data_Cilantro_Outputs\\qPCR_Fit_Water_Testing_Probs.csv")
# save the model to disk
filename = 'C:\\Users\gareyes3\Documents\GitHub\CPS-Farm-To-Facility-Cilantro\logistic_AW_Testing_qPCR.sav'
pickle.dump(logr_AW , open(filename, 'wb'))