0

I have used the mice package for calculating imputations (10 iterations, 5 imputations). Because I am new to this area, my 'methodologist' - who is very patient with me! - want to judge the imputed values (so not the completed sets). I can't seem to find a way to collect all imputed values in one clear dataframe.

The data is about youngsters who answered a lot of questions in a 5 point Likert scale. I have several imp per age category. For example:

With my command imp_val_15_plus <- Filter(Negate(is.null), imp_15plus$imp)I can see all imputed values per question and per id. So for example imp_val_15plus[1:2] gives:

$X02_07
                                     1 2 3 4 5
qwertyuiop123456789                  4 4 4 4 4

$X02_12
                                     1 2 3 4 5
adfghjkl09823430233                  2 2 5 2 2
zcvnmoi987412597800                  1 2 1 1 2

So here are two questions (X02_07 and X02_12). The first has one NA (id qwe...789) and the latter has two NA's (id adf...0233 and zcvn...7800)

I would like to have a dataframe like this:

q_nr      id                                   1 2 3 4 5
$X02_07   qwertyuiop123456789                  4 4 4 4 4
$X02_12   adfghjkl09823430233                  2 2 5 2 2
$X02_12   zcvnmoi987412597800                  1 2 1 1 2

So I thought of a way to extract the values I need and then try to use a loop for all these values. I tried to extract the values:
names(imp_val_15plus[1]) gives me the question number [1] "X02_07"
row.names(imp_val_15plus[[1]]) gives me the id number [1] "qwertyuiop123456789"

But then I go wrong with the imputed values. With as.integer(imp_val_15plus[[1]]) I get [1] 3 3 3 3 3 instead of what I wanted [1] 4 4 4 4 4. The three's are logic, because of the factors available for question $X02_07. Normally there should be the factorlevels 1 - 5, but none of the youngsters used a 1, so my levels for this question are 2 - 5.

Have a look at str(imp_val_15plus[[1]]), it gives:

'data.frame':   1 obs. of  5 variables:
 $ 1: Factor w/ 4 levels "2","3","4","5": 3
  ..- attr(*, "contrasts")= num [1:4, 1:3] 0 1 0 0 0 0 1 0 0 0 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr  "2" "3" "4" "5"
  .. .. ..$ : chr  "2" "3" "4"
 $ 2: Factor w/ 4 levels "2","3","4","5": 3
  ..- attr(*, "contrasts")= num [1:4, 1:3] 0 1 0 0 0 0 1 0 0 0 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr  "2" "3" "4" "5"
  .. .. ..$ : chr  "2" "3" "4"
etc., etc.

It makes sense that I got the three's, because this is the number of the factor with the levels "2","3","4","5". How do I obtain the value of the value itself (the 4) instead of the 3? Or is there an other way to present all imputed values (not the completed set!!) in a neat way?

Marlein
  • 115
  • 2
  • 8
  • Take a look at my answer here https://stackoverflow.com/questions/50565145/stripplot-in-mice/50737746#50737746 I think it accomplishes what you want. You can add a specific dummy for the values that are imputed by creating a missing indicator `impL$M_x <- rep(is.na(impL[which(impL$Imputed == "Observed"),'x']),imp$m +1)`. Using this dummy you can look specifically at the imputed values for each dataset `impL[which(impL$Imputed == "Imputed" & impL$M_x == TRUE),c('.imp','x')]` – Niek Jun 13 '18 at 06:58
  • The issue you mention with `as.integer(imp_val_15plus[[1]])` can possibly be solved by first transforming the factor to a character `as.integer(as.character(imp_val_15plus[[1]]))` – Niek Jun 13 '18 at 06:59
  • I got a lot of errors using your solution, but it brought me to another approach, so thank you. Your second idea , using `as.integer(as.character(imp_val_15plus[[1]]))` cannot work, because it only sees the value and disregards the factorlevels. i.e. you get the same answer. But thanks for helping me! – Marlein Jun 13 '18 at 10:53
  • Glad I was of some help. Could you post a small example of the data or some data that is equal in structure to `imp_val_15plus`? This may help answer your question – Niek Jun 13 '18 at 13:00

0 Answers0