Skip to content

Commit

Permalink
update .lintr file and review likelihood.R file
Browse files Browse the repository at this point in the history
  • Loading branch information
Karim-Mane committed Dec 11, 2023
1 parent 21f9fe0 commit 03da876
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 92 deletions.
31 changes: 27 additions & 4 deletions .lintr
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
linters: linters_with_tags(
tags = NULL, # include all linters
linters: all_linters(
packages = c("lintr", "etdev"),
object_name_linter = NULL,
undesirable_function_linter = NULL,
implicit_integer_linter = NULL,
extraction_operator_linter = NULL,
todo_comment_linter = NULL,
library_call_linter = NULL,
undesirable_function_linter(
modify_defaults(
default_undesirable_functions,
citEntry = "use the more modern bibentry() function",
library = NULL # too many false positive in too many files
)
),
function_argument_linter = NULL,
indentation_linter = NULL, # unstable as of lintr 3.1.0
# Use minimum R declared in DESCRIPTION or fall back to current R version.
# Install etdev package from https://github.com/epiverse-trace/etdev
backport_linter(if (length(x <- etdev::extract_min_r_version())) x else getRversion())
)
exclusions: list(
"tests/testthat.R" = list(unused_import_linter = Inf)
"tests/testthat.R" = list(
unused_import_linter = Inf
),
"tests" = list(
undesirable_function_linter = Inf
),
"data-raw" = list(
missing_package_linter = Inf,
namespace_linter = Inf
),
# RcppExports.R is auto-generated and will not pass many linters. In
# particular, it can create very long lines.
"R/RcppExports.R",
# R/stanmodels.R is auto-generated and will not pass many linters. In
# particular, it uses `sapply()`.
"R/stanmodels.R"
)
18 changes: 10 additions & 8 deletions R/checks.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#' Check if offspring argument is specified as a character string
#'
#' @param offspring_dist Offspring distribution: a character string
#' corresponding to the R distribution function (e.g., "pois" for Poisson,
#' where \code{\link{rpois}} is the R function to generate Poisson random
#' numbers).
#' corresponding to the R distribution function (e.g., "pois" for Poisson,
#' where \code{\link{rpois}} is the R function to generate Poisson random
#' numbers).
#'
#' @keywords internal
check_offspring_valid <- function(offspring_dist) {
if (!checkmate::test_string(offspring_dist)) {
Expand All @@ -19,8 +20,9 @@ check_offspring_valid <- function(offspring_dist) {
#' Check if constructed random number generator for offspring exists
#'
#' @param roffspring_name Constructed random offspring sampler: a character
#' string corresponding to the R distribution function (e.g., "rpois" for
#' Poisson.
#' string corresponding to the R distribution function (e.g., "rpois" for
#' Poisson.
#'
#' @keywords internal
check_offspring_func_valid <- function(roffspring_name) {
if (!(exists(roffspring_name)) ||
Expand All @@ -36,15 +38,15 @@ check_offspring_func_valid <- function(roffspring_name) {
#'
#' @keywords internal
check_generation_time_valid <- function(generation_time) {
if (!checkmate::test_function(generation_time, nargs = 1)) {
if (!checkmate::test_function(generation_time, nargs = 1L)) {
stop(sprintf(
"%s %s",
"The `generation_time` argument must be a function",
"(see details in ?simulate_tree)."
))
}
x <- generation_time(10)
if (!checkmate::test_numeric(x, len = 10)) {
x <- generation_time(10L)
if (!checkmate::test_numeric(x, len = 10L)) {
stop(
"The return values of `generation_time`",
"must be a numeric vector of length `n`."
Expand Down
58 changes: 36 additions & 22 deletions R/likelihood.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#'
#' @inheritParams offspring_ll
#' @inheritParams simulate_summary
#'
#' @param chains Vector of chain summaries (sizes/lengths)
#' @param nsim_obs Number of simulations if the log-likelihood/likelihood is to
#' be approximated for imperfect observations.
#' be approximated for imperfect observations.
#' @param log Logical; Should the log-likelihoods be transformed to
#' likelihoods? (Defaults to TRUE).
#' likelihoods? (Defaults to TRUE).
#' @param obs_prob Observation probability (assumed constant)
#' @param exclude A vector of indices of the sizes/lengths to exclude from the
#' log-likelihood calculation.
#' log-likelihood calculation.
#' @param individual If TRUE, a vector of individual log-likelihood/likelihood
#' contributions will be returned rather than the sum/product.
#' contributions will be returned rather than the sum/product.
#'
#' @return
#' If \code{log = TRUE}
#'
Expand All @@ -28,54 +30,66 @@
#' except that likelihoods, instead of log-likelihoods, are calculated in all
#' cases. Moreover, the joint likelihoods are the product, instead of the sum,
#' of the individual likelihoods.
#'
#' @seealso offspring_ll(), pois_size_ll(), nbinom_size_ll(), gborel_size_ll(),
#' pois_length_ll(), geom_length_ll()
#'
#' @author Sebastian Funk
#'
#' @examples
#' # example of observed chain sizes
#' set.seed(121)
#' # randomly generate 20 chains of size 1 to 10
#' chain_sizes <- sample(1:10, 20, replace = TRUE)
#' likelihood(
#' chains = chain_sizes, statistic = "size",
#' offspring_dist = "pois", nsim_obs = 100, lambda = 0.5
#' chains = chain_sizes,
#' statistic = "size",
#' offspring_dist = "pois",
#' nsim_obs = 100,
#' lambda = 0.5
#' )
#' @export
likelihood <- function(chains, statistic = c("size", "length"), offspring_dist,
nsim_obs, log = TRUE, obs_prob = 1, stat_max = Inf,
exclude = NULL, individual = FALSE, ...) {
likelihood <- function(chains,
offspring_dist,
nsim_obs,
statistic = c("size", "length"),
log = TRUE,
obs_prob = 1L,
stat_max = Inf,
exclude = NULL,
individual = FALSE, ...) {
statistic <- match.arg(statistic)

## Input checking
## Check nsim_obs when specified
if (!missing(nsim_obs)) {
checkmate::assert_number(
nsim_obs, lower = 1, finite = TRUE, na.ok = FALSE
nsim_obs, lower = 1L, finite = TRUE, na.ok = FALSE
)
}

checkmate::assert_numeric(
chains, lower = 0, upper = Inf, any.missing = FALSE
chains, lower = 0L, upper = Inf, any.missing = FALSE
)
checkmate::assert_character(statistic)
check_offspring_valid(offspring_dist)
checkmate::assert_number(
obs_prob, lower = 0, upper = 1, finite = TRUE, na.ok = FALSE
obs_prob, lower = 0L, upper = 1L, finite = TRUE, na.ok = FALSE
)
checkmate::assert_number(
stat_max, lower = 0, na.ok = FALSE
stat_max, lower = 0L, na.ok = FALSE
)
checkmate::assert_logical(
log, any.missing = FALSE, all.missing = FALSE, len = 1
log, any.missing = FALSE, all.missing = FALSE, len = 1L
)
checkmate::assert_logical(
individual, any.missing = FALSE, all.missing = FALSE, len = 1
individual, any.missing = FALSE, all.missing = FALSE, len = 1L
)
checkmate::assert_numeric(
exclude, null.ok = TRUE
)

if (obs_prob < 1) {
if (obs_prob < 1L) {
if (missing(nsim_obs)) {
stop("'nsim_obs' must be specified if 'obs_prob' is < 1")
}
Expand All @@ -91,7 +105,7 @@ likelihood <- function(chains, statistic = c("size", "length"), offspring_dist,
), simplify = FALSE)
stat_rep_vect <- unlist(stat_rep_list)
if (!is.finite(stat_max)) {
stat_max <- max(stat_rep_vect) + 1
stat_max <- max(stat_rep_vect) + 1L
}
} else {
chains[chains >= stat_max] <- stat_max
Expand All @@ -102,7 +116,7 @@ likelihood <- function(chains, statistic = c("size", "length"), offspring_dist,
## determine for which sizes to calculate the log-likelihood
## (for true chain size)
if (any(stat_rep_vect == stat_max)) {
calc_sizes <- seq_len(stat_max - 1)
calc_sizes <- seq_len(stat_max - 1L)
} else {
calc_sizes <- unique(c(stat_rep_vect, exclude))
}
Expand All @@ -122,10 +136,10 @@ likelihood <- function(chains, statistic = c("size", "length"), offspring_dist,
offspring_ll,
c(
list(
x = calc_sizes,
x = calc_sizes,
offspring_dist = offspring_dist,
statistic = statistic,
stat_max = stat_max
statistic = statistic,
stat_max = stat_max
),
pars
)
Expand Down Expand Up @@ -154,7 +168,7 @@ likelihood <- function(chains, statistic = c("size", "length"), offspring_dist,
## if individual == FALSE, return the joint log-likelihood
## (sum of the log-likelihoods)
if (!individual) {
chains_likelihood <- vapply(chains_likelihood, sum, 0)
chains_likelihood <- vapply(chains_likelihood, sum, 0L)
}

## transform log-likelihoods into likelihoods if required
Expand Down
Loading

0 comments on commit 03da876

Please sign in to comment.