Skip to content

Commit b1f5df4

Browse files
authored
update rounding (#972)
1 parent c46e0f0 commit b1f5df4

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/get_webtext.R

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#' Rscript to generate texts for the visualization webpage
22
#' To run:
3-
#' Rscript src/get_webtext.R --reference-date "2025-02-22" --base-hub-path "."
4-
#' --hub-reports-path "../covidhub-reports"
3+
#' Rscript src/get_webtext.R --reference-date "2025-02-22"
4+
#' --hub-reports-path "../covidhub-reports"
55

66
parser <- argparser::arg_parser(
77
"Generate text for the webpage."
@@ -171,27 +171,28 @@ reporting_rate_flag <- if (length(latest_reporting_below80$location_name) > 0) {
171171
""
172172
}
173173

174-
format_statistical_values <- function(median, pi_lower, pi_upper) {
175-
half_width <- abs(pi_upper - pi_lower) / 2
176-
digits <- -floor(log10(half_width))
177-
c(
178-
median = round(median, digits = digits),
179-
lower = round(pi_lower, digits = digits),
180-
upper = round(pi_upper, digits = digits)
181-
)
174+
round_to_place <- function(value) {
175+
if (value >= 1000) {
176+
rounded_val <- round(value, -2)
177+
} else if (value >= 10) {
178+
rounded_val <- round(value, -1)
179+
} else {
180+
rounded_val <- round(value, 0)
181+
}
182+
return(rounded_val)
182183
}
183184

184185
# generate variables used in the web text
185-
forecast_1wk_ahead <-
186-
format_statistical_values(
187-
ensemble_us_1wk_ahead$quantile_0.5_count,
188-
ensemble_us_1wk_ahead$quantile_0.025_count,
189-
ensemble_us_1wk_ahead$quantile_0.975_count
190-
)
191186

192-
median_forecast_1wk_ahead <- forecast_1wk_ahead["median"]
193-
lower_95ci_forecast_1wk_ahead <- forecast_1wk_ahead["lower"]
194-
upper_95ci_forecast_1wk_ahead <- forecast_1wk_ahead["upper"]
187+
median_forecast_1wk_ahead <- round_to_place(
188+
ensemble_us_1wk_ahead$quantile_0.5_count
189+
)
190+
lower_95ci_forecast_1wk_ahead <- round_to_place(
191+
ensemble_us_1wk_ahead$quantile_0.025_count
192+
)
193+
upper_95ci_forecast_1wk_ahead <- round_to_place(
194+
ensemble_us_1wk_ahead$quantile_0.975_count
195+
)
195196

196197
designated <- wkly_submissions[wkly_submissions$designated_model, ]
197198
not_designated <- wkly_submissions[!wkly_submissions$designated_model, ]

0 commit comments

Comments
 (0)