# Primarycensoreddist: Primary Event Censored Distributions in R and Stan

**URL:** <https://community.epinowcast.org/t/primarycensoreddist-primary-event-censored-distributions-in-r-and-stan/245>\
**Category:** Uncategorized\
**Tags:** package-extension\
**Created:** [4 September 2024 21:55 UTC](https://community.epinowcast.org/t/primarycensoreddist-primary-event-censored-distributions-in-r-and-stan/245 "2024-09-04T21:55:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samabbott](https://dub1.discourse-cdn.com/flex005/user_avatar/community.epinowcast.org/samabbott/32/5_2.png) [@samabbott](https://community.epinowcast.org/u/samabbott)\
**Post date:** [4 September 2024 21:55 UTC](https://community.epinowcast.org/t/primarycensoreddist-primary-event-censored-distributions-in-r-and-stan/245/1 "2024-09-04T21:55:12Z")

</div>

New package that aims to replace `epinowcast::simulate_double_censored()`, the current approaches to handling pmfs in the various convolution packages we support, and provides likelihoods for fitting censored and truncated delays inspired by some of the work @sambrand and I (mostly Sam) are doing in [`EpiAware`](https://cdcgov.github.io/Rt-without-renewal/dev/). Its also very heavily based on all the work that @sangwoopark @kcharniga2 and @athowes have been doing and hopefully will feed into more similar work.

> **[Primary Event Censored Distributions in R and Stan](https://primarycensored.epinowcast.org/)**
>
> This package provides both R functions for working with primary event censored distributions and Stan implementations for use in Bayesian modeling. Primary event censored distributions are useful for modeling delayed reporting scenarios in...

It’s still early stages but I expect the R interface to be stable. Seeing some numerical issues in the grad calls when using the models for fitting that mean may need to change up the stan side interface a bit (see the issues for more discussion).

This also represents some thinking about [Streamlining of epi modeling tools - #13 by adrianlison](https://community.epinowcast.org/t/streamlining-of-epi-modeling-tools/241/13) in the sense that ideally this could be used as a dependency to provide stan code for calculating PMFs across packages in a more “correct” way. The initial targets for that would be `epinowcast`, `epidist`, and `EpiNow2` so developers from those packages’ thoughts are particularly useful.

Lots of rough edges so feedback (ideally via issues) very welcome. Contributions are also very very welcome as ever.

There is also an issue for anyone who fancies dealing with solving some integrals analytically which could be fun and improve performance.

---

<div class="post-metadata">

**Author:** ![samabbott](https://dub1.discourse-cdn.com/flex005/user_avatar/community.epinowcast.org/samabbott/32/5_2.png) [@samabbott](https://community.epinowcast.org/u/samabbott)\
**Post date:** [18 September 2024 09:53 UTC](https://community.epinowcast.org/t/primarycensoreddist-primary-event-censored-distributions-in-r-and-stan/245/2 "2024-09-18T09:53:29Z")

</div>

There is now a `0.3.0` release that has stable stan likelihoods and model wrappers for both `fitdistplus` and `stan`. There is also a fairly complete set of documentation to walk through the package functionality.

The next release is targeting integrating analytical solutions (from @sambrand ) for common uses cases which should give a nice speed bump vs the more generic numerical approach.

---

<div class="post-metadata">

**Author:** ![samabbott](https://dub1.discourse-cdn.com/flex005/user_avatar/community.epinowcast.org/samabbott/32/5_2.png) [@samabbott](https://community.epinowcast.org/u/samabbott)\
**Post date:** [22 September 2024 12:49 UTC](https://community.epinowcast.org/t/primarycensoreddist-primary-event-censored-distributions-in-r-and-stan/245/3 "2024-09-22T12:49:15Z")

</div>

`0.5.0` out now with analytical solutions implemented for lognormal and gamma with uniform primary events (thanks to @sambrand).

Fit time (on the non-trivial example) is down to 1 second with headroom for more (via directly solving the pmf)

---

<div class="post-metadata">

**Author:** ![samabbott](https://dub1.discourse-cdn.com/flex005/user_avatar/community.epinowcast.org/samabbott/32/5_2.png) [@samabbott](https://community.epinowcast.org/u/samabbott)\
**Post date:** [5 November 2024 05:10 UTC](https://community.epinowcast.org/t/primarycensoreddist-primary-event-censored-distributions-in-r-and-stan/245/4 "2024-11-05T05:10:27Z")

</div>

primarycensored is now on CRAN and stable. @sambrand and co are working to get a julia version off the ground with matching features here [GitHub - EpiAware/PrimaryCensored.jl: Primary censoring extension for Distributions.jl](https://github.com/epiaware/primarycensored.jl)
