Skip to content

Commit

Permalink
Run styler + lintr
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmbaazam committed Oct 9, 2024
1 parent 6e2ef30 commit eac7a3d
Show file tree
Hide file tree
Showing 22 changed files with 518 additions and 459 deletions.
34 changes: 26 additions & 8 deletions R/borel.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
#' dborel(1:5, 1)
dborel <- function(x, mu, log = FALSE) {
checkmate::assert_numeric(
x, lower = 1, upper = Inf
x,
lower = 1,
upper = Inf
)
checkmate::assert_number(
mu, lower = 0, finite = TRUE, na.ok = FALSE
mu,
lower = 0,
finite = TRUE,
na.ok = FALSE
)

ld <- -mu * x + (x - 1) * log(mu * x) - lgamma(x + 1)
Expand All @@ -41,10 +46,16 @@ dborel <- function(x, mu, log = FALSE) {
#' rborel(5, 1)
rborel <- function(n, mu, censor_at = Inf) {
checkmate::assert_number(
n, lower = 1, finite = TRUE, na.ok = FALSE
n,
lower = 1,
finite = TRUE,
na.ok = FALSE
)
checkmate::assert_number(
mu, lower = 0, finite = TRUE, na.ok = FALSE
mu,
lower = 0,
finite = TRUE,
na.ok = FALSE
)
# Run simulations
out <- simulate_chain_stats(
Expand Down Expand Up @@ -81,16 +92,22 @@ rgborel <- function(n, size, prob, mu, censor_at = Inf) {
# its "correct implementation" for documentation/clarity purposes, as well as
# for simulations.
checkmate::assert_number(
size, finite = TRUE, lower = 0
size,
finite = TRUE,
lower = 0
)
if (!missing(prob)) {
checkmate::assert_number(
prob, lower = 0, upper = 1
prob,
lower = 0,
upper = 1
)
}
if (!missing(mu)) {
checkmate::assert_number(
mu, finite = TRUE, lower = 0
mu,
finite = TRUE,
lower = 0
)
}
if (!missing(prob)) {
Expand All @@ -101,6 +118,7 @@ rgborel <- function(n, size, prob, mu, censor_at = Inf) {
x <- rgamma(n, shape = size, rate = size / mu)
# then, sample from borel
return(vapply(
x, rborel, n = 1, censor_at = censor_at, FUN.VALUE = numeric(1)
x, rborel,
n = 1, censor_at = censor_at, FUN.VALUE = numeric(1)
))
}
8 changes: 4 additions & 4 deletions R/checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
# check that stat_threshold is an integer or Inf.
checkmate::assert(
checkmate::anyInfinite(stat_threshold),
checkmate::check_integerish(
stat_threshold,
lower = 1
checkmate::check_integerish(
stat_threshold,
lower = 1
),
combine = "or"
)
Expand Down Expand Up @@ -102,7 +102,7 @@
if (!is.null(generation_time)) {
.check_generation_time_valid(generation_time)
} else if (tf_specified) {
stop("If `tf` is specified, `generation_time` must be specified too.")
stop("If `tf` is specified, `generation_time` must be specified too.")
}
checkmate::assert_number(
tf,
Expand Down
88 changes: 44 additions & 44 deletions R/epichains.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
#' @author James M. Azam
#' @keywords internal
.new_epichains <- function(sim_df,
n_chains,
statistic,
offspring_dist,
stat_threshold,
track_pop) {
n_chains,
statistic,
offspring_dist,
stat_threshold,
track_pop) {
# Assemble the elements of the object
obj <- sim_df
class(obj) <- c("epichains", class(obj))
Expand Down Expand Up @@ -55,11 +55,11 @@
#' @author James M. Azam
#' @keywords internal
.epichains <- function(sim_df,
n_chains,
offspring_dist,
track_pop,
statistic = c("size", "length"),
stat_threshold = Inf) {
n_chains,
offspring_dist,
track_pop,
statistic = c("size", "length"),
stat_threshold = Inf) {
# Check that inputs are well specified
checkmate::assert_data_frame(sim_df, min.cols = 3, min.rows = n_chains)
checkmate::assert_integerish(
Expand Down Expand Up @@ -112,10 +112,10 @@
#' @author James M. Azam
#' @keywords internal
.new_epichains_summary <- function(chains_summary,
n_chains,
statistic,
offspring_dist,
stat_threshold) {
n_chains,
statistic,
offspring_dist,
stat_threshold) {
# Assemble the elements of the object
obj <- chains_summary
class(obj) <- c("epichains_summary", class(chains_summary))
Expand All @@ -141,10 +141,10 @@
#' @author James M. Azam
#' @keywords internal
.epichains_summary <- function(chains_summary,
n_chains,
offspring_dist,
statistic = c("size", "length"),
stat_threshold = Inf) {
n_chains,
offspring_dist,
statistic = c("size", "length"),
stat_threshold = Inf) {
# chain_summary can sometimes contain infinite values, so check
# that finite elements are integerish.
checkmate::check_integerish(
Expand Down Expand Up @@ -194,12 +194,12 @@
#' # population up to chain size 10.
#' set.seed(32)
#' chains_pois_offspring <- simulate_chains(
#' n_chains = 10,
#' statistic = "size",
#' offspring_dist = rpois,
#' stat_threshold = 10,
#' generation_time = function(n) rep(3, n),
#' lambda = 2
#' n_chains = 10,
#' statistic = "size",
#' offspring_dist = rpois,
#' stat_threshold = 10,
#' generation_time = function(n) rep(3, n),
#' lambda = 2
#' )
#' chains_pois_offspring # Print the object
print.epichains <- function(x, ...) {
Expand All @@ -226,11 +226,11 @@ print.epichains <- function(x, ...) {
#' # population up to chain size 10.
#' set.seed(32)
#' chain_summary_print_eg <- simulate_chain_stats(
#' n_chains = 10,
#' statistic = "size",
#' offspring_dist = rpois,
#' stat_threshold = 10,
#' lambda = 2
#' n_chains = 10,
#' statistic = "size",
#' offspring_dist = rpois,
#' stat_threshold = 10,
#' lambda = 2
#' )
#' chain_summary_print_eg # Print the object
print.epichains_summary <- function(x, ...) {
Expand Down Expand Up @@ -322,19 +322,19 @@ format.epichains_summary <- function(x, ...) {
"Max: %s",
ifelse(
is.infinite(
statistics[["max_stat"]]),
paste0(">=", attr(x, "stat_threshold")
statistics[["max_stat"]]
),
paste0(">=", attr(x, "stat_threshold")),
statistics[["max_stat"]]
)
),
sprintf(
"Min: %s",
ifelse(
is.infinite(
statistics[["min_stat"]]),
paste0(">=", attr(x, "stat_threshold")
statistics[["min_stat"]]
),
paste0(">=", attr(x, "stat_threshold")),
statistics[["min_stat"]]
)
)
Expand Down Expand Up @@ -455,11 +455,11 @@ summary.epichains <- function(object, ...) {
#' # population up to chain size 10.
#' set.seed(32)
#' chain_stats <- simulate_chain_stats(
#' n_chains = 10,
#' statistic = "size",
#' offspring_dist = rpois,
#' stat_threshold = 10,
#' lambda = 2
#' n_chains = 10,
#' statistic = "size",
#' offspring_dist = rpois,
#' stat_threshold = 10,
#' lambda = 2
#' )
#' summary(chain_stats)
summary.epichains_summary <- function(object, ...) {
Expand Down Expand Up @@ -526,7 +526,7 @@ summary.epichains_summary <- function(object, ...) {
stopifnot(
"object does not contain the correct columns" =
c("chain", "infector", "infectee", "generation") %in%
colnames(x),
colnames(x),
"column `chain` must be a numeric" =
is.numeric(x$chain),
"column `infector` must be a numeric" =
Expand Down Expand Up @@ -643,11 +643,11 @@ tail.epichains <- function(x, ...) {
#' cases_per_gen <- aggregate(chains, by = "generation")
#' head(cases_per_gen)
aggregate.epichains <- function(x,
by = c(
"time",
"generation"
),
...) {
by = c(
"time",
"generation"
),
...) {
.validate_epichains(x)
checkmate::assert_string(by)
# Get grouping variable
Expand Down
11 changes: 4 additions & 7 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#' @keywords internal
.update_chain_stat <- function(stat_type, stat_latest, n_offspring) {
return(
switch(
stat_type,
switch(stat_type,
size = stat_latest + n_offspring,
length = stat_latest + pmin(1, n_offspring),
stop("stat_type must be 'size' or 'length'")
Expand All @@ -27,8 +26,7 @@
#' @keywords internal
.get_statistic_func <- function(chain_statistic) {
return(
switch(
chain_statistic,
switch(chain_statistic,
size = .rbinom_size,
length = .rgen_length,
stop("chain_statistic must be 'size' or 'length'")
Expand Down Expand Up @@ -73,7 +71,6 @@
offspring_func_pars,
n_offspring,
chains) {

possible_new_offspring <- do.call(
offspring_func,
c(
Expand Down Expand Up @@ -104,8 +101,8 @@
#' given the current susceptible population size.
#' @keywords internal
.get_susceptible_offspring <- function(new_offspring,
susc_pop,
pop) {
susc_pop,
pop) {
# We first adjust for the case where susceptible can be Inf but prob can only
# be maximum 1.
binom_prob <- min(1, susc_pop / pop, na.rm = TRUE)
Expand Down
Loading

0 comments on commit eac7a3d

Please sign in to comment.