Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaitlyn Johnson committed Feb 17, 2025
1 parent 4c2b931 commit 0245241
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
40 changes: 14 additions & 26 deletions R/estimate_uncertainty.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,9 @@ estimate_uncertainty <- function(triangle_for_uncertainty,
matr_observed = matr_observed,
n_history = n_history
)

# Extracting results
exp_to_add_already_observed <- do.call(
rbind,
lapply(results, `[[`, "exp_to_add")
)
to_add_already_observed <- do.call(
rbind,
lapply(results, `[[`, "to_add")
)
}


## Method 2: Generate retrospective nowcasts using the delay distribution
# specified
if (!is.null(delay_pmf)) {
## Method 2: Generate retrospective nowcasts using the delay distribution
# specified
} else if (!is.null(delay_pmf)) {
.validate_delay_and_triangle(
triangle = triangle_for_uncertainty,
delay_pmf = delay_pmf
Expand All @@ -137,19 +124,20 @@ estimate_uncertainty <- function(triangle_for_uncertainty,
matr_observed = matr_observed,
delay_pmf = delay_pmf
)

# Extracting results
exp_to_add_already_observed <- do.call(
rbind,
lapply(results, `[[`, "exp_to_add")
)
to_add_already_observed <- do.call(
rbind,
lapply(results, `[[`, "to_add")
)
}


# Extracting results
exp_to_add_already_observed <- do.call(
rbind,
lapply(results, `[[`, "exp_to_add")
)
to_add_already_observed <- do.call(
rbind,
lapply(results, `[[`, "to_add")
)


disp_params <- vector(length = n_horizons)
# remove rows with zero initial reports (Christmas etc)
to_keep <- abs(exp_to_add_already_observed[, 1]) >= 0.1
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/test-estimate_uncertainty.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@ test_that("estimate_uncertainty function generates dispersion parameters", {
delay_pmf <- c(0.4, 0.3, 0.2, 0.1)

disp_params <- estimate_uncertainty(
triangle_to_nowcast = triangle,
triangle_for_uncertainty = triangle,
delay_pmf = delay_pmf
)
expect_vector(disp_params)
expect_length(disp_params, 3L)

# Test case 2: Custom n_history_dispersion
result2 <- estimate_uncertainty(triangle,
result2 <- estimate_uncertainty(
triangle_for_uncertainty = triangle,
delay_pmf,
n_history_dispersion = 4
)
expect_length(result2, 3L)

# Test case 3: n_history_dispersion too large
expect_error(
estimate_uncertainty(triangle,
estimate_uncertainty(
triangle_for_uncertainty = triangle,
delay_pmf,
n_history_dispersion = 10
),
Expand All @@ -41,7 +43,7 @@ test_that("estimate_uncertainty function generates dispersion parameters", {
wrong_delay_pmf <- c(0.5, 0.5)
expect_error(
estimate_uncertainty(
triangle,
triangle_for_uncertainty = triangle,
wrong_delay_pmf
),
regexp = "Length of the delay pmf is not the same as the number of delays"
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-validate_uncertainty_inputs.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ test_that(".validate_uncertainty_inputs provides correct warnings", {
# Test case 4: Warning when delay_pmf is provided
expect_warning(
.validate_uncertainty_inputs(matrix(1:16, 4, 4), c(0.5, 0.3, 0.2), 2, 2),
"The delay distribution specified will be used to compute "
regexp = "The delay distribution specified will be used to compute"
)

# Test case 5: Warning when delay_pmf is not provided
expect_warning(
.validate_uncertainty_inputs(matrix(1:16, 4, 4), NULL, 2, 2),
"No delay distribution was specified, therefore the delay "
"No delay distribution was specified, therefore the delay"
)
})

Expand Down

0 comments on commit 0245241

Please sign in to comment.