title: R - mice - adding a column that sums columns with imputed values
body: <p>I have a database with missing data. I need to impute data (I am using mice), then create new columns based on the original columns (using the imputed data). It is these new columns that I need to do my statistical analyses with.</p>
  
  <p>Specifically, my participants filled in several questionnaires using a 7-point likert scale. Some didn't answer all the questions. I need to impute values, then 
      1- sum the values in the columns and have access to this new value for statistical analyses
      2- depending on this sum, divide the participants into "mild, moderate, high" and use this for statistical analyses.</p>
  
  <p>I have based what I am trying to do on this stackoverflow answer:
  <a href="https://stackoverflow.com/questions/26667162/perform-operation-on-each-imputed-dataset-in-rs-mice">Perform operation on each imputed dataset in R&#39;s MICE</a></p>
  
  <p>Here is my code (using R):</p>
  
  <pre><code># Create a sample bdd
  bdd=data.frame(
      gender=c("M","F","M", "M", "M", "F"),
      choice=c(1,2,NA,1,1,1),
      gardes=c(0,0,0,5,7,NA),
      EE1=c(3,4,1,NA,3,0),
      EE2=c(2,5,1,3,3,0),
      EE3=c(3,NA,1,5,3,0),
      EE4=c(3,6,1,2,3,0),
      EE5=c(1,4,1,2,3,5),
      EE6=c(3,1,1,3,3,4),
      EE7=c(5,0,1,5,3,5),
      EE8=c(2,6,1,1,3,3),
      EE9=c(3,4,1,6,3,4)
      )
  
  # Create the additional variable - this will have missing values
  bdd$EE &lt;- bdd$EE1+bdd$EE2+bdd$EE3+bdd$EE4+bdd$EE5+bdd$EE6+bdd$EE7+bdd$EE8+bdd$EE9
  
  # create ini to get access to meth and pred
  ini &lt;- mice(bdd, max = 0, print = FALSE)
  
  # Change the method of imputation for EE, so that it always equals bdd$EE1+...+bdd$EE9
  meth1 &lt;- ini$meth
  meth1["EE"] &lt;- "~I(bdd$EE1+bdd$EE2+bdd$EE3+bdd$EE4+bdd$EE5+bdd$EE6+bdd$EE7+bdd$EE8+bdd$EE9)"
  
  pred1 &lt;- ini$pred  
  # change the predictor matrix so only bdd$EE1-9 predicts EE (necessary?)
  pred1[ "EE", ] &lt;- 0 
  pred1[ "EE", c("EE1", "EE2", "EE3", "EE4", "EE5", "EE6", "EE7", "EE8", "EE9")] &lt;- 1
  # change the predictor matrix so that EE isnt used to predict
  pred1[ , "EE" ] &lt;- 0  
  
  
  # Imputations
  imput &lt;- mice(bdd, seed=1, pred = pred1, meth = meth1, m=1, print = FALSE)
  </code></pre>
  
  <p>Please note that this does not work. Any other way to do this elegantly? TIA for any and all advice!!!</p>
  
  <p>Edited to add: this is the error message I get when I try to run this code:</p>
  
  <pre><code>Warning messages:
  1: In `[&lt;-.data.frame`(`*tmp*`, , i, value = list(`1` = c(20L, 14L,  :
      replacement element 1 has 456 rows to replace 2 rows
  2: In `[&lt;-.data.frame`(`*tmp*`, , i, value = list(`1` = c(20L, 14L,  :
      replacement element 1 has 456 rows to replace 2 rows
  3: In `[&lt;-.data.frame`(`*tmp*`, , i, value = list(`1` = c(20L, 14L,  :
      replacement element 1 has 456 rows to replace 2 rows
  4: In `[&lt;-.data.frame`(`*tmp*`, , i, value = list(`1` = c(20L, 14L,  :
      replacement element 1 has 456 rows to replace 2 rows
  5: In `[&lt;-.data.frame`(`*tmp*`, , i, value = list(`1` = c(20L, 14L,  :
      replacement element 1 has 456 rows to replace 2 rows
  </code></pre>
  
  <p>Here is the bdd I created for this question:</p>
  
  <pre><code>      gender choice gardes EE1 EE2 E3 EE4 EE5 EE6 E7 EE8 EE9
  1      M      1      0   3   2  3   3   1   3  5   2   3
  2      F      2      0   4   5 NA   6   4   1  0   6   4
  3      M     NA      0   1   1  1   1   1   1  1   1   1
  4      M      1      5  NA   3  5   2   2   3  5   1   6
  5      M      1      7   3   3  3   3   3   3  3   3   3
  6      F      1     NA   0   0  0   0   5   4  5   3   4
  </code></pre>