|
9 | 9 | #' @param nsim_obs Number of simulations to be used to approximate the |
10 | 10 | #' log-likelihood/likelihood if `obs_prob < 1` (imperfect observation). If |
11 | 11 | #' `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`. |
12 | 17 | #' @param obs_prob Observation probability. A number (probability) between 0 |
13 | 18 | #' and 1. A value greater than 0 but less 1 implies imperfect observation and |
14 | 19 | #' simulations will be used to approximate the (log)likelihood. This will |
|
102 | 107 | #' size = 0.2 |
103 | 108 | #' ) |
104 | 109 | #' 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 | +#' ) |
105 | 123 | #' @export |
106 | 124 | # nolint start: cyclocomp_linter |
107 | 125 | likelihood <- function(chains, statistic = c("size", "length"), offspring_dist, |
108 | 126 | nsim_obs, obs_prob = 1, stat_threshold = Inf, log = TRUE, |
109 | | - exclude = NULL, individual = FALSE, ...) { |
| 127 | + exclude = NULL, individual = FALSE, nsim_offspring = 100, |
| 128 | + ...) { |
110 | 129 | statistic <- match.arg(statistic) |
111 | 130 |
|
112 | 131 | ## Input checking |
@@ -144,6 +163,10 @@ likelihood <- function(chains, statistic = c("size", "length"), offspring_dist, |
144 | 163 | exclude, |
145 | 164 | null.ok = TRUE |
146 | 165 | ) |
| 166 | + checkmate::assert_integerish( |
| 167 | + nsim_offspring, |
| 168 | + lower = 1 |
| 169 | + ) |
147 | 170 | # likelihood is designed to work with numeric objects to <epichains> objects |
148 | 171 | # need to be coerced to <epichains_summary> objects (numeric vector under |
149 | 172 | # the hood) for the likelihood calculation |
@@ -249,7 +272,8 @@ likelihood <- function(chains, statistic = c("size", "length"), offspring_dist, |
249 | 272 | x = calc_sizes, |
250 | 273 | offspring_dist = offspring_dist, |
251 | 274 | statistic = statistic, |
252 | | - stat_threshold = stat_threshold |
| 275 | + stat_threshold = stat_threshold, |
| 276 | + nsim_offspring = nsim_offspring |
253 | 277 | ), |
254 | 278 | pars |
255 | 279 | ) |
|
0 commit comments