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

Fixes bug in as_epiparam() #162

Merged
merged 5 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 8 additions & 2 deletions .lintr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ linters: linters_with_tags(
implicit_integer_linter = NULL,
extraction_operator_linter = NULL,
todo_comment_linter = NULL,
function_argument_linter = NULL
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)
)

36 changes: 36 additions & 0 deletions R/epidist_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,12 @@ clean_epidist_params <- function(prob_dist_params, ...) {
#' @keywords internal
clean_epidist_params.gamma <- function(prob_dist_params) {

# if unparameterised return named vector of NAs
if (isTRUE(is.na(prob_dist_params))) {
joshwlambert marked this conversation as resolved.
Show resolved Hide resolved
prob_dist_params <- c(shape = NA_real_, scale = NA_real_)
return(prob_dist_params)
}

# if shape and rate are provided convert to shape and scale
if (all(c("shape", "rate") %in% names(prob_dist_params))) {
prob_dist_params[["rate"]] <- 1 / prob_dist_params[["rate"]]
Expand Down Expand Up @@ -580,6 +586,12 @@ clean_epidist_params.gamma <- function(prob_dist_params) {
#' @keywords internal
clean_epidist_params.lnorm <- function(prob_dist_params) {

# if unparameterised return named vector of NAs
if (isTRUE(is.na(prob_dist_params))) {
prob_dist_params <- c(meanlog = NA_real_, sdlog = NA_real_)
return(prob_dist_params)
}

# if mu and sigma are provided convert to meanlog and sdlog
if (all(c("mu", "sigma") %in% names(prob_dist_params))) {

Expand Down Expand Up @@ -616,6 +628,12 @@ clean_epidist_params.lnorm <- function(prob_dist_params) {
#' @keywords internal
clean_epidist_params.weibull <- function(prob_dist_params) {

# if unparameterised return named vector of NAs
if (isTRUE(is.na(prob_dist_params))) {
prob_dist_params <- c(shape = NA_real_, scale = NA_real_)
return(prob_dist_params)
}

if (all(c("shape", "scale") %in% names(prob_dist_params))) {
# remove class attribute from prob_dist_params
prob_dist_params <- unclass(prob_dist_params)
Expand All @@ -638,6 +656,12 @@ clean_epidist_params.weibull <- function(prob_dist_params) {
#' @keywords internal
clean_epidist_params.nbinom <- function(prob_dist_params) {

# if unparameterised return named vector of NAs
if (isTRUE(is.na(prob_dist_params))) {
prob_dist_params <- c(mean = NA_real_, dispersion = NA_real_)
return(prob_dist_params)
}

if (all(c("n", "p") %in% names(prob_dist_params))) {

# convert prob to mean
Expand Down Expand Up @@ -682,6 +706,12 @@ clean_epidist_params.nbinom <- function(prob_dist_params) {
#' @keywords internal
clean_epidist_params.geom <- function(prob_dist_params) {

# if unparameterised return named NA
if (isTRUE(is.na(prob_dist_params))) {
prob_dist_params <- c(prob = NA_real_)
return(prob_dist_params)
}

# if mean is provided convert to prob
if ("mean" %in% names(prob_dist_params)) {
prob_dist_params[["mean"]] <- 1 / prob_dist_params[["mean"]]
Expand Down Expand Up @@ -732,6 +762,12 @@ clean_epidist_params.geom <- function(prob_dist_params) {
#' @keywords internal
clean_epidist_params.pois <- function(prob_dist_params) {

# if unparameterised return named NA
if (isTRUE(is.na(prob_dist_params))) {
prob_dist_params <- c(mean = NA_real_)
return(prob_dist_params)
}

if (names(prob_dist_params) %in% c("mean", "l")) {
names(prob_dist_params) <- "mean"

Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-epidist_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,33 @@ test_that("clean_epidist_params works for default method", {
)
})

test_that("clean_epidist_params works for unparameterised (NA), (#161)", {
params <- NA
class(params) <- "gamma"
res <- clean_epidist_params(prob_dist_params = params)
expect_identical(res, c(shape = NA_real_, scale = NA_real_))

class(params) <- "lnorm"
res <- clean_epidist_params(prob_dist_params = params)
expect_identical(res, c(meanlog = NA_real_, sdlog = NA_real_))

class(params) <- "weibull"
res <- clean_epidist_params(prob_dist_params = params)
expect_identical(res, c(shape = NA_real_, scale = NA_real_))

class(params) <- "nbinom"
res <- clean_epidist_params(prob_dist_params = params)
expect_identical(res, c(mean = NA_real_, dispersion = NA_real_))

class(params) <- "geom"
res <- clean_epidist_params(prob_dist_params = params)
expect_identical(res, c(prob = NA_real_))

class(params) <- "pois"
res <- clean_epidist_params(prob_dist_params = params)
expect_identical(res, c(mean = NA_real_))
})

test_that("create_epidist_region works as expected", {
region <- create_epidist_region(
continent = "Europe",
Expand Down
Loading