-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_group_tests.R
178 lines (134 loc) · 5.53 KB
/
sim_group_tests.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
# De vijf parameters hieronder zijn aan te passen
# Bij grote aantallen runs 'verbose' op 0 zetten, anders duurt het erg lang
#
# 2x2 abbreviations:
#
# condition positive (CP) condition negative (CN)
#
# predicted positive (PP) true positive (TP) false positive (FP)
#
# predicted negative (PN) false negative (FN) true negative (TN)
#
#
# parameters, arbitrair gekozen:
group_size <- 50 # individuals per initial blood test
positive_split <- 2 # split into x subgroups when group is positive
runs <- 1000 # how many groups to simulate
prevalence <- .02 # infection prevalence
sens <- .70 # test sensitivity
spec <- .95 # test specificity
verbose <- 0 # show simulation output (2 = all, 1 = some, 0 = none)
#===========================================
# code below
# print function with verbosity
vcat <- function(level = 1, ...) {
if(level <= verbose) {
cat(...)
}
}
# function to test one (sub)group
test_group <- function(samples, level = 0) {
# print text
spacer <- paste0(" ", strrep(" ", level))
vcat(1, spacer,"testing",length(samples),"people: ")
# keep count of how many tests we're using
tests <<- tests+1
# true outcome would be if "samples" contains at least one 1,
# then take sens and spec into account
true_condition <- sum(samples) > 0
if(true_condition) outcome <- rbinom(1,1,sens) == 1
else outcome <- rbinom(1,1,spec) == 0
# if the test result is positive:
if(outcome) {
if(true_condition) vcat(1, "true positive ✅☣️\n")
else vcat(1, "false positive ❗️☣️\n")
# split into subgroups
if(length(samples) > 1) {
subgroups <- split(samples, ceiling(seq(length(samples))/(length(samples)/positive_split)))
# test every subgroup and collect results
ret <- c()
for(g in subgroups) {
vcat(2, spacer,"split:",g,"\n")
ret <- append(ret,test_group(g, level+1))
}
# return results
return(ret)
} else {
# return status 1 for current person
return(c(1))
}
# if the test result is negative:
} else {
if(true_condition) vcat(1, "false negative ❗️✅️\n")
else vcat(1, "true negative ✅✅️\n")
# return outcome 0 for all people in sample
return(rep(0,length(samples)))
}
}
# print info
total_people <- group_size*runs
cat("# Testing",total_people,"people in",runs,"groups of",group_size,"and splitting in",positive_split,"subgroups when positive\n")
# count number of tests
tests <- 0
# store results
results <- c(0,0,0,0) # TP, FP, FN, TN
# generate sample populations and start tests
for(i in 1:runs) {
samples <- rbinom(group_size, 1, prevalence) # create a group of random samples (0 = not infected, 1 = infected)
vcat(1,"\nnew group:",group_size,"people,",sum(samples),"true positive(s)\n") # show spoiler: true number of cases in group
outcome <- test_group(samples) # start testing this group
vcat(1,"true condition:",samples,"\n")
vcat(1,"test result: ",outcome,"\n")
TP <- sum(outcome == TRUE & samples == outcome)
FP <- sum(outcome == TRUE & samples != outcome)
FN <- sum(outcome == FALSE & samples != outcome)
TN <- sum(outcome == FALSE & samples == outcome)
results <- results + c(TP, FP, FN, TN)
}
# print result
cat("\n# Results after everybody was tested once:\n")
cat("Predicted Positive: ",results[1]+results[2],"\n")
cat("Predicted Negative: ",results[3]+results[4],"\n\n")
cat("True Positives: ",results[1],"\n")
cat("False Positives:",results[2],"\n")
cat("False Negatives:",results[3],"\n")
cat("True Negatives: ",results[4],"\n\n")
#
# Second run of tests
#
# store results
results2 <- c(results[1],results[2],0,0) # TP, FP, FN, TN
# second test for negative people
cat("\n# Testing negative-tested people again, groupwise")
total <- results[3]+results[4]
pos <- results[3]
groups <- ceiling(total/group_size)
for(i in 1:groups) {
size <- min(group_size, total)
samples <- rbinom(size, 1, pos/total) # create a group of random samples (0 = not infected, 1 = infected)
vcat(1,"\nnew group: ",size," people,",sum(samples),"true positive(s)\n") # show spoiler: true number of cases in group
total <- total - size
pos <- pos - sum(samples)
outcome <- test_group(samples)
vcat(1,"true condition:",samples,"\n")
vcat(1,"test result: ",outcome,"\n")
TP <- sum(outcome == TRUE & samples == outcome)
FP <- sum(outcome == TRUE & samples != outcome)
FN <- sum(outcome == FALSE & samples != outcome)
TN <- sum(outcome == FALSE & samples == outcome)
results2 <- results2 + c(TP, FP, FN, TN)
}
# print result
cat("\n# Results after those testing negative were tested again:\n")
cat("# (those tested postive still counted as positive)\n")
cat("Predicted Positive: ",results2[1]+results2[2],"\n")
cat("Predicted Negative: ",results2[3]+results2[4],"\n\n")
cat("True Positives: ",results2[1],"\n")
cat("False Positives:",results2[2],"\n")
cat("False Negatives:",results2[3],"\n")
cat("True Negatives: ",results2[4],"\n\n")
cat("Sensitivity: ",results2[1]/(results2[1]+results2[3]),"\n")
cat("Specificity: ",results2[4]/(results2[2]+results2[4]),"\n")
cat("PPV: ",results2[1]/(results2[1]+results2[2]),"\n")
cat("NPV: ",results2[4]/(results2[3]+results2[4]),"\n\n")
cat("Required",tests,'tests for',total_people,"people\n= 1 test per",total_people/tests,"diagnosed persons\n= 1 test per",(results2[3]+results2[4])/tests,"persons back to work\n")