amices / mice

Multivariate Imputation by Chained Equations
https://amices.org/mice/
GNU General Public License v2.0
444 stars 107 forks source link

Error when converting imputed dataset into mids object (as.mids) in R #418

Closed rbrewster727 closed 3 years ago

rbrewster727 commented 3 years ago

I am attempting to convert a multiply imputed dataset into a mids object with the as.mids() command and receive the following error:

    Error in `[<-.data.frame`(`*tmp*`, j, value = list(enrollment_final_IMP = c(1L,  : 
      replacement element 1 has 15 rows, need 19
    In addition: Warning messages:
    1: In data.matrix(x) : NAs introduced by coercion
    2: In `[<-.data.frame`(`*tmp*`, j, value = list(enrollment_final_IMP = c(4L,  :
      replacement element 1 has 21 rows to replace 19 rows

Curiously, I've never previously encountered problems with this step and the error does not seem to be reproducible with any of the sample datasets (e.g. boys) or when I use a truncated version of my imputed dataset (with head).

The imputed dataframe was generated using mice and complete as part of the mice package. Below is a sample:


    > head(discon_peds_mids)
      sponsor_final_IMP enrollment_final_IMP      nct_id primary_purpose_final_IMP  br_phase4_IMP br_masking2_IMP br_allocation_IMP
    1          academic                50-99 NCT03515499                     other Not Applicable            None        Randomized
    2          academic                50-99 NCT03515499                     other Not Applicable            None        Randomized
    3          academic                50-99 NCT03515499                     other Not Applicable            None        Randomized
    4          academic                10-49 NCT04266925                Prevention Not Applicable            None    Non-Randomized
    5          academic                10-49 NCT04266925                Prevention Not Applicable            None    Non-Randomized
    6          academic                10-49 NCT04266925                Prevention Not Applicable            None    Non-Randomized
      has_dmc_IMP br_gni_lmic_hic_IMP facilities_final_IMP cardiac_and_vascular_disorders_IMP childhood_vaccines_IMP
    1       FALSE            HIC Only                    1                                  0                      0
    2       FALSE            HIC Only                    1                                  0                      0
    3       FALSE            HIC Only                    1                                  0                      0
    4       FALSE            HIC Only                    1                                  0                      0
    5       FALSE            HIC Only                    1                                  0                      0
    6       FALSE            HIC Only                    1                                  0                      0
      congenital_and_inherited_metabolic_diseases_IMP digestive_and_liver_disorders_IMP endocrine_disorders_IMP
    1                                               0                                 0                       0
    2                                               0                                 0                       0
    3                                               0                                 0                       0
    4                                               0                                 0                       0
    5                                               0                                 0                       0
    6                                               0                                 0                       0
      hematologic_disorders_IMP id_neglected_tropic_diseases_IMP id_other_infectious_disease_final_IMP id_respiratory_infections_IMP
    1                         0                                0                                     0                             0
    2                         0                                0                                     0                             0
    3                         0                                0                                     0                             0
    4                         0                                0                                     0                             0
    5                         0                                0                                     0                             0
    6                         0                                0                                     0                             0
      id_sexually_transmitted_infections_IMP immune_and_autoimmune_disorders_IMP injuries_IMP
    1                                      0                                   1            0
    2                                      0                                   1            0
    3                                      0                                   1            0
    4                                      0                                   0            0
    5                                      0                                   0            0
    6                                      0                                   0            0
      musculoskeletal_development_and_disorders_IMP mental_and_behavioral_disorders_IMP neonatal_care_and_disorders_IMP neoplasms_IMP
    1                                             0                                   1                               0             0
    2                                             0                                   1                               0             0
    3                                             0                                   1                               0             0
    4                                             1                                   0                               0             0
    5                                             1                                   0                               0             0
    6                                             1                                   0                               0             0
      neurological_disorders_IMP nutrition_IMP renal_and_urogenital_disorders_IMP respiratory_tract_disease_IMP
    1                          0             0                                  0                             0
    2                          0             0                                  0                             0
    3                          0             0                                  0                             0
    4                          0             0                                  0                             0
    5                          0             0                                  0                             0
    6                          0             0                                  0                             0
      sense_organ_disorders_IMP skin_and_subcutaneous_disorders_IMP anesthesia_critical_care_surgery_IMP other_final_IMP discon_outcome
    1                         0                                   0                                    0               0              0
    2                         0                                   0                                    0               0              0
    3                         0                                   0                                    0               0              0
    4                         0                                   0                                    0               0              0
    5                         0                                   0                                    0               0              0
    6                         0                                   0                                    0               0              0
      .imp .id
    1    0   4
    2    1   4
    3    2   4
    4    0   9
    5    1   9
    6    2   9

Thank you in advance for any recommendations!

stefvanbuuren commented 3 years ago

It must be something with the data. I am afraid we cannot help without a reprex.