title: mice: Convert long completed data to a mids object
body: <p>I have a longitudinal dataframe in wide format with waves separated by suffixes (e.g., &quot;_Wave1&quot;, &quot;_Wave2&quot;, etc.) that I would like to multiply impute and convert to long format. <a href="https://stat.ethz.ch/pipermail/r-help/2014-July/420107.html" rel="nofollow noreferrer">Similar unanswered question here</a>.</p>
  <p>First, I tried imputing the data <em>after</em> converting from wide to long, but I realized that <code>mice</code> would impute data that shouldn't have been imputed - for example, it would impute Scale 1 for the first wave of data collection (&quot;Wave 1&quot;), but Scale 1 was never completed at Wave 1, so it shouldn't have been imputed for that wave. At this point, I tried editing the prediction matrix, but since I've already converted to long format after imputation, all information about waves was lost.</p>
  <p>Then, I tried imputing <em>before</em> converting from wide to long, but encountered an error with <code>as.mids</code>. Converting from wide to long ends up making <code>.id</code> repeat, which causes <code>as.mids</code> to error.</p>
  <p>I would appreciate any insight or advice.</p>
  <pre><code>library(mice, warn.conflicts = FALSE)
  library(tidyverse)
  library(missMethods)
  set.seed(123)
  
  studyid &lt;- 1:10
  color &lt;- sample(c(&quot;red&quot;,&quot;blue&quot;,&quot;yellow&quot;), 10, replace=TRUE, prob=c(0.25, 0.25, 0.5))
  #scale_1_Wave1 &lt;- NA
  scale_2_Wave1 &lt;- sample(1:25, 10, replace=TRUE)
  scale_3_Wave1 &lt;- sample(1:25, 10, replace=TRUE)
  #scale_1_Wave2 &lt;- NA
  scale_2_Wave2 &lt;- sample(1:25, 10, replace=TRUE)
  scale_3_Wave2 &lt;- sample(1:25, 10, replace=TRUE)
  scale_1_Wave3 &lt;- sample(1:25, 10, replace=TRUE)
  scale_2_Wave3 &lt;- sample(1:25, 10, replace=TRUE)
  #scale_3_Wave3 &lt;- NA
  # Scale 1 wasn't given at Waves 1 or 2, only Wave 3
  # Scale 3 wasn't given at Wave 3
  
  df &lt;- data.frame(studyid, color,
                   scale_2_Wave1, scale_3_Wave1,
                   scale_2_Wave2, scale_3_Wave2,
                   scale_1_Wave3, scale_2_Wave3)
  
  # make some missingness
  df &lt;- delete_MCAR(df, p = 0.2, cols_mis = c(3:8))
  
  rm(list=setdiff(ls(), &quot;df&quot;))
  
  ## Attempt #1: convert wide to long, then impute
  
  df_long &lt;- df %&gt;%
    pivot_longer(contains(&quot;Wave&quot;), names_to = &quot;scalename&quot;, values_to = &quot;scalevalue&quot;) %&gt;%
    separate(scalename, c(&quot;scale&quot;, &quot;Wave&quot;), &quot;_Wave&quot;) %&gt;%
    pivot_wider(names_from = scale, values_from = scalevalue)
  
  imp1 &lt;- mice(df_long, m = 5, print = FALSE)
  #&gt; Warning: Number of logged events: 2
  complete1 &lt;- complete(imp1, action = &quot;long&quot;, include = TRUE)
  
  # Scale 1 shouldn't have any data at Waves 1 or 2, but it does
  ggplot(complete1, aes(x = Wave, y = scale_1)) + geom_boxplot()
  #&gt; Warning: Removed 22 rows containing non-finite values (stat_boxplot).
  
  ## Attempt #2: impute, then convert wide to long
  
  imp2 &lt;- mice(df, m = 5, print = FALSE)
  #&gt; Warning: Number of logged events: 2
  complete2 &lt;- complete(imp2, action = &quot;long&quot;, include = TRUE)
  
  # convert imputed data from wide to long
  complete_long &lt;- complete2 %&gt;%
    pivot_longer(contains(&quot;Wave&quot;), names_to = &quot;scalename&quot;, values_to = &quot;scalevalue&quot;) %&gt;%
    separate(scalename, c(&quot;scale&quot;, &quot;Wave&quot;), &quot;_Wave&quot;) %&gt;%
    pivot_wider(names_from = scale, values_from = scalevalue)
  
  # now try to convert it back to mids
  imp_long &lt;- as.mids(complete_long, .imp = 1, .id = 2)
  #&gt; Warning: non-unique values when setting 'row.names': '1', '2', '3', '4', '5',
  #&gt; '6', '7', '8', '9', '10'
  #&gt; Error in `.rowNamesDF&lt;-`(x, value = value): duplicate 'row.names' are not allowed
  Created on 2022-01-05 by the reprex package (v2.0.1)
  </code></pre>