-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions_PrDetectingDisease.R
529 lines (388 loc) · 17.2 KB
/
Functions_PrDetectingDisease.R
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#Posterior probability is calculated by updating the prior probability by using Bayes' theorem.
#Assuming Sp=1
#Assuming prior = Pr(population (country) is free)
#NPV = Pr(D-|T-) = pfree
#NPV = (1-prior)/((1-prior)+prior(1-SSe)) = (1-prior)/(1-prior*Se)
#SSe = population/system sensitivity
#Discounting prior probability by risk of introduction
pfree.est<-function(SSe,p.intro,priorPFree){
PostPInf_tl1 = (1-priorPFree)
priorPInf = PostPInf_tl1 + p.intro - (PostPInf_tl1*p.intro)
PFree = (1-priorPInf)/(1-priorPInf*SSe)
as.data.frame(cbind(SSe,priorPFree,p.intro,priorPInf,PFree))
}
#Example
#pp<-pfree.est(SSe=0.45,p.intro=0.01,priorPFree=0.5)
#############################################################################
#Application 2 Estimating probability of detecting disease
#############################################################################
source("Functions_modelling_dSe1.R")
#Calculate dSeTab1 - table with expected Se (estimated by rescaling infection curves)
#for testing RLN and high and low-quality obex samples
#IncludeSeVar - function to add stocasticity around dSe given by infection curve
#x1A - number of max time steps since infection for adults
#x1Y - number of max time steps since infection for yearlings
#8 time steps = 1 month
##############################################################################
#simSSe3L - function to estimate Surveilance Sensitivity
#Assuming 3 risk levels, separating between yearlings, adult females and adult males
################################################################################
#Time since infection are randomly drawn from time line
#N = c(mean population size yearlings, population size adult females and adult males)
#N_sd = c(sd population size yearlings, population size adult females and adult males)
#n = c(number of yearlings sampled, number of adult females and males sampled)
#RR = c(1,2,4) - relative risk of yearlings, adult females and adult males
#pstarN = design prevalencegiven as number infected
#PrRLN = Proportion of samples inlcuding RLN
#dSeTab = table with expected Se (estimated by rescaling infection curves) for testing RLN and high and low-quality obex samples
#nsim = nr of simulations
#x1Y = vector with possible infection times for infected yearlings
#x1A = vector with possible infection times for infected adults
#rpert-function are utilized to random draw values from a distribution defined by min, max and mode
#PrLQ.min=0.02,
#PrLQ.max=0.60,
#PrLQ.mode=0.22,
simSSe3L<-function(N=N,N_sd=N_sd, nn=nn,RR=RR1,pstarN=pstarN,dSE=dSe1,PrRLN=PrRLN,x1Y=x1Y,x1A=x1A,nsim=nsim){
#n1 = nn[1] +nn[2] +nn[3]
#PrAf<-nn[2]/n1
#PrAm<-nn[3]/n1
SSe<-rep(NA,nsim)
Pr_AllTestingNeg<-SSe
#Draw random proportion of LQ obex sample from given pert-distribution
PrLQv<-rpert(n=nsim,x.min=PrLQ.min,x.max=PrLQ.max,x.mode=PrLQ.mode)
N1<-rnorm(n=nsim,mean=N[1],sd=N_sd[1])
N2<-rnorm(n=nsim,mean=N[2],sd=N_sd[2])
N3<-rnorm(n=nsim,mean=N[3],sd=N_sd[3])
N1[N1<=0]<-1
N2[N2<=0]<-1
N3[N3<=0]<-1
for (i in c(1:nsim)){
Ntot=N1[i] + N2[i] + N3[i]
pstar=round(pstarN/Ntot,4)
#Adjusted risk
ARy <- 1/ (RR[3]*(N3[i]/Ntot)+RR[2]*(N2[i]/Ntot) + (N1[i]/Ntot)) #yearlings
ARadf <- RR[2]*ARy #adult females
ARadm <- RR[3]*ARy #adult males
nnv1<-ifelse(nn[1]<N1[i],nn[1],N1[i]-1)
nnv2<-ifelse(nn[2]<N2[i],nn[2],N2[i]-1)
nnv3<-ifelse(nn[3]<N3[i],nn[3],N3[i]-1)
#Draw random time since infection
tpY<-sample(x1Y,nnv1,replace=T) #yearlings
tpAdf<-sample(x1A,nnv2,replace=T) #adult females
tpAdm<-sample(x1A,nnv3,replace=T) #adult males
#dSe for yearlings
dSE_RLNobexY<-dSE[tpY,"yRLN"]
dSE_obexYLQ<-dSE[tpY,"yObexLowq"]
dSE_obexYHQ<-dSE[tpY,"yObexLowq"]
#dSe for adult females
dSE_RLNobexAdf<-dSE[tpAdf,"yRLN"]
dSE_obexAdfLQ<-dSE[tpAdf,"yObexLowq"]
dSE_obexAdfHQ<-dSE[tpAdf,"yObexHighq"]
#dSe for adult males
dSE_RLNobexAdm<-dSE[tpAdm,"yRLN"]
dSE_obexAdmLQ<-dSE[tpAdm,"yObexLowq"]
dSE_obexAdmHQ<-dSE[tpAdm,"yObexHighq"]
#dSe for yearlings with stochasticy
dSE_RLNobexYi<-IncludeSeVar(Y=dSE_RLNobexY)
dSE_obexYLQi<-IncludeSeVar(Y=dSE_obexYLQ)
dSE_obexYHQi<-IncludeSeVar(Y=dSE_obexYHQ)
#dSe for adult females with stochasticity
dSE_RLNobexAdfi<-IncludeSeVar(Y=dSE_RLNobexAdf)
dSE_obexAdfLQi<-IncludeSeVar(Y=dSE_obexAdfLQ)
dSE_obexAdfHQi<-IncludeSeVar(Y=dSE_obexAdfHQ)
#dSe for adult males with stochasticity
dSE_RLNobexAdmi<-IncludeSeVar(Y=dSE_RLNobexAdm)
dSE_obexAdmLQi<-IncludeSeVar(Y=dSE_obexAdmLQ)
dSE_obexAdmHQi<-IncludeSeVar(Y=dSE_obexAdmHQ)
#Probability of positive test result given Age class, Sex and tissue type
PrYRLN<-ARy*PrRLN*dSE_RLNobexYi
PrAdfRLN<-ARadf*PrRLN*dSE_RLNobexAdfi
PrAdmRLN<-ARadm*PrRLN*dSE_RLNobexAdmi
PrYobex<-ARy*(1-PrRLN)*(PrLQv[i]*dSE_obexYLQi + (1-PrLQv[i])* dSE_obexYHQi)
PrAdfobex<-ARadf*(1-PrRLN)*(PrLQv[i]*dSE_obexAdfLQi + (1-PrLQv[i])* dSE_obexAdfHQi)
PrAdmobex<-ARadm*(1-PrRLN)*(PrLQv[i]*dSE_obexAdmLQi + (1-PrLQv[i])* dSE_obexAdmHQi)
SeY<-PrYRLN+PrYobex
SeAdf<-PrAdfRLN+PrAdfobex
SeAdm<-PrAdmRLN+PrAdmobex
#Pr_NonTestingPos
Pr_AllTestingNeg[i]<- (1 - sum(SeY,SeAdf,SeAdm)/Ntot)^(pstar*Ntot)
#SSe = Surveillance sensitivity
SSe[i] <- 1 - Pr_AllTestingNeg[i]
}
SSe
}
#############################
#############################
#N and nn is given as a matrix of simulated data (nrow=nsim)
simSSe3LmeanDSE_i<-function(N=N,nn=nn,k1=k,RR=RR1,pstarN=pstarN,dSE=dSe1,PrRLN=PrRLN,x1Y=x1Y,x1A=x1A,nsim=nsim){
SSe<-rep(NA,nsim)
Pr_AllTestingNeg<-SSe
#Draw random proportion of LQ obex sample from given pert-distribution
PrLQv<-rpert(n=nsim,x.min=PrLQ.min,x.max=PrLQ.max,x.mode=PrLQ.mode)
for (i in c(1:nsim)){
N1=N[3,k1,i]+N[4,k1,i]
N2=N[5,k1,i]
N3=N[6,k1,i]
Ntot=N1 + N2 + N3
pstar=round(pstarN/Ntot,4)
#Adjusted risk
ARy <- 1/ (RR[3]*(N3/Ntot)+RR[2]*(N2/Ntot) + (N1/Ntot)) #yearlings
ARadf <- RR[2]*ARy #adult females
ARadm <- RR[3]*ARy #adult males
nn1=nn[3,k1,i]+nn[4,k1,i]
nn2=nn[5,k1,i]
nn3=nn[6,k1,i]
nnv1<-ifelse(nn1<N1,nn1,N1-1)
nnv2<-ifelse(nn2<N2,nn2,N2-1)
nnv3<-ifelse(nn3<N3,nn3,N3-1)
#Draw random time since infection
tpY<-sample(x1Y,nnv1,replace=T) #yearlings
tpAdf<-sample(x1A,nnv2,replace=T) #adult females
tpAdm<-sample(x1A,nnv3,replace=T) #adult males
#dSe for yearlings
dSE_RLNobexY<-dSE[tpY,"yRLN"]
dSE_obexYLQ<-dSE[tpY,"yObexLowq"]
dSE_obexYHQ<-dSE[tpY,"yObexLowq"]
#dSe for adult females
dSE_RLNobexAdf<-dSE[tpAdf,"yRLN"]
dSE_obexAdfLQ<-dSE[tpAdf,"yObexLowq"]
dSE_obexAdfHQ<-dSE[tpAdf,"yObexHighq"]
#dSe for adult males
dSE_RLNobexAdm<-dSE[tpAdm,"yRLN"]
dSE_obexAdmLQ<-dSE[tpAdm,"yObexLowq"]
dSE_obexAdmHQ<-dSE[tpAdm,"yObexHighq"]
#dSe for yearlings without stochasticy
dSE_RLNobexYi<-dSE_RLNobexY
dSE_obexYLQi<-dSE_obexYLQ
dSE_obexYHQi<- dSE_obexYHQ
#dSe for adult females without stochasticity
dSE_RLNobexAdfi<-dSE_RLNobexAdf
dSE_obexAdfLQi<- dSE_obexAdfLQ
dSE_obexAdfHQi<-dSE_obexAdfHQ
#dSe for adult males without stochasticity
dSE_RLNobexAdmi<-dSE_RLNobexAdm
dSE_obexAdmLQi<-dSE_obexAdmLQ
dSE_obexAdmHQi<-dSE_obexAdmHQ
#Probability of positive test result given Age class, Sex and tissue type
PrYRLN<-ARy*PrRLN*dSE_RLNobexYi
PrAdfRLN<-ARadf*PrRLN*dSE_RLNobexAdfi
PrAdmRLN<-ARadm*PrRLN*dSE_RLNobexAdmi
PrYobex<-ARy*(1-PrRLN)*(PrLQv[i]*dSE_obexYLQi + (1-PrLQv[i])* dSE_obexYHQi)
PrAdfobex<-ARadf*(1-PrRLN)*(PrLQv[i]*dSE_obexAdfLQi + (1-PrLQv[i])* dSE_obexAdfHQi)
PrAdmobex<-ARadm*(1-PrRLN)*(PrLQv[i]*dSE_obexAdmLQi + (1-PrLQv[i])* dSE_obexAdmHQi)
SeY<-PrYRLN+PrYobex
SeAdf<-PrAdfRLN+PrAdfobex
SeAdm<-PrAdmRLN+PrAdmobex
#Pr_NonTestingPos
Pr_AllTestingNeg[i]<- (1 - sum(SeY,SeAdf,SeAdm)/Ntot)^(pstar*Ntot)
#SSe = Surveillance sensitivity
SSe[i] <- 1 - Pr_AllTestingNeg[i]
}
SSe
}
#N and nn is given as a matrix of simulated data (nrow=nsim)
simSSe3L_i<-function(N=N,nn=nn,k1=k,RR=RR1,pstarN=pstarN,dSE=dSe1,PrRLN=PrRLN,x1Y=x1Y,x1A=x1A,nsim=nsim){
SSe<-rep(NA,nsim)
Pr_AllTestingNeg<-SSe
#Draw random proportion of LQ obex sample from given pert-distribution
PrLQv<-rpert(n=nsim,x.min=PrLQ.min,x.max=PrLQ.max,x.mode=PrLQ.mode)
for (i in c(1:nsim)){
N1=N[3,k1,i]+N[4,k1,i]
N2=N[5,k1,i]
N3=N[6,k1,i]
Ntot=N1 + N2 + N3
pstar=round(pstarN/Ntot,4)
#Adjusted risk
ARy <- 1/ (RR[3]*(N3/Ntot)+RR[2]*(N2/Ntot) + (N1/Ntot)) #yearlings
ARadf <- RR[2]*ARy #adult females
ARadm <- RR[3]*ARy #adult males
nn1=nn[3,k1,i]+nn[4,k1,i]
nn2=nn[5,k1,i]
nn3=nn[6,k1,i]
nnv1<-ifelse(nn1<N1,nn1,N1-1)
nnv2<-ifelse(nn2<N2,nn2,N2-1)
nnv3<-ifelse(nn3<N3,nn3,N3-1)
#Draw random time since infection
tpY<-sample(x1Y,nnv1,replace=T) #yearlings
tpAdf<-sample(x1A,nnv2,replace=T) #adult females
tpAdm<-sample(x1A,nnv3,replace=T) #adult males
#dSe for yearlings
dSE_RLNobexY<-dSE[tpY,"yRLN"]
dSE_obexYLQ<-dSE[tpY,"yObexLowq"]
dSE_obexYHQ<-dSE[tpY,"yObexLowq"]
#dSe for adult females
dSE_RLNobexAdf<-dSE[tpAdf,"yRLN"]
dSE_obexAdfLQ<-dSE[tpAdf,"yObexLowq"]
dSE_obexAdfHQ<-dSE[tpAdf,"yObexHighq"]
#dSe for adult males
dSE_RLNobexAdm<-dSE[tpAdm,"yRLN"]
dSE_obexAdmLQ<-dSE[tpAdm,"yObexLowq"]
dSE_obexAdmHQ<-dSE[tpAdm,"yObexHighq"]
#dSe for yearlings with stochasticy
dSE_RLNobexYi<-IncludeSeVar(Y=dSE_RLNobexY)
dSE_obexYLQi<-IncludeSeVar(Y=dSE_obexYLQ)
dSE_obexYHQi<-IncludeSeVar(Y=dSE_obexYHQ)
#dSe for adult females with stochasticity
dSE_RLNobexAdfi<-IncludeSeVar(Y=dSE_RLNobexAdf)
dSE_obexAdfLQi<-IncludeSeVar(Y=dSE_obexAdfLQ)
dSE_obexAdfHQi<-IncludeSeVar(Y=dSE_obexAdfHQ)
#dSe for adult males with stochasticity
dSE_RLNobexAdmi<-IncludeSeVar(Y=dSE_RLNobexAdm)
dSE_obexAdmLQi<-IncludeSeVar(Y=dSE_obexAdmLQ)
dSE_obexAdmHQi<-IncludeSeVar(Y=dSE_obexAdmHQ)
#Probability of positive test result given Age class, Sex and tissue type
PrYRLN<-ARy*PrRLN*dSE_RLNobexYi
PrAdfRLN<-ARadf*PrRLN*dSE_RLNobexAdfi
PrAdmRLN<-ARadm*PrRLN*dSE_RLNobexAdmi
PrYobex<-ARy*(1-PrRLN)*(PrLQv[i]*dSE_obexYLQi + (1-PrLQv[i])* dSE_obexYHQi)
PrAdfobex<-ARadf*(1-PrRLN)*(PrLQv[i]*dSE_obexAdfLQi + (1-PrLQv[i])* dSE_obexAdfHQi)
PrAdmobex<-ARadm*(1-PrRLN)*(PrLQv[i]*dSE_obexAdmLQi + (1-PrLQv[i])* dSE_obexAdmHQi)
SeY<-PrYRLN+PrYobex
SeAdf<-PrAdfRLN+PrAdfobex
SeAdm<-PrAdmRLN+PrAdmobex
#Pr_NonTestingPos
Pr_AllTestingNeg[i]<- (1 - sum(SeY,SeAdf,SeAdm)/Ntot)^(pstar*Ntot)
#SSe = Surveillance sensitivity
SSe[i] <- 1 - Pr_AllTestingNeg[i]
}
SSe
}
#Include stochasticity in RR
simSSe3L_RRi<-function(N=N,N_sd=N_sd, nn=nn,RR=RR1,pstarN=pstarN,dSE=dSe1,PrRLN=PrRLN,x1Y=x1Y,x1A=x1A,nsim=nsim){
#n1 = nn[1] +nn[2] +nn[3]
#PrAf<-nn[2]/n1
#PrAm<-nn[3]/n1
SSe<-rep(NA,nsim)
Pr_AllTestingNeg<-SSe
#Draw random proportion of LQ obex sample from given pert-distribution
PrLQv<-rpert(n=nsim,x.min=PrLQ.min,x.max=PrLQ.max,x.mode=PrLQ.mode)
N1<-rnorm(n=nsim,mean=N[1],sd=N_sd[1])
N2<-rnorm(n=nsim,mean=N[2],sd=N_sd[2])
N3<-rnorm(n=nsim,mean=N[3],sd=N_sd[3])
N1[N1<=0]<-1
N2[N2<=0]<-1
N3[N3<=0]<-1
for (i in c(1:nsim)){
Ntot=N1[i] + N2[i] + N3[i]
pstar=round(pstarN/Ntot,4)
#Adjusted risk
ARy <- 1/ (RR[i,3]*(N3[i]/Ntot)+RR[i,2]*(N2[i]/Ntot) + (N1[i]/Ntot)) #yearlings
ARadf <- RR[i,2]*ARy #adult females
ARadm <- RR[i,3]*ARy #adult males
nnv1<-ifelse(nn[1]<N1[i],nn[1],N1[i]-1)
nnv2<-ifelse(nn[2]<N2[i],nn[2],N2[i]-1)
nnv3<-ifelse(nn[3]<N3[i],nn[3],N3[i]-1)
#Draw random time since infection
tpY<-sample(x1Y,nnv1,replace=T) #yearlings
tpAdf<-sample(x1A,nnv2,replace=T) #adult females
tpAdm<-sample(x1A,nnv3,replace=T) #adult males
#dSe for yearlings
dSE_RLNobexY<-dSE[tpY,"yRLN"]
dSE_obexYLQ<-dSE[tpY,"yObexLowq"]
dSE_obexYHQ<-dSE[tpY,"yObexLowq"]
#dSe for adult females
dSE_RLNobexAdf<-dSE[tpAdf,"yRLN"]
dSE_obexAdfLQ<-dSE[tpAdf,"yObexLowq"]
dSE_obexAdfHQ<-dSE[tpAdf,"yObexHighq"]
#dSe for adult males
dSE_RLNobexAdm<-dSE[tpAdm,"yRLN"]
dSE_obexAdmLQ<-dSE[tpAdm,"yObexLowq"]
dSE_obexAdmHQ<-dSE[tpAdm,"yObexHighq"]
#dSe for yearlings with stochasticy
dSE_RLNobexYi<-IncludeSeVar(Y=dSE_RLNobexY)
dSE_obexYLQi<-IncludeSeVar(Y=dSE_obexYLQ)
dSE_obexYHQi<-IncludeSeVar(Y=dSE_obexYHQ)
#dSe for adult females with stochasticity
dSE_RLNobexAdfi<-IncludeSeVar(Y=dSE_RLNobexAdf)
dSE_obexAdfLQi<-IncludeSeVar(Y=dSE_obexAdfLQ)
dSE_obexAdfHQi<-IncludeSeVar(Y=dSE_obexAdfHQ)
#dSe for adult males with stochasticity
dSE_RLNobexAdmi<-IncludeSeVar(Y=dSE_RLNobexAdm)
dSE_obexAdmLQi<-IncludeSeVar(Y=dSE_obexAdmLQ)
dSE_obexAdmHQi<-IncludeSeVar(Y=dSE_obexAdmHQ)
#Probability of positive test result given Age class, Sex and tissue type
PrYRLN<-ARy*PrRLN*dSE_RLNobexYi
PrAdfRLN<-ARadf*PrRLN*dSE_RLNobexAdfi
PrAdmRLN<-ARadm*PrRLN*dSE_RLNobexAdmi
PrYobex<-ARy*(1-PrRLN)*(PrLQv[i]*dSE_obexYLQi + (1-PrLQv[i])* dSE_obexYHQi)
PrAdfobex<-ARadf*(1-PrRLN)*(PrLQv[i]*dSE_obexAdfLQi + (1-PrLQv[i])* dSE_obexAdfHQi)
PrAdmobex<-ARadm*(1-PrRLN)*(PrLQv[i]*dSE_obexAdmLQi + (1-PrLQv[i])* dSE_obexAdmHQi)
SeY<-PrYRLN+PrYobex
SeAdf<-PrAdfRLN+PrAdfobex
SeAdm<-PrAdmRLN+PrAdmobex
#Pr_NonTestingPos
Pr_AllTestingNeg[i]<- (1 - sum(SeY,SeAdf,SeAdm)/Ntot)^(pstar*Ntot)
#SSe = Surveillance sensitivity
SSe[i] <- 1 - Pr_AllTestingNeg[i]
}
SSe
}
#Include stochasticity in RR
#N and nn is given as a matrix of simulated data (nrow=nsim)
simSSe3LmeanDSE_RRi<-function(N=N,nn=nn,k1=k,RR=RR1,pstarN=pstarN,dSE=dSe1,PrRLN=PrRLN,x1Y=x1Y,x1A=x1A,nsim=nsim){
#RR1 is a matrix with nsim rows and 3 columns with relativ risk of Yearlings, adult females and adult males versus yearlings.
SSe<-rep(NA,nsim)
Pr_AllTestingNeg<-SSe
#Draw random proportion of LQ obex sample from given pert-distribution
PrLQv<-rpert(n=nsim,x.min=PrLQ.min,x.max=PrLQ.max,x.mode=PrLQ.mode)
for (i in c(1:nsim)){
N1=N[3,k1,i]+N[4,k1,i]
N2=N[5,k1,i]
N3=N[6,k1,i]
Ntot=N1 + N2 + N3
pstar=round(pstarN/Ntot,4)
#Adjusted risk
ARy <- 1/ (RR[i,3]*(N3/Ntot)+RR[i,2]*(N2/Ntot) + (N1/Ntot)) #yearlings
ARadf <- RR[i,2]*ARy #adult females
ARadm <- RR[i,3]*ARy #adult males
nn1=nn[3,k1,i]+nn[4,k1,i]
nn2=nn[5,k1,i]
nn3=nn[6,k1,i]
nnv1<-ifelse(nn1<N1,nn1,N1-1)
nnv2<-ifelse(nn2<N2,nn2,N2-1)
nnv3<-ifelse(nn3<N3,nn3,N3-1)
#Draw random time since infection
tpY<-sample(x1Y,nnv1,replace=T) #yearlings
tpAdf<-sample(x1A,nnv2,replace=T) #adult females
tpAdm<-sample(x1A,nnv3,replace=T) #adult males
#dSe for yearlings
dSE_RLNobexY<-dSE[tpY,"yRLN"]
dSE_obexYLQ<-dSE[tpY,"yObexLowq"]
dSE_obexYHQ<-dSE[tpY,"yObexLowq"]
#dSe for adult females
dSE_RLNobexAdf<-dSE[tpAdf,"yRLN"]
dSE_obexAdfLQ<-dSE[tpAdf,"yObexLowq"]
dSE_obexAdfHQ<-dSE[tpAdf,"yObexHighq"]
#dSe for adult males
dSE_RLNobexAdm<-dSE[tpAdm,"yRLN"]
dSE_obexAdmLQ<-dSE[tpAdm,"yObexLowq"]
dSE_obexAdmHQ<-dSE[tpAdm,"yObexHighq"]
#dSe for yearlings without stochasticy
dSE_RLNobexYi<-dSE_RLNobexY
dSE_obexYLQi<-dSE_obexYLQ
dSE_obexYHQi<- dSE_obexYHQ
#dSe for adult females without stochasticity
dSE_RLNobexAdfi<-dSE_RLNobexAdf
dSE_obexAdfLQi<- dSE_obexAdfLQ
dSE_obexAdfHQi<-dSE_obexAdfHQ
#dSe for adult males without stochasticity
dSE_RLNobexAdmi<-dSE_RLNobexAdm
dSE_obexAdmLQi<-dSE_obexAdmLQ
dSE_obexAdmHQi<-dSE_obexAdmHQ
#Probability of positive test result given Age class, Sex and tissue type
PrYRLN<-ARy*PrRLN*dSE_RLNobexYi
PrAdfRLN<-ARadf*PrRLN*dSE_RLNobexAdfi
PrAdmRLN<-ARadm*PrRLN*dSE_RLNobexAdmi
PrYobex<-ARy*(1-PrRLN)*(PrLQv[i]*dSE_obexYLQi + (1-PrLQv[i])* dSE_obexYHQi)
PrAdfobex<-ARadf*(1-PrRLN)*(PrLQv[i]*dSE_obexAdfLQi + (1-PrLQv[i])* dSE_obexAdfHQi)
PrAdmobex<-ARadm*(1-PrRLN)*(PrLQv[i]*dSE_obexAdmLQi + (1-PrLQv[i])* dSE_obexAdmHQi)
SeY<-PrYRLN+PrYobex
SeAdf<-PrAdfRLN+PrAdfobex
SeAdm<-PrAdmRLN+PrAdmobex
#Pr_NonTestingPos
Pr_AllTestingNeg[i]<- (1 - sum(SeY,SeAdf,SeAdm)/Ntot)^(pstar*Ntot)
#SSe = Surveillance sensitivity
SSe[i] <- 1 - Pr_AllTestingNeg[i]
}
SSe
}