# BayesPIM: Bayesian prevalence-incidence mixture model

<!-- badges: start -->

<!-- badges: end -->

BayesPIM fits prevalence-incidence mixture (PIM) models to screening and
surveillance data, as described in Klausch et al. (2026). It models time
to incidence with an accelerated failure time specification, latent
prevalence at baseline with a probit model, and imperfect test
sensitivity, using a Bayesian Gibbs sampler. For full usage see
`vignette("BayesPIM_intro")`.

## Installation

``` r
install.packages("BayesPIM")
```

Or the development version from
[GitHub](https://github.com/thomasklausch2/BayesPIM):

``` r
# install.packages("devtools")
devtools::install_github("thomasklausch2/BayesPIM", build_vignettes = TRUE)
```

## Example

Simulate screening data and fit the model. The default sampler is the
collapsed slice sampler, which augments only the latent screening
interval.

``` r
library(BayesPIM)

set.seed(2025)
dat <- gen_data(
  kappa = 0.7,            # test sensitivity
  n = 1e3, theta = 0.2,   # sample size and baseline prevalence
  p = 1, p_discrete = 1,
  beta_t = c(0.2, 0.2), beta_g = c(0.2, 0.2),
  v_min = 20, v_max = 30, mean_rc = 80,
  sigma_t = 0.2, mu_t = 5, dist = "weibull", prob_r = 1
)

mod <- bayespim(
  v_obs = dat$v_obs,
  x_t = dat$x, x_g = dat$x, r = dat$r,
  kappa = 0.7, update_kappa = FALSE,
  dist = "weibull",             # also lognormal, loglog, gamma, gengamma
  sampler = "slice_collapsed",  # also "slice" and "mh"
  ndraws = 1e3, warmup = 5e2,
  chains = 4, seed_chains = 1:4,
  update_till_converge = TRUE   # extend sampling until R-hat and ESS are met
)
```

Convergence diagnostics are printed while fitting and stored in
`mod$convergence`. Posterior summaries and trace plots come from the
`summary()` and `plot()` methods:

``` r
summary(mod)              # posterior quantiles, R-hat and ESS per block
plot(mod, thinning = 5)   # trace and density plots
```

Compare incidence distributions with information criteria, and obtain
posterior predictive cumulative incidence functions:

``` r
get_ic(mod, samples = 1e3, cores = 2)   # WAIC-1, WAIC-2 and DIC

cif <- ppCIF(mod, pst_samples = 1e3, quant = seq(0, 300, length.out = 601))
plot(cif, type = "both")   # mixture and non-prevalent CIF
```

## References

T. Klausch, B. I. Lissenberg-Witte, and V. M. H. Coupé (2026). “A
Bayesian prevalence-incidence mixture model for screening outcomes with
misclassification.”, Statistics in Medicine, 45(8-9), e70433.
<doi:10.1002/sim.70433>
