1
$\begingroup$

I have a categorical variable, var1 , that can take on values of W, B, A, M, N or P. There are some NAs that I want to impute using the mice package in R, but I know that the missing values cannot be "W" or "B" because those people said that they do not belong in that category. I want to impute var1 but force mice to only choose from everything except B or W .

Here is sample code for you to use:

df=data.frame(age=c(24,37,58,65,70,84, 56, 36, 48,23,15),
    var1 =c("B","W", NA, "A",NA, "P","N", NA, "M",NA, "B"),
    var1categ=c(0,0, 1, 1, 1,1,1,1,1,1, 0),
    ht = c(156, 169, 180, 175, 168, 165, 171, 158, 160, 175, 160))


imp=mice(df, remove_collinear = FALSE)

Let me know if you need more information.

$\endgroup$
  • $\begingroup$ A good question for a programming-related site; not so much for this one, though, because the question is about the mechanics of assigning values to data using code. $\endgroup$ – rolando2 Oct 7 at 11:41
  • $\begingroup$ @rolando2 I found here a similar question stats.stackexchange.com/questions/32179/… and I thought that people in this site focus on issues like these :) $\endgroup$ – Alexia S Oct 7 at 11:49
  • $\begingroup$ You're right that some programming questions without much statistical content per se slip through the cracks. But many such questions do get voted to be closed here. I'd try stackoverflow.com or another R-focused site. Cheers ~ $\endgroup$ – rolando2 Oct 7 at 11:55
  • $\begingroup$ @rolando2 I uploaded it also there. Thanks :) :) $\endgroup$ – Alexia S Oct 7 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.