Skip to content

Commit b2a2dd7

Browse files
authored
Merge pull request #1294 from cmu-delphi/bodge-E1
Fix E1 naming in survey microdata
2 parents 43b7d25 + 2e6cf7d commit b2a2dd7

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

facebook/delphiFacebook/R/responses.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ load_response_one <- function(input_filename, params, contingency_run) {
170170
input_data <- bodge_v4_translation(input_data, wave)
171171
input_data <- bodge_C6_C8(input_data, wave)
172172
input_data <- bodge_B13(input_data, wave)
173+
input_data <- bodge_E1(input_data, wave)
173174

174175
input_data <- code_symptoms(input_data, wave)
175176
input_data <- code_hh_size(input_data, wave)
@@ -486,6 +487,34 @@ bodge_B13 <- function(input_data, wave) {
486487
return(input_data)
487488
}
488489

490+
#' Fix E1_* names in Wave 11 data after ~June 16, 2021.
491+
#'
492+
#' Items E1_1 through E1_4 are part of a matrix. Qualtrics, for unknown reasons,
493+
#' switched to naming these E1_4 through E1_7 in June. Convert back to the
494+
#' intended names.
495+
#'
496+
#' @param input_data data frame of responses, before subsetting to select
497+
#' variables
498+
#' @param wave integer indicating survey version
499+
#'
500+
#' @return corrected data frame
501+
#' @importFrom dplyr rename
502+
bodge_E1 <- function(input_data, wave) {
503+
E14_present <- all(c("E1_1", "E1_2", "E1_3", "E1_4") %in% names(input_data))
504+
E47_present <- all(c("E1_4", "E1_5", "E1_6", "E1_7") %in% names(input_data))
505+
assert(!(E14_present && E47_present), "fields E1_1-E1_4 should not be present at the same time as fields E1_4-E1_7")
506+
507+
if ( E47_present ) {
508+
input_data <- rename(input_data,
509+
E1_1 = "E1_4",
510+
E1_2 = "E1_5",
511+
E1_3 = "E1_6",
512+
E1_4 = "E1_7"
513+
)
514+
}
515+
return(input_data)
516+
}
517+
489518
#' Process module assignment column.
490519
#'
491520
#' Rename `module` and recode to A/B/`NA`. Note: module assignment column name

facebook/delphiFacebook/man/bodge_E1.Rd

Lines changed: 22 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-responses.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,43 @@ test_that("B13 bodge works correctly", {
349349
input)
350350
})
351351

352+
test_that("E1 bodge works correctly", {
353+
input <- tibble(
354+
E1_1 = c(1, 2, 3, 1),
355+
E1_2 = c(1, 2, 3, 2),
356+
E1_3 = c(1, 2, 3, 3),
357+
E1_4 = c(1, 2, 3, 4)
358+
)
359+
expect_equal(bodge_E1(input, wave = NA), input)
360+
361+
input_4_to_7 <- tibble(
362+
E1_4 = c(1, 2, 3, 1),
363+
E1_5 = c(1, 2, 3, 2),
364+
E1_6 = c(1, 2, 3, 3),
365+
E1_7 = c(1, 2, 3, 4)
366+
)
367+
368+
expect_equal(bodge_E1(input_4_to_7, wave = NA), input)
369+
370+
input_mixed <- tibble(
371+
E1_4 = c(1, 2, 3, 1),
372+
E1_2 = c(1, 2, 3, 2),
373+
E1_3 = c(1, 2, 3, 3),
374+
E1_7 = c(1, 2, 3, 4)
375+
)
376+
expect_equal(bodge_E1(input_mixed, wave = NA), input_mixed)
377+
378+
input_both <- tibble(
379+
E1_1 = c(1, 2, 3, 1),
380+
E1_2 = c(1, 2, 3, 2),
381+
E1_3 = c(1, 2, 3, 3),
382+
E1_4 = c(1, 2, 3, 1),
383+
E1_5 = c(1, 2, 3, 2),
384+
E1_6 = c(1, 2, 3, 3),
385+
E1_7 = c(1, 2, 3, 4)
386+
)
387+
expect_error(bodge_E1(input_both, wave = NA),
388+
"fields E1_1-E1_4 should not be present at the same time as fields E1_4-E1_7"
389+
)
390+
})
391+

0 commit comments

Comments
 (0)