Skip to content

Commit

Permalink
Update tests and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkucharski committed Jul 1, 2024
1 parent 5de4572 commit e79f954
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 48 deletions.
29 changes: 16 additions & 13 deletions R/estimate_severity.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
#' the estimate and confidence intervals cannot be calculated and the output
#' `<data.frame>` contains only `NA`s.
#'
#' - When `total_outcomes <= total_deaths`, the confidence intervals cannot be
#' reliably calculated and are returned as `NA`. The severity estimate is returned
#' as `0.999`.
#' - When `total_outcomes <= total_deaths`, the estimate and confidence intervals

Check warning on line 33 in R/estimate_severity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/estimate_severity.R,line=33,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 81 characters.
#' cannot be reliably calculated and are returned as `NA`.
.estimate_severity <- function(total_cases,
total_deaths,
total_outcomes,
Expand Down Expand Up @@ -63,27 +62,31 @@
# maximum likelihood estimation for corrected severity
# using increments of 0.1% severity
pprange <- seq(from = 1e-4, to = 1.0, by = 1e-4)

# if more expected outcomes than observed deaths, set outcomes equal to deaths
if(total_outcomes>total_deaths){
total_outcomes_checked <- total_outcomes
}else{
total_outcomes_checked <- total_deaths
if (total_outcomes >= total_deaths){

Check warning on line 67 in R/estimate_severity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/estimate_severity.R,line=67,col=38,[brace_linter] There should be a space before an opening curly brace.

Check warning on line 67 in R/estimate_severity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/estimate_severity.R,line=67,col=38,[paren_body_linter] There should be a space between a right parenthesis and a body expression.
total_outcomes_checked <- total_outcomes }else{

Check warning on line 68 in R/estimate_severity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/estimate_severity.R,line=68,col=48,[brace_linter] Closing curly-braces should always be on their own line, unless they are followed by an else.

Check warning on line 68 in R/estimate_severity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/estimate_severity.R,line=68,col=53,[brace_linter] There should be a space before an opening curly brace.

Check warning on line 68 in R/estimate_severity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/estimate_severity.R,line=68,col=54,[trailing_whitespace_linter] Trailing whitespace is superfluous.
total_outcomes_checked <- NA
message(
"Total deaths = ", total_deaths,
" and expected outcomes = ", round(total_outcomes),
" so setting expected outcomes = total deaths. Note: CI output will be NA under this assumption."
" so setting expected outcomes = NA. If we were to assume
total deaths = expected outcomes, it would produce an estimate of 1."
)
}

Check warning on line 77 in R/estimate_severity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/estimate_severity.R,line=77,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
# get likelihoods using selected function
lik <- func_likelihood(total_outcomes_checked, total_deaths, pprange)

# maximum likelihood estimate - if this is empty, return NA
# Otherwise return 95% confidence interval of likelihood
severity_estimate <- pprange[which.max(lik)]

# 95% confidence interval of likelihood
severity_lims <- range(pprange[lik >= (max(lik) - 1.92)])
if (length(severity_estimate)==0){

Check warning on line 84 in R/estimate_severity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/estimate_severity.R,line=84,col=32,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 84 in R/estimate_severity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/estimate_severity.R,line=84,col=36,[brace_linter] There should be a space before an opening curly brace.

Check warning on line 84 in R/estimate_severity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/estimate_severity.R,line=84,col=36,[paren_body_linter] There should be a space between a right parenthesis and a body expression.
severity_estimate <- NA
severity_lims <- c(NA,NA) }else{
severity_lims <- range(pprange[lik >=
(max(lik,na.rm = TRUE) - 1.92)],na.rm = TRUE)
}

# return a vector for easy conversion to data
severity_estimate <- c(severity_estimate, severity_lims)
Expand Down Expand Up @@ -126,7 +129,7 @@
# NOTE: internal function is not input checked
# switch likelihood function based on total cases and p_mid
# Binomial approx
if (total_cases < poisson_threshold | (p_mid >= 0.05)) {
if (total_cases < poisson_threshold || (p_mid >= 0.05)) {
func_likelihood <- function(total_outcomes, total_deaths, pp) {
lchoose(round(total_outcomes), total_deaths) +
(total_deaths * log(pp)) +
Expand Down
10 changes: 2 additions & 8 deletions man/dot-estimate_severity.Rd

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

2 changes: 0 additions & 2 deletions man/dot-select_func_likelihood.Rd

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

8 changes: 2 additions & 6 deletions tests/testthat/_snaps/estimate_ascertainment.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@
Code
estimate_ascertainment(data = ebola1976, delay_density = function(x) dgamma(x,
shape = 2.4, scale = 3.33), severity_baseline = 0.7)
Message
Total cases = 245 and p = 0.959: using Normal approximation to binomial likelihood.
Output
ascertainment_estimate ascertainment_low ascertainment_high
1 0.7185383 0.7087172 0.8377214
1 0.7297748 0.7147963 0.7530931

# Static ascertainment from vignette

Code
estimate_ascertainment(data = covid_uk, delay_density = function(x) dlnorm(x,
meanlog = 2.577, sdlog = 0.44), severity_baseline = 0.014)
Message
Total cases = 283420 and p = 0.206: using Normal approximation to binomial likelihood.
Output
ascertainment_estimate ascertainment_low ascertainment_high
1 0.09810792 0.02316347 0.2167183
1 0.06779661 0.06734007 0.06829268

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/estimate_severity.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
severity_estimate
Output
severity_estimate severity_low severity_high
0.9742 0.8356 0.9877
0.9592 0.9295 0.9793

---

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/estimate_static.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
scfr_corrected
Output
severity_estimate severity_low severity_high
1 0.9742 0.8356 0.9877
1 0.9592 0.9295 0.9793

2 changes: 1 addition & 1 deletion tests/testthat/test-estimate_ascertainment.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ test_that("Ascertainment > 1.0 throws a warning", {
estimate_ascertainment(
data = ebola1976,
delay_density = function(x) dgamma(x, shape = 2.40, scale = 3.33),
severity_baseline = 0.9
severity_baseline = 0.99
),
regexp = "Ascertainment ratios > 1.0 detected, setting these values to 1.0"
)
Expand Down
19 changes: 3 additions & 16 deletions tests/testthat/test-estimate_severity.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,9 @@ test_that("Special cases of `.estimate_severity()`", {
poisson_threshold = 100
),
c(
severity_estimate = 1e-4, # lowest possible severity under this method
severity_low = NA_real_,
severity_high = NA_real_
)
)

total_outcomes <- 99
expect_identical(
.estimate_severity(
total_cases, total_deaths, total_outcomes,
poisson_threshold = 100
),
c(
severity_estimate = 1 - 1e-4, # highest possible severity
severity_low = NA_real_,
severity_high = NA_real_
severity_estimate = NA, # set NA because not valid calculation
severity_low = NA,
severity_high = NA
)
)

Expand Down
Binary file added tests/testthat/testthat-problems.rds
Binary file not shown.

0 comments on commit e79f954

Please sign in to comment.