@@ -87,13 +87,30 @@ viewof specificity = Inputs.range([50, 100], {
87
87
grid = {
88
88
const totalDots = 100;
89
89
const diseaseCount = Math.round(totalDots * (prevalence / 100));
90
-
90
+ const noDiseaseCount = totalDots - diseaseCount;
91
+
91
92
const expectedTruePositives = Math.round(diseaseCount * (sensitivity / 100));
92
- const expectedFalsePositives = Math.round((totalDots - diseaseCount) * ((100 - specificity) / 100));
93
-
93
+ const expectedFalsePositives = Math.round(noDiseaseCount * ((100 - specificity) / 100));
94
+
95
+ // Create an array of indices for the population
96
+ const indices = Array.from({ length: totalDots }, (_, index) => index);
97
+
98
+ // Shuffle the indices randomly
99
+ const shuffledIndices = indices.sort(() => Math.random() - 0.5);
100
+
101
+ // Assign disease status based on the first `diseaseCount` indices
102
+ const hasDisease = new Set(shuffledIndices.slice(0, diseaseCount));
103
+
104
+ // Separate true positives and false positives
105
+ const truePositiveIndices = Array.from(hasDisease).slice(0, expectedTruePositives);
106
+ const falsePositiveIndices = shuffledIndices
107
+ .filter(index => !hasDisease.has(index))
108
+ .slice(0, expectedFalsePositives);
109
+
110
+ // Create the dots array with proper properties
94
111
const dots = Array(totalDots).fill(null).map((_, index) => ({
95
- hasDisease: index < diseaseCount ,
96
- testPositive: index < (expectedTruePositives + expectedFalsePositives)
112
+ hasDisease: hasDisease.has( index) ,
113
+ testPositive: truePositiveIndices.includes( index) || falsePositiveIndices.includes(index),
97
114
}));
98
115
99
116
// Calculate statistics
0 commit comments