Skip to content

Commit c64d43c

Browse files
authored
feat: make nsim_offspring explicit in likelihood()
- Add nsim_offspring = 100 as explicit parameter to likelihood() - Add @param nsim_offspring documentation - Validate nsim_offspring with checkmate::assert_integerish - Pass nsim_offspring explicitly to .offspring_ll() instead of via ... - Add example showing nsim_offspring usage with custom distribution - Update vignette to clarify nsim_offspring is a named argument
1 parent d5e6054 commit c64d43c

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

R/likelihood.R

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#' @param nsim_obs Number of simulations to be used to approximate the
1010
#' log-likelihood/likelihood if `obs_prob < 1` (imperfect observation). If
1111
#' `obs_prob == 1`, this argument is ignored.
12+
#' @param nsim_offspring Number of simulations of the offspring distribution
13+
#' to use to approximate the distribution of the chain statistic summary
14+
#' (size/length). This is only used when `offspring_dist` does not have a
15+
#' closed-form likelihood (i.e., it is not one of the distributions for which
16+
#' `{epichains}` has an analytical solution). Defaults to `100`.
1217
#' @param obs_prob Observation probability. A number (probability) between 0
1318
#' and 1. A value greater than 0 but less 1 implies imperfect observation and
1419
#' simulations will be used to approximate the (log)likelihood. This will
@@ -102,11 +107,25 @@
102107
#' size = 0.2
103108
#' )
104109
#' epichains_summary_eg_lik
110+
#'
111+
#' # Example using a custom (non-built-in) offspring distribution
112+
#' # that requires simulation-based likelihood approximation
113+
#' set.seed(121)
114+
#' chain_sizes <- sample(1:10, 20, replace = TRUE)
115+
#' likelihood(
116+
#' chains = chain_sizes,
117+
#' statistic = "size",
118+
#' offspring_dist = rbinom,
119+
#' nsim_offspring = 100,
120+
#' size = 1,
121+
#' prob = 0.9
122+
#' )
105123
#' @export
106124
# nolint start: cyclocomp_linter
107125
likelihood <- function(chains, statistic = c("size", "length"), offspring_dist,
108126
nsim_obs, obs_prob = 1, stat_threshold = Inf, log = TRUE,
109-
exclude = NULL, individual = FALSE, ...) {
127+
exclude = NULL, individual = FALSE, nsim_offspring = 100,
128+
...) {
110129
statistic <- match.arg(statistic)
111130

112131
## Input checking
@@ -144,6 +163,10 @@ likelihood <- function(chains, statistic = c("size", "length"), offspring_dist,
144163
exclude,
145164
null.ok = TRUE
146165
)
166+
checkmate::assert_integerish(
167+
nsim_offspring,
168+
lower = 1
169+
)
147170
# likelihood is designed to work with numeric objects to <epichains> objects
148171
# need to be coerced to <epichains_summary> objects (numeric vector under
149172
# the hood) for the likelihood calculation
@@ -249,7 +272,8 @@ likelihood <- function(chains, statistic = c("size", "length"), offspring_dist,
249272
x = calc_sizes,
250273
offspring_dist = offspring_dist,
251274
statistic = statistic,
252-
stat_threshold = stat_threshold
275+
stat_threshold = stat_threshold,
276+
nsim_offspring = nsim_offspring
253277
),
254278
pars
255279
)

vignettes/epichains.Rmd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ If an analytical solution does not exist, an internal simulation function,
227227
`.offspring_ll()` is employed. It uses simulations to approximate the probability
228228
distributions ([using a linear approximation to the cumulative
229229
distribution](https://en.wikipedia.org/wiki/Empirical_distribution_function)
230-
for unobserved sizes/lengths). If simulation is to be used, an extra argument
231-
`nsim_offspring` must be passed to `likelihood()` to specify the number of
232-
simulations to be used for this approximation.
230+
for unobserved sizes/lengths). If simulation is to be used, the `nsim_offspring`
231+
argument of `likelihood()` can be used to specify the number of simulations to be
232+
used for this approximation (it defaults to `100`).
233233
Approximate values of the likelihood will vary with every call to the simulation (because the simulations used for estimation vary), and it may be worth calling `likelihood()` multiple times in this case to see the error this may introduce.
234234

235235
For example, let's look at an example where `chain_sizes` is observed and we

0 commit comments

Comments
 (0)