-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing Data Fix, Partial Keys Fix #205
Conversation
- no documentation yet - no support for mixed structure insturments yet
TODO: Check if true, but beleive this is no longer needed now that we've redefined redcap_form_instance/redcap_event_instance
@@ -84,7 +84,9 @@ clean_redcap_long <- function(db_data_long, | |||
# Retrieve mixed structure fields and forms in reference df | |||
mixed_structure_ref <- get_mixed_structure_fields(db_data_long) %>% | |||
filter(.data$rep_and_nonrep & !str_ends(.data$field_name, "_form_complete")) %>% | |||
left_join(db_metadata_long %>% select("field_name", "form_name"), | |||
left_join( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No change, just lintr.
@@ -292,7 +295,7 @@ read_redcap <- function(redcap_uri, | |||
out <- add_metadata(out, db_metadata, redcap_uri, token, suppress_redcapr_messages) | |||
|
|||
if (is_longitudinal) { | |||
out <- add_event_mapping(out, linked_arms) | |||
out <- add_event_mapping(out, linked_arms, repeat_event_types) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add repeat_event_types
reference table to add_event_mapping()
#' | ||
add_event_mapping <- function(supertbl, linked_arms) { | ||
|
||
add_event_mapping <- function(supertbl, linked_arms, repeat_event_types) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add_event_mapping()
now accepts a reference table for the repeating event types designations. This table is joined onto the linked_arms
data to make repeat_type
available in the eventual redcap_events
supertibble column.
@@ -18,12 +18,13 @@ | |||
add_partial_keys <- function(db_data, | |||
var = NULL) { | |||
if (!is.null(enexpr(var))) { | |||
pattern <- "^(\\w+?)_arm_(\\d)$" | |||
# Include handling for instances where REDCap appends with "_1b" or similar | |||
pattern <- "^(\\w+?)_arm_(\\d+\\w?)$" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix regex to address #206
R/utils.R
Outdated
|
||
db_data <- db_data %>% | ||
mutate( | ||
redcap_event = sub(pattern, "\\1", {{ var }}), | ||
redcap_arm = as.integer(sub(pattern, "\\2", {{ var }})) | ||
redcap_arm = as.factor(sub(pattern, "\\2", {{ var }})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Convert to factor to capture non-integer arm values as mentioned in #206
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think character
is probably better here since these will usually be numbers and aren't the type of categorical variables you normally expect as factor to represent
@@ -416,14 +421,38 @@ convert_mixed_instrument <- function(db_data_long, mixed_structure_ref) { | |||
) | |||
) | |||
|
|||
repeat_together_present <- any( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we now want to separate RT events so their instances get captured under redcap_event_instance
, we first need to check for the right behavior for RTs (i.e. no repeating instrument but a repeat instance detected).
!is.na(db_data_long$redcap_repeat_instance) | ||
) | ||
|
||
if (!"redcap_event_instance" %in% names(db_data_long) && repeat_together_present) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If RT detected but no column supplied for redcap_event_instance
, add it as an empty column.
relocate(.data$redcap_event_instance, .after = .data$redcap_repeat_instance) | ||
} | ||
|
||
if (repeat_together_present) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally, shift the redcap repeat instance values over to the redcap event instance column.
( | ||
!is.na(.data$redcap_form_instance) | | ||
if_any(starts_with("redcap_event_instance"), ~ !is.na(.)) | ||
) & |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filter for when either redcap_form_instance
is NA
or redcap_event_instance
is NA
(if it exists).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One tiny comment but looks good otherwise
R/utils.R
Outdated
|
||
db_data <- db_data %>% | ||
mutate( | ||
redcap_event = sub(pattern, "\\1", {{ var }}), | ||
redcap_arm = as.integer(sub(pattern, "\\2", {{ var }})) | ||
redcap_arm = as.factor(sub(pattern, "\\2", {{ var }})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think character
is probably better here since these will usually be numbers and aren't the type of categorical variables you normally expect as factor to represent
Good call, thanks. I thought a factor would be better for how |
Description
This PR no longer implements
join_data_tibbles()
, that is being moved out into a new branch. Instead, this PR seeks to adress a few bugs, one minor (documented below), and one major where we are mistakenly reporting mixed data structure outputs.I will be posting comments on the code itself below to explain some of the changes.
The Missing Data Issue
Because we weren't making use of
redcap_event_instance
correctly, we were not accurately reporting all of the data associated with each REDCap event. See below from the Mixed Structure REDCap for reference:And here is what currently comes out of the REDCapTidieR data tibbles from
main
:Current Output with Missing Data
In the second data tibble (the repeat form), we should expect to see 4 entries, 2 for the non repeating event due to 2 repeating form instances, and 2 for the repeating together event entries.
In the third data tibble (the mixed structure form), we should expect to see 5 entries and are again missing the 2 repeating together event entries.
Here is the output with the proposed code changes:
Proposed Output with Missing Data Added
Proposed Changes
List changes below in bullet format:
add_event_mapping()
with output from new functionget_repeat_event_types()
and makerepeat_type
available in theredcap_events
column of the supertibbleclean_redcap_()
convert_mixed_instrument()
to detect RT data and shift instances over toredcap_event_instance
add_partial_keys()
to handle regex for arms that don't end in an integer (See [BUG] add_partial_keys() Doesn't fully capture arm regex #206)Add new<- in new branchjoin_data_tibbles()
function and testsRemaining TODO's
Finish updatingjoin_data_tibbles()
to handleby
appropriately for RT/RS dataSee comment in [BUG] Misleading Mixed Data Structure Outputs #204 about concerns<- <- in new branchIssue Addressed
Closes #206
Closes #204
PR Checklist
Before submitting this PR, please check and verify below that the submission meets the below criteria:
.RDS
) updated underinst/testdata/create_test_data.R
usethis::use_version()
Code Review
This section to be used by the reviewer and developers during Code Review after PR submission
Code Review Checklist