Last active
June 17, 2022 22:10
-
-
Save turgeonmaxime/362a219d7f13656d48e46e4afbb122f5 to your computer and use it in GitHub Desktop.
Sensitivity and specificity are not properties of the test, they depend on the population
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
expit <- function(t) exp(t)/(1 + exp(t)) | |
n <- 1000000 | |
prev_vec <- c(0.01, 0.05, 0.1, 0.25, 0.5) | |
results <- purrr::map_df(prev_vec, \(prev) { | |
# Generate data | |
dvec <- rbinom(n, prob = prev, size = 1) | |
xvec <- rnorm(n, 2*dvec - 1) | |
yvec <- ifelse(xvec > 0, 1, 0) | |
sel_vec <- rbinom(n, prob = expit(xvec), | |
size = 1) | |
# Without selection | |
sens <- sum(dvec & yvec)/sum(dvec) | |
spec <- sum(!dvec & !yvec)/sum(!dvec) | |
# With selection | |
sens2 <- sum(dvec & yvec & sel_vec)/sum(dvec & sel_vec) | |
spec2 <- sum(!dvec & !yvec & sel_vec)/sum(!dvec & sel_vec) | |
tribble( | |
~metric, ~no_select, ~select, | |
"Sens", sens, sens2, | |
"Spec", spec, spec2 | |
) |> mutate(prev = prev) | |
}) | |
results |> | |
pivot_longer(cols = c("no_select", "select"), | |
names_to = "Selection", | |
values_to = "Value") |> | |
ggplot(aes(prev, Value, colour = Selection)) + | |
geom_point() + | |
geom_line() + | |
facet_grid(~metric) + | |
cowplot::theme_minimal_hgrid() + | |
scale_y_continuous(limits = c(0, 1)) + | |
xlab("Prevalence") + ylab("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
People typically assume that sensitivity and specificity are properties of a diagnostic test, but they actually depend on the population. This small simulation study looks at this.
We have a simple data generating mechanism:
Finally, we compare two scenarios, one where selection depends on X, and one where there is no selection. We assume a simple model where the selection probability is the inverse logit of X, so that larger values of X are more likely to be selected.
This is the output of the code above:
![image](https://user-images.githubusercontent.com/6857788/174406176-72eec71b-9c57-44b9-8e79-90f6e2766980.png)