-1

In my data example

    data=structure(list(groupvar = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 
2L, 1L), v1 = c(27L, 52L, 92L, 86L, NA, 19L, 94L, NA, 26L, 94L, 
NA, 58L, 96L, 74L, 8L, 66L, 65L, 41L, 70L, 21L, 64L, 40L, 17L, 
7L, NA, 14L, 63L), v2 = c(59L, 91L, 45L, 40L, 56L, 17L, 72L, 
78L, 19L, 62L, 87L, NA, 79L, 62L, 40L, 67L, 93L, 1L, 64L, 22L, 
NA, 98L, 44L, 85L, 67L, 88L, 92L), v3 = c(97L, 15L, 27L, 55L, 
86L, 66L, NA, 61L, 27L, 47L, 93L, 68L, 72L, 4L, 35L, 69L, 65L, 
NA, 83L, 60L, 42L, NA, 90L, 81L, NA, 27L, 60L)), .Names = c("groupvar", 
"v1", "v2", "v3"), class = "data.frame", row.names = c(NA, -27L
))

There is groupvar (1 group and second group). I have many variable, but here only three. And there are many missing values in these variables. How can i perform multiple imputation for each variable(the type of variable can by numeric,int and so on), but for each group separately, using MICE

Edit

simple imp <- mice(data) is not give the need output, because i need by group I want that the result was

groupvar    v1  v2  v3
1          27   59  97
1          52   91  15
1          92   45  27
1          86   40  55
1         *64*  56  86
2          7    85  81
2          58*61,8* 68
2        64 *61,8*  42

** i marked example of imputed value

Community
  • 1
  • 1
psysky
  • 3,037
  • 5
  • 28
  • 64

1 Answers1

2

Group 'groupvar' as a factor.

data <- structure(list(groupvar = as.factor(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 
                                    2L, 1L)), 
                       v1 = c(27L, 52L, 92L, 86L, NA, 19L, 94L, NA, 26L, 94L, 
                              NA, 58L, 96L, 74L, 8L, 66L, 65L, 41L, 70L, 21L, 64L, 40L, 17L, 
                              7L, NA, 14L, 63L), 
                       v2 = c(59L, 91L, 45L, 40L, 56L, 17L, 72L, 
                              78L, 19L, 62L, 87L, NA, 79L, 62L, 40L, 67L, 93L, 1L, 64L, 22L, 
                              NA, 98L, 44L, 85L, 67L, 88L, 92L), 
                       v3 = c(97L, 15L, 27L, 55L, 
                              86L, 66L, NA, 61L, 27L, 47L, 93L, 68L, 72L, 4L, 35L, 69L, 65L, 
                              NA, 83L, 60L, 42L, NA, 90L, 81L, NA, 27L, 60L)),
                  .Names = c("groupvar", 
                             "v1", "v2", "v3"), class = "data.frame", row.names = c(NA, -27L
                             ))

Then use the mice package assuming the mice package is properly installed.

library(mice)
imp <- mice(data)
complete(imp)

   groupvar v1 v2 v3
1         1 27 59 97
2         1 52 91 15
3         1 92 45 27
4         1 86 40 55
5         1 21 56 86
6         1 19 17 66
7         1 94 72  4
8         1 66 78 61
9         1 26 19 27
10        2 94 62 47
11        2  8 87 93
12        2 58 72 68
13        2 96 79 72
14        2 74 62  4
15        2  8 40 35
16        2 66 67 69
17        2 65 93 65
18        2 41  1 47
19        2 70 64 83
20        2 21 22 60
21        2 64 62 42
22        1 40 98 27
23        1 17 44 90
24        2  7 85 81
25        1 63 67 55
26        2 14 88 27
27        1 63 92 60
Agriculturist
  • 521
  • 9
  • 20
  • Thank you Agriculturist, but, how to do, that imputation was for each group separately 1 and 2. cause now i see that it's performed for all group at once. Is it posiible to split it? – psysky Jul 13 '18 at 13:57
  • 1
    imp <- mice(data[data$groupvar == "1",]) – Agriculturist Jul 13 '18 at 14:24
  • if there is not 2 groups, but 100 groups, how can i be. i can't write 1000 string. Do you understand me? – psysky Jul 13 '18 at 14:34
  • The best way is split-apply-combine method using the plyr package. Convert numbers into strings using the as.character() function. You can use numbers, but numbers may get treated as indexes. So if "2" is in position 3 the group numbers may not make sense. Plenty of tutorials exist on split - apply - combine. – Agriculturist Jul 13 '18 at 17:37