Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substitute nested ifelse with data.table::fcase() #383

Merged
merged 31 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0023aba
updated gitignore
jamesmbaazam May 4, 2023
6d1eccc
replaced ifelse with fcase in map_prob_change()
jamesmbaazam May 4, 2023
7e91528
revised explicit breakpoints for the categories
jamesmbaazam May 4, 2023
c8db992
replace nested ifelse() with data.table::fcase() in create_backcalc_d…
jamesmbaazam May 5, 2023
7b63d9b
set the default samples argument to 1000 and added a warning message
jamesmbaazam May 5, 2023
6a8a115
replaced ifelse with data.table::fcase in create_gp_data()
jamesmbaazam May 5, 2023
e27fe70
replaced ifelse with fcase in create_initial_conditions()
jamesmbaazam May 5, 2023
ba2572d
moved warning message into rightful place (inside the if() block)
jamesmbaazam May 5, 2023
9274e7a
replaced ifelse with data.table::fcase in regional_summary()
jamesmbaazam May 5, 2023
188a79a
replaced ifelse with data.table::fcase in format_fit()
jamesmbaazam May 5, 2023
72e04de
linting: removed nolint tags and unnecessary whitespace, and added pr…
jamesmbaazam May 5, 2023
b3ced53
Revert "set the default samples argument to 1000 and added a warning …
jamesmbaazam May 9, 2023
f2a2a36
register fcase import
jamesmbaazam May 9, 2023
ff9412c
redoc create_backcalc_data
jamesmbaazam May 9, 2023
f8a5cb2
make rho an array
jamesmbaazam May 9, 2023
0d79c79
linting: remove whitespace
jamesmbaazam May 10, 2023
a173a03
added a condition for when date is NA
jamesmbaazam May 10, 2023
7d30e3b
removed linting gates
jamesmbaazam May 11, 2023
84491c8
fixed indentation
jamesmbaazam May 11, 2023
610fb6d
added James Azam as contributor
jamesmbaazam May 11, 2023
bf9459c
removed a space
jamesmbaazam May 11, 2023
276af42
incremented the version and added a news item
jamesmbaazam May 11, 2023
3841eaa
removed a space
jamesmbaazam May 11, 2023
6109f24
linting: removed whitespace and added lint gates
jamesmbaazam May 13, 2023
74f699b
redoc'd package
jamesmbaazam May 13, 2023
b853c98
incremented dev version
jamesmbaazam May 16, 2023
37786f0
changed package version in NEWS
jamesmbaazam May 16, 2023
b286eba
Updated news entry to link PR
jamesmbaazam May 16, 2023
e883619
fixed indentation
jamesmbaazam May 16, 2023
70483ea
remove space
seabbs May 17, 2023
2f2a765
fix space
seabbs May 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ CRAN-RELEASE
# C++ object files
inst/include/*.o
# avoid rstantools generated files
src
src

.DS_Store
7 changes: 6 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Type: Package
Package: EpiNow2
Title: Estimate Real-Time Case Counts and Time-Varying
Epidemiological Parameters
Version: 1.3.6.2000
Version: 1.3.6.2001
seabbs marked this conversation as resolved.
Show resolved Hide resolved
Authors@R:
c(person(given = "Sam",
family = "Abbott",
Expand Down Expand Up @@ -67,6 +67,11 @@ Authors@R:
family = "Chapman",
role = "ctb",
email = "lloyd.chapman1@lshtm.ac.uk "),
person(given = "James M.",
family = "Azam",
role = "ctb",
email = "james.azam@lshtm.ac.uk",
comment = c(ORCID = "0000-0001-5782-7330")),
person(given = "EpiForecasts",
role = "aut"),
person(given = "Sebastian",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ importFrom(data.table,as.data.table)
importFrom(data.table,copy)
importFrom(data.table,data.table)
importFrom(data.table,dcast)
importFrom(data.table,fcase)
importFrom(data.table,fifelse)
importFrom(data.table,frollmean)
importFrom(data.table,frollsum)
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# EpiNow2 1.3.6.2000
# EpiNow2 (development version)

# EpiNow2 1.3.6.2001
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be just epinow2 1.3.6 it doesn't need the nested title

Copy link
Contributor Author

@jamesmbaazam jamesmbaazam May 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been fixed in 37786f0.


This release is in development. For a stable release install 1.3.5 from CRAN.

Expand All @@ -10,6 +12,8 @@ This release is in development. For a stable release install 1.3.5 from CRAN.
* Added a GitHub Action to build the README when it is altered.
* Added handling of edge case where we sample from the negative binomial with
mean close or equal to 0. By @sbfnk in #366.
* Replaced use of nested `ifelse()` and `data.table::fifelse()` in the
seabbs marked this conversation as resolved.
Show resolved Hide resolved
code base with `data.table::fcase()`.

# EpiNow2 1.3.5

Expand Down
63 changes: 33 additions & 30 deletions R/create.R
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ create_rt_data <- function(rt = rt_opts(), breakpoints = NULL,
#' define the back calculation. Defaults to `backcalc_opts()`.
#'
#' @seealso backcalc_opts
#' @importFrom data.table fcase
#' @return A list of settings defining the Gaussian process
#' @export
#' @author Sam Abbott
Expand All @@ -282,18 +283,17 @@ create_rt_data <- function(rt = rt_opts(), breakpoints = NULL,
#'
#' # custom lengthscale
#' create_gp_data(gp_opts(ls_mean = 14), data)
create_backcalc_data <- function(backcalc = backcalc_opts) {
create_backcalc_data <- function(backcalc = backcalc_opts()) {
data <- list(
rt_half_window = as.integer((backcalc$rt_window - 1) / 2),
# nolint start
backcalc_prior = ifelse(backcalc$prior == "none", 0,
ifelse(backcalc$prior == "reports", 1,
ifelse(backcalc$prior == "infections", 2, 0)
)
)
# nolint end
)
return(data)
rt_half_window = as.integer((backcalc$rt_window - 1) / 2),
backcalc_prior = data.table::fcase(
backcalc$prior == "none", 0,
backcalc$prior == "reports", 1,
backcalc$prior == "infections", 2,
default = 0
)
)
return(data)
}
#' Create Gaussian Process Data
#'
Expand All @@ -305,6 +305,7 @@ create_backcalc_data <- function(backcalc = backcalc_opts) {
#' Gaussian process.
#' @param data A list containing the following numeric values:
#' `t`, `seeding_time`, `horizon`.
#' @importFrom data.table fcase
#' @seealso gp_opts
#' @return A list of settings defining the Gaussian process
#' @export
Expand All @@ -325,6 +326,7 @@ create_backcalc_data <- function(backcalc = backcalc_opts) {
#'
#' # custom lengthscale
#' create_gp_data(gp_opts(ls_mean = 14), data)

seabbs marked this conversation as resolved.
Show resolved Hide resolved
create_gp_data <- function(gp = gp_opts(), data) {
# Define if GP is on or off
if (is.null(gp)) {
Expand All @@ -347,19 +349,19 @@ create_gp_data <- function(gp = gp_opts(), data) {

# map settings to underlying gp stan requirements
gp_data <- list(
fixed = as.numeric(fixed),
M = M,
L = gp$boundary_scale,
ls_meanlog = convert_to_logmean(gp$ls_mean, gp$ls_sd),
ls_sdlog = convert_to_logsd(gp$ls_mean, gp$ls_sd),
ls_min = gp$ls_min,
ls_max = data$t - data$seeding_time - data$horizon,
alpha_sd = gp$alpha_sd,
# nolint start
gp_type = ifelse(gp$kernel == "se", 0,
ifelse(gp$kernel == "matern", 1, 0)
fixed = as.numeric(fixed),
jamesmbaazam marked this conversation as resolved.
Show resolved Hide resolved
sbfnk marked this conversation as resolved.
Show resolved Hide resolved
seabbs marked this conversation as resolved.
Show resolved Hide resolved
M = M,
L = gp$boundary_scale,
ls_meanlog = convert_to_logmean(gp$ls_mean, gp$ls_sd),
ls_sdlog = convert_to_logsd(gp$ls_mean, gp$ls_sd),
ls_min = gp$ls_min,
ls_max = data$t - data$seeding_time - data$horizon,
alpha_sd = gp$alpha_sd,
gp_type = data.table::fcase(
gp$kernel == "se", 0,
gp$kernel == "matern", 1,
default = 0
)
# nolint end
)

gp_data <- c(data, gp_data)
Expand Down Expand Up @@ -538,6 +540,7 @@ create_stan_data <- function(reported_cases, generation_time,
#' @return An initial condition generating function
#' @importFrom purrr map2_dbl
#' @importFrom truncnorm rtruncnorm
#' @importFrom data.table fcase
#' @export
# @author Sam Abbott
# @author Sebastian Funk
Expand Down Expand Up @@ -581,13 +584,13 @@ create_initial_conditions <- function(data) {
meanlog = data$ls_meanlog,
sdlog = ifelse(data$ls_sdlog > 0, data$ls_sdlog * 0.1, 0.01)
))
# nolint start
out$rho <- ifelse(out$rho > data$ls_max, data$ls_max - 0.001,
ifelse(out$rho < data$ls_min, data$ls_min + 0.001,
out$rho
)
)
# nolint end

out$rho <- array(data.table::fcase(
out$rho > data$ls_max, data$ls_max - 0.001,
out$rho < data$ls_min, data$ls_min + 0.001,
default = out$rho
))

out$alpha <- array(
truncnorm::rtruncnorm(1, a = 0, mean = 0, sd = data$alpha_sd)
)
Expand Down
1 change: 0 additions & 1 deletion R/dist.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ dist_fit <- function(values = NULL, samples = NULL, cores = 1,
if (samples < 1000) {
samples <- 1000
}

seabbs marked this conversation as resolved.
Show resolved Hide resolved
# model parameters
lows <- values - 1
lows <- ifelse(lows <= 0, 1e-6, lows)
Expand Down
20 changes: 8 additions & 12 deletions R/estimate_infections.R
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ fit_model_with_vb <- function(args, future = FALSE, id = "stan") {
#' @param start_date Date, earliest date with data.
#'
#' @inheritParams calc_summary_measures
#' @importFrom data.table fifelse rbindlist
#' @importFrom data.table fcase rbindlist
#' @importFrom lubridate days
#' @importFrom futile.logger flog.info
#' @return A list of samples and summarised posterior parameter estimates.
Expand All @@ -695,20 +695,16 @@ format_fit <- function(posterior_samples, horizon, shift, burn_in, start_date,
format_out$samples <- format_out$samples[, strat := NA]
}
# add type based on horizon
# nolint start
format_out$samples <- format_out$samples[
,
type := data.table::fifelse(
date > (max(date, na.rm = TRUE) - horizon),
"forecast",
data.table::fifelse(
date > (max(date, na.rm = TRUE) - horizon - shift),
"estimate based on partial data",
"estimate"
type := data.table::fcase(
date > (max(date, na.rm = TRUE) - horizon), "forecast",
date > (max(date, na.rm = TRUE) - horizon - shift),
"estimate based on partial data",
is.na(date), NA_character_,
seabbs marked this conversation as resolved.
Show resolved Hide resolved
default = "estimate"
)
seabbs marked this conversation as resolved.
Show resolved Hide resolved
)
]
# nolint end
]
seabbs marked this conversation as resolved.
Show resolved Hide resolved

# remove burn in period if specified
if (burn_in > 0) {
Expand Down
2 changes: 1 addition & 1 deletion R/stanmodels.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Rcpp::loadModule("stan_fit4tune_inv_gamma_mod", what = TRUE)
# instantiate each stanmodel object
stanmodels <- sapply(stanmodels, function(model_name) {
# create C++ code for stan model
stan_file <- if (dir.exists("stan")) "stan" else file.path("inst", "stan")
jamesmbaazam marked this conversation as resolved.
Show resolved Hide resolved
stan_file <- if(dir.exists("stan")) "stan" else file.path("inst", "stan")
stan_file <- file.path(stan_file, paste0(model_name, ".stan"))
stanfit <- rstan::stanc_builder(stan_file,
allow_undefined = TRUE,
Expand Down
23 changes: 12 additions & 11 deletions R/summarise.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ summarise_results <- function(regions,
#' @inheritParams epinow
#' @importFrom purrr map_chr compact
#' @importFrom ggplot2 coord_cartesian guides guide_legend ggsave ggplot_build
#' @importFrom data.table setDT
#' @importFrom data.table setDT fcase
#' @importFrom futile.logger flog.info
#' @examples
#' \donttest{
Expand Down Expand Up @@ -336,10 +336,11 @@ regional_summary <- function(regional_output = NULL,
)
}
save_ggplot(summary_plot, "summary_plot.png",
width = ifelse(length(regions) > 60,
ifelse(length(regions) > 120, 36, 24), # nolint
12
)
width = data.table::fcase(
length(regions) > 60 & length(regions) > 120, 36,
length(regions) > 60 & !(length(regions) > 120), 24,
default = 12
)
)
}
# extract regions with highest number of reported cases in the last week
Expand Down Expand Up @@ -370,11 +371,11 @@ regional_summary <- function(regional_output = NULL,
}

if (all_regions) {
# nolint start
plots_per_row <- ifelse(length(regions) > 60,
ifelse(length(regions) > 120, 8, 5), 3
)
# nolint end
plots_per_row <- data.table::fcase(
length(regions) > 60 & length(regions) > 120, 8,
length(regions) > 60 & !(length(regions) > 120), 5,
default = 3
)

plots <- report_plots(
summarised_estimates = results$estimates$summarised,
Expand Down Expand Up @@ -669,7 +670,7 @@ calc_CrIs <- function(samples, summarise_by = NULL, CrIs = c(0.2, 0.5, 0.9)) {
# nolint start
order_CrIs <- c(
paste0("lower_", rev(scale_CrIs)), paste0("upper_", scale_CrIs)
)
)
# nolint end
jamesmbaazam marked this conversation as resolved.
Show resolved Hide resolved
with_CrIs <- data.table::dcast(
with_CrIs, ... ~ factor(CrI, levels = order_CrIs),
Expand Down
21 changes: 10 additions & 11 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ make_conf <- function(value, CrI = 90, reverse = FALSE) {
#' "Likely decreasing" (< 0.95), "Decreasing" (<= 1)
#' @param var Numeric variable to be categorised
#'
#' @importFrom data.table fcase
#' @return A character variable.
#' @export
#' @examples
Expand All @@ -85,17 +86,15 @@ make_conf <- function(value, CrI = 90, reverse = FALSE) {
#'
#' map_prob_change(var)
map_prob_change <- function(var) {
# nolint start
var <- ifelse(var < 0.05, "Increasing",
ifelse(var < 0.4, "Likely increasing",
ifelse(var < 0.6, "Stable",
ifelse(var < 0.95, "Likely decreasing",
"Decreasing"
)
)
)
)
# nolint end

var <- data.table::fcase(
var < 0.05, "Increasing",
var < 0.4, "Likely increasing",
var < 0.6, "Stable",
var < 0.95, "Likely decreasing",
var <= 1, "Decreasing"
)

var <- factor(var, levels = c(
"Increasing", "Likely increasing", "Stable",
"Likely decreasing", "Decreasing"
Expand Down
1 change: 1 addition & 0 deletions man/EpiNow2-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/create_backcalc_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.