devtools::load_all()
#> ℹ Loading testerror
#> ℹ Re-compiling testerror (debug build)
#> ── R CMD INSTALL ───────────────────────────────────────────────────────────────
#> * installing *source* package ‘testerror’ ...
#> ** using staged installation
#> * DONE (testerror)
library(tidyverse)
#> ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
#> ✔ dplyr 1.1.4 ✔ readr 2.1.5
#> ✔ forcats 1.0.0 ✔ stringr 1.5.1
#> ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
#> ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
#> ✔ purrr 1.0.2
#> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ readr::edition_get() masks testthat::edition_get()
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ purrr::is_null() masks testthat::is_null()
#> ✖ dplyr::lag() masks stats::lag()
#> ✖ readr::local_edition() masks testthat::local_edition()
#> ✖ dplyr::matches() masks tidyr::matches(), testthat::matches()
#> ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(testerror)
options(mc.cores = 2) # parallel::detectCores())
rstan::rstan_options(auto_write = FALSE)
here::i_am("vignettes/testerror.Rmd")
#> here() starts at /tmp/RtmpG0SwlU/Rbuild1bc0719e6483/testerror
source(here::here("vignettes/formatting.R"))
binax_pos = 26
binax_n = 786
# Fit beta distributions to sinclair 2013 data:
# We apply a widening to increase uncertainty in sensitivity
binax_sens = beta_params(median = 0.740, lower = 0.666, upper = 0.823, widen = 5)
binax_spec = beta_params(median = 0.972, lower= 0.927, upper = 0.998)
tmp1 = testerror::bayesian_true_prevalence_model(
pos_obs = binax_pos,
n_obs = binax_n,
sens = binax_sens,
spec = binax_spec,
model_type = "logit"
)
#> recompiling to avoid crashing R session
#> Warning: There were 96 divergent transitions after warmup. See
#> https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
#> to find out why this is a problem and how to eliminate them.
#> Warning: Examine the pairs() plot to diagnose sampling problems
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess
binax_summ = tmp1$summary %>%
dplyr::transmute(
Test = "Binax",
Positivity = sprintf("%d/%d (%1.2f%%)", binax_pos, binax_n, binax_pos/binax_n*100),
`Estimated prevalence` = prevalence.label,
`Sensitivity` = sens.label,
`Specificity` = spec.label,
`Method` = prevalence.method
)
binax_summ %>% default_table()
Test | Positivity | Estimated prevalence | Sensitivity | Specificity | Method |
---|---|---|---|---|---|
Binax | 26/786 (3.31%) | 0.39% [0.00% — 3.75%] | 75.17% [53.02% — 89.31%] | 97.24% [95.67% — 99.01%] | bayes (logit) |
For this description we are going to assume we have only count data for panel and components, and we have limited information about component tests.
# data from Forstner et al (2019)
components = tibble::tibble(
serotype = factor(c("1", "3", "4", "5", "6A", "6B", "7F", "9V", "14", "18C", "19A", "19F", "23F")),
pos = c(2, 30, 2, 1, 4, 0, 7, 1, 1, 3, 2, 3, 4),
n = 796
)
uad1_pos = 59
uad1_n = 796
# control group data
# negatives at
uad1_spec = spec_prior() %>%
# data from Pride et al
update_posterior(0,17)
# Datd from Pride et al
uad1_controls = tibble::tibble(
serotype = factor(c("1", "3", "4", "5", "6A", "6B", "7F", "9V", "14", "18C", "19A", "19F", "23F")),
false_neg_diseased = c(0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
n_diseased = c(7, 1, 3, 1, 1, 0, 4, 2, 7, 0, 6, 0, 2),
# In the design of UAD1 the cut off is calibrated on 400 negative samples and
# set to make 2 positive
# Forstner et al (2019) control group
# In the control group of 397 non-CAP patients, 1 patient had to
# be excluded because of indeterminable results (0.3%, see Fig. 1)
# and SSUAD was positive in 3 patients (0.8%). Of those 3 non-CAP
# patients, 2 were positive for serotype 18C and 1 person for two
# pneumococcal serotypes (5, 7F).
false_pos_controls = 2+c(0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 2L, 0L, 0L, 0L),
n_controls = 400+396
)
uad1_sens = sens_prior() %>%
# data from Pride et al
update_posterior(17,17)
With this we can use a bayesian model to construct adjusted estimates
corr3 = testerror::bayesian_panel_true_prevalence_model(
panel_pos_obs = uad1_pos,
panel_n_obs = uad1_n,
panel_name = "PCV13",
pos_obs = components$pos,
n_obs = components$n,
test_names = components$serotype,
false_pos_controls = uad1_controls$false_pos_controls,
n_controls = uad1_controls$n_controls,
false_neg_diseased = uad1_controls$false_neg_diseased,
n_diseased = uad1_controls$n_diseased,
panel_sens = uad1_sens,
model_type = "logit"
)
#> recompiling to avoid crashing R session
#> 5.103 seconds (Total)
#> Chain 1:
corr3$summary %>%
mutate(
pos_obs = c(components$pos, uad1_pos),
n_obs = c(components$n, uad1_n)
) %>%
dplyr::transmute(
Test = test,
Positivity = sprintf("%d/%d (%1.2f%%)", pos_obs, n_obs, pos_obs/n_obs*100),
`Estimated prevalence` = prevalence.label,
`Sensitivity` = sens.label,
`Specificity` = spec.label,
`Method` = prevalence.method
) %>%
bind_rows(binax_summ) %>%
default_table() %>% huxtable::set_top_border(row = huxtable::final(2), col=huxtable::everywhere, value = 1)
Test | Positivity | Estimated prevalence | Sensitivity | Specificity | Method |
---|---|---|---|---|---|
1 | 2/796 (0.25%) | 0.03% [0.00% — 0.42%] | 97.45% [77.32% — 99.92%] | 99.81% [99.50% — 99.96%] | bayes (logit) |
3 | 30/796 (3.77%) | 5.46% [2.93% — 20.38%] | 62.47% [16.70% — 96.77%] | 99.80% [99.29% — 99.97%] | bayes (logit) |
4 | 2/796 (0.25%) | 0.03% [0.00% — 0.46%] | 96.19% [61.35% — 99.91%] | 99.81% [99.51% — 99.97%] | bayes (logit) |
5 | 1/796 (0.13%) | 0.01% [0.00% — 0.33%] | 93.59% [28.35% — 99.87%] | 99.79% [99.47% — 99.94%] | bayes (logit) |
6A | 4/796 (0.50%) | 0.16% [0.00% — 0.99%] | 94.09% [33.84% — 99.87%] | 99.76% [99.39% — 99.96%] | bayes (logit) |
6B | 0/796 (0.00%) | 0.01% [0.00% — 0.29%] | 88.52% [7.55% — 99.83%] | 99.91% [99.67% — 99.99%] | bayes (logit) |
7F | 7/796 (0.88%) | 0.33% [0.00% — 1.29%] | 96.50% [66.04% — 99.92%] | 99.61% [99.11% — 99.91%] | bayes (logit) |
9V | 1/796 (0.13%) | 0.02% [0.00% — 0.31%] | 95.14% [49.07% — 99.89%] | 99.85% [99.58% — 99.97%] | bayes (logit) |
14 | 1/796 (0.13%) | 0.01% [0.00% — 0.29%] | 97.41% [76.37% — 99.92%] | 99.85% [99.59% — 99.98%] | bayes (logit) |
18C | 3/796 (0.38%) | 0.02% [0.00% — 0.65%] | 87.62% [8.80% — 99.87%] | 99.62% [99.22% — 99.86%] | bayes (logit) |
19A | 2/796 (0.25%) | 0.03% [0.00% — 0.41%] | 97.31% [73.45% — 99.92%] | 99.81% [99.49% — 99.96%] | bayes (logit) |
19F | 3/796 (0.38%) | 0.07% [0.00% — 0.94%] | 89.08% [9.69% — 99.86%] | 99.77% [99.41% — 99.96%] | bayes (logit) |
23F | 4/796 (0.50%) | 0.14% [0.00% — 0.88%] | 95.17% [48.86% — 99.90%] | 99.75% [99.37% — 99.96%] | bayes (logit) |
PCV13 | 59/796 (7.41%) | 6.69% [4.15% — 21.11%] | 68.11% [23.90% — 94.95%] | 96.90% [95.91% — 97.81%] | bayes (logit) |
Binax | 26/786 (3.31%) | 0.39% [0.00% — 3.75%] | 75.17% [53.02% — 89.31%] | 97.24% [95.67% — 99.01%] | bayes (logit) |
# senstivity / specificity from Kakiuchi et al
kak_uad1_sens = beta_params(median = 0.741, lower = 0.537, upper = 0.889)
kak_uad1_spec = beta_params(median = 0.954, lower = 0.917, upper = 0.978) %>%
# data from Pride et al
update_posterior(17,17)
corr4 = testerror::bayesian_panel_true_prevalence_model(
panel_pos_obs = uad1_pos,
panel_n_obs = uad1_n,
panel_name = "PCV13",
pos_obs = components$pos,
n_obs = components$n,
test_names = components$serotype,
false_pos_controls = uad1_controls$false_pos_controls,
n_controls = uad1_controls$n_controls,
false_neg_diseased = uad1_controls$false_neg_diseased,
n_diseased = uad1_controls$n_diseased,
panel_sens = kak_uad1_sens,
panel_spec = kak_uad1_spec,
model_type="logit"
)
#> recompiling to avoid crashing R session
#> recompiling to avoid crashing R session
#> : 2.277 seconds (Total)
#> Chain 2:
#> Warning: There were 1 divergent transitions after warmup. See
#> https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
#> to find out why this is a problem and how to eliminate them.
#> Warning: Examine the pairs() plot to diagnose sampling problems
corr4$summary %>%
mutate(
pos_obs = c(components$pos, uad1_pos),
n_obs = c(components$n, uad1_n)
) %>%
dplyr::transmute(
Test = test,
Positivity = sprintf("%d/%d (%1.2f%%)", pos_obs, n_obs, pos_obs/n_obs*100),
`Estimated prevalence` = prevalence.label,
`Sensitivity` = sens.label,
`Specificity` = spec.label,
`Method` = prevalence.method
) %>% bind_rows(
binax_summ
) %>%
default_table() %>% huxtable::set_top_border(row = huxtable::final(2), col=huxtable::everywhere, value = 1)
Test | Positivity | Estimated prevalence | Sensitivity | Specificity | Method |
---|---|---|---|---|---|
1 | 2/796 (0.25%) | 0.02% [0.00% — 0.42%] | 97.58% [77.84% — 99.91%] | 99.80% [99.49% — 99.96%] | bayes (logit) |
3 | 30/796 (3.77%) | 5.00% [3.18% — 8.45%] | 67.03% [42.16% — 88.34%] | 99.78% [99.22% — 99.97%] | bayes (logit) |
4 | 2/796 (0.25%) | 0.02% [0.00% — 0.42%] | 96.11% [57.89% — 99.90%] | 99.79% [99.49% — 99.96%] | bayes (logit) |
5 | 1/796 (0.13%) | 0.01% [0.00% — 0.31%] | 93.78% [37.48% — 99.87%] | 99.78% [99.46% — 99.94%] | bayes (logit) |
6A | 4/796 (0.50%) | 0.14% [0.00% — 1.02%] | 94.09% [30.72% — 99.88%] | 99.73% [99.35% — 99.95%] | bayes (logit) |
6B | 0/796 (0.00%) | 0.01% [0.00% — 0.29%] | 88.01% [7.92% — 99.83%] | 99.90% [99.65% — 99.99%] | bayes (logit) |
7F | 7/796 (0.88%) | 0.30% [0.00% — 1.25%] | 96.49% [65.80% — 99.91%] | 99.56% [99.08% — 99.89%] | bayes (logit) |
9V | 1/796 (0.13%) | 0.02% [0.00% — 0.34%] | 95.12% [47.93% — 99.90%] | 99.85% [99.57% — 99.97%] | bayes (logit) |
14 | 1/796 (0.13%) | 0.01% [0.00% — 0.30%] | 97.44% [76.86% — 99.93%] | 99.85% [99.56% — 99.97%] | bayes (logit) |
18C | 3/796 (0.38%) | 0.02% [0.00% — 0.65%] | 88.56% [9.47% — 99.83%] | 99.60% [99.20% — 99.85%] | bayes (logit) |
19A | 2/796 (0.25%) | 0.02% [0.00% — 0.45%] | 97.36% [73.60% — 99.94%] | 99.80% [99.48% — 99.96%] | bayes (logit) |
19F | 3/796 (0.38%) | 0.07% [0.00% — 1.01%] | 88.92% [10.65% — 99.88%] | 99.76% [99.41% — 99.96%] | bayes (logit) |
23F | 4/796 (0.50%) | 0.14% [0.00% — 0.86%] | 95.17% [51.55% — 99.88%] | 99.73% [99.33% — 99.96%] | bayes (logit) |
PCV13 | 59/796 (7.41%) | 6.02% [4.24% — 9.10%] | 71.67% [51.37% — 86.38%] | 96.67% [95.69% — 97.53%] | bayes (logit) |
Binax | 26/786 (3.31%) | 0.39% [0.00% — 3.75%] | 75.17% [53.02% — 89.31%] | 97.24% [95.67% — 99.01%] | bayes (logit) |