Skip to content

Commit fffdd82

Browse files
authored
Merge pull request #1379 from cmu-delphi/survey-prevent-6d-7dav
Suppress too-recent smoothed weighted indicators
2 parents 70f6a2f + 2973229 commit fffdd82

File tree

9 files changed

+50
-9
lines changed

9 files changed

+50
-9
lines changed

facebook/delphiFacebook/NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ importFrom(stats,na.omit)
112112
importFrom(stats,setNames)
113113
importFrom(stats,weighted.mean)
114114
importFrom(stringi,stri_extract)
115+
importFrom(stringi,stri_extract_first)
115116
importFrom(stringi,stri_replace)
116117
importFrom(stringi,stri_replace_all)
117118
importFrom(stringi,stri_split)

facebook/delphiFacebook/R/aggregate.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ summarize_indicators_day <- function(day_df, indicators, target_day, geo_level,
179179
var_weight <- indicators$var_weight[row]
180180
compute_fn <- indicators$compute_fn[[row]]
181181

182+
# Prevent smoothed weighted signals from being reported for dates after
183+
# the latest available weight data.
184+
if (target_day > params$latest_weight_date &&
185+
indicators$smooth_days[row] > 1 &&
186+
indicators$var_weight[row] != "weight_unif") {
187+
188+
next
189+
}
190+
182191
# Copy only columns we're using.
183192
select_cols <- c(metric, var_weight, "weight_in_location")
184193
# This filter uses `x[, cols, with=FALSE]` rather than the newer

facebook/delphiFacebook/R/contingency_run.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ run_contingency_tables_one_period <- function(params, aggregations)
144144
return()
145145
}
146146

147-
data_agg <- join_weights(data_agg, params, weights = "full")
147+
data_agg <- join_weights(data_agg, params, weights = "full")$df
148148
msg_df("response data to aggregate", data_agg)
149149

150150
produce_aggregates(data_agg, aggregations, cw_list, params)

facebook/delphiFacebook/R/run.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ run_facebook <- function(params)
2727
# create data that will be aggregated for covidcast
2828
data_agg <- create_data_for_aggregation(input_data)
2929
data_agg <- filter_data_for_aggregation(data_agg, params, lead_days = 12)
30-
data_agg <- join_weights(data_agg, params, weights = "step1")
30+
weight_result <- join_weights(data_agg, params, weights = "step1")
31+
data_agg <- weight_result$df
32+
latest_weight_date_step1 <- weight_result$weight_date
3133
msg_df("response data to aggregate", data_agg)
3234

35+
params$latest_weight_date <- ifelse(
36+
is.na(latest_weight_date_step1), as.Date(params$end_date), latest_weight_date_step1
37+
)
38+
3339
# create "complete" data that will be shared with research partners
3440
data_full <- create_complete_responses(input_data, cw_list$county, params)
3541
data_full <- filter_complete_responses(data_full, params)
36-
data_full <- join_weights(data_full, params, weights = "full")
42+
data_full <- join_weights(data_full, params, weights = "full")$df
3743
msg_df("full data to share with research partners", data_full)
3844

3945
## Set default number of cores for mclapply to the total available number,

facebook/delphiFacebook/R/weights.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ write_cid <- function(data, type_name, params)
4747
#'
4848
#' @importFrom dplyr bind_rows left_join
4949
#' @importFrom data.table fread
50+
#' @importFrom stringi stri_extract_first
5051
#'
5152
#' @export
5253
join_weights <- function(data, params, weights = c("step1", "full"))
@@ -61,6 +62,11 @@ join_weights <- function(data, params, weights = c("step1", "full"))
6162

6263
weights_files <- dir(params$weights_in_dir, pattern = pattern, full.names = TRUE)
6364
weights_files <- sort(weights_files)
65+
66+
latest_weight <- tail(weights_files, n = 1)
67+
latest_weight_date <- as.Date(
68+
stri_extract_first(latest_weight, regex = "^[0-9]{4}-[0-9]{2}-[0-9]{2}")
69+
)
6470

6571
col_types <- c("character", "double")
6672
col_names <- c("cid", "weight")
@@ -74,5 +80,5 @@ join_weights <- function(data, params, weights = c("step1", "full"))
7480
agg_weights <- agg_weights[!duplicated(cid),]
7581
data <- left_join(data, agg_weights, by = c("token" = "cid"))
7682

77-
return(data)
83+
return( list(df = data, weight_date = latest_weight_date) )
7884
}

facebook/delphiFacebook/integration-tests/testthat/test-contingency-run.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ rand_weights <- rand_weights / sum(rand_weights)
163163

164164
mock_join_weights <- function(data, params, weights = c("step1", "full")) {
165165
data <- cbind(as.data.table(data), weight=rand_weights)
166-
return(data)
166+
return( list(df = data, weight_date = NA) )
167167
}
168168

169169
mock_mix_weights <- function(weights, s_mix_coef, s_weight) {

facebook/delphiFacebook/integration-tests/testthat/test-integration.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ test_that("testing weighted community values files", {
152152
params <- relativize_params(read_params(test_path("params-test.json")))
153153

154154
input_data <- load_responses_all(params)
155-
input_data <- join_weights(input_data, params)
155+
input_data <- join_weights(input_data, params)$df
156156

157157
# there are 2 / 4 households in PA on 2020-05-11 for community
158158
these <- input_data[input_data$date == "2020-05-11" & input_data$zip5 == "15106",]
@@ -194,7 +194,7 @@ test_that("testing weighted smoothed community values files", {
194194
params <- relativize_params(read_params(test_path("params-test.json")))
195195

196196
input_data <- load_responses_all(params)
197-
input_data <- join_weights(input_data, params)
197+
input_data <- join_weights(input_data, params)$df
198198

199199
# there are 2 / 4 households in PA on 2020-05-11 for community
200200
these <- input_data[
@@ -328,7 +328,7 @@ test_that("testing weighted ili/cli values files", {
328328
params <- relativize_params(read_params(test_path("params-test.json")))
329329

330330
input_data <- load_responses_all(params)
331-
input_data <- join_weights(input_data, params)
331+
input_data <- join_weights(input_data, params)$df
332332
data_agg <- create_data_for_aggregation(input_data)
333333

334334
## There are 4 households in PA on 2020-05-11, one with ILI.

facebook/delphiFacebook/man/code_vaccinated_breakdown.Rd

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

facebook/delphiFacebook/unit-tests/testthat/test-weights.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ test_that("testing join_weights command", {
3333
fake_data <- tibble(
3434
token = c("DSFIJBjAexoQjDStr", "mGDsqbweUYzFnmZUH", "zocUNXYISDyYcVIQn")
3535
)
36-
fake_data_w <- join_weights(fake_data, list(weights_in_dir = "weights_in"))
36+
fake_data_w <- join_weights(fake_data, list(weights_in_dir = "weights_in"))$df
3737

3838
expect_true(max(abs(expected_weights - fake_data_w$weight)) < 1e-7)
3939

0 commit comments

Comments
 (0)