Repository navigation
as.mids() changes derived logical variables after complete(..., "long") round-trip #749
|
I may have found an issue with as.mids() when converting edited long-format imputed data back into a mids object. I imputed two binary variables first, then derived two additional logical variables from the completed long data. After converting the long data back using as.mids(), some derived values appeared to change unexpectedly. In particular, rows with remission_6m_upcr == TRUE and remission_6m_egfr == TRUE should give remission_6m_pr == FALSE, but after the round-trip I observed cases where remission_6m_pr became TRUE. Reproducible example In my actual data, some values of remission_6m_pr appeared to switch after converting back with as.mids(). Session info: Why is it the case? Thank you!! Best, |
Replies: 2 comments
|
Thanks for the detailed reprex, @irenerlover. This is not a bug in Two things worth noting first1. Type mismatch warning You will see:
dat_imp <- dat0 %>%
mutate(
remission_6m_upcr = factor(remission_6m_upcr),
remission_6m_egfr = factor(remission_6m_egfr)
)2. The "13 element mismatches" This is the symptom you observed. The cause is explained below. Why it happensWhen
The fixCompute derived variables after the round-trip, not before: # Step 1: convert types only — do NOT derive new variables yet
long_A <- complete(imp, action = "long", include = TRUE) %>%
arrange(.imp, .id) %>%
mutate(
remission_6m_upcr = as.logical(remission_6m_upcr),
remission_6m_egfr = as.logical(remission_6m_egfr)
)
# Step 2: round-trip
imp_back <- as.mids(long_A)
# Step 3: derive AFTER extracting completed datasets
long_B <- complete(imp_back, action = "long", include = TRUE) %>%
arrange(.imp, .id) %>%
mutate(
remission_6m = case_when(
is.na(remission_6m_upcr) | is.na(remission_6m_egfr) ~ NA,
remission_6m_upcr & remission_6m_egfr ~ TRUE,
TRUE ~ FALSE
),
remission_6m_pr = case_when(
is.na(remission_6m_upcr) & is.na(remission_6m_egfr) ~ NA,
remission_6m_upcr & remission_6m_egfr ~ FALSE,
TRUE ~ FALSE
)
)A simpler alternativeIf the only reason for the |
|
Thanks for the stefvanbuuren detailed answer. I tried to explain what happened in my case in my own words: This seems to be a special case (.imp = 0: NA + TRUE => TRUE; for .imp = 1, NA is filled with TRUE, but the TRUE on the right-hand side does not change at the same time because of the way as.mids() reconstructs the data:'pulls remission_6m_pr from mids$data' ), which appears to trigger this behavior. |

Thanks for the detailed reprex, @irenerlover.
This is not a bug in
mice— it is a consequence of howas.mids()reconstructs themidsobject.Two things worth noting first
1. Type mismatch warning
You will see:
logregexpects a factor, butremission_6m_upcrandremission_6m_egfrare stored asinteger. This works in practice but is fragile. The cleaner approach is to store them as factors from the start:2. The "13 element …