3
$\begingroup$

I am having a problem using MICE, where it generates the following warning:

Warning message:
In var(data[, j], na.rm = TRUE) : NAs introduced by coercion

This seems to be related to the presence of a unique alphanumeric ID variable in the data. For example:

require(mice)

imp <- mice(nhanes,maxit=0)
# no problem

nhanes$id <- 1:25
imp <- mice(nhanes,exclude="id",maxit=0)
# no problem

nhanes$id <- LETTERS[1:25]
imp <- mice(nhanes,exclude="id",maxit=0)

The latter generates the warning:

In var(data[, j], na.rm = TRUE) : NAs introduced by coercion

The predictor matrix is

imp$pred

    age bmi hyp chl id
age   0   0   0   0  0
bmi   1   0   1   1  0
hyp   1   1   0   1  0
chl   1   1   1   0  0
id    0   0   0   0  0

Note that here id is not used to impute anything. So it seems strange that a warning would be generated at all, in this simple case. Even this generates the same warning:

pred <- imp$pred

imp <- mice(
  data=nhanes,
  m = 5,  
  maxit=5,
  imputationMethod = c(
    "norm",  # age
    "norm",  # bmi
    "norm",  # hyp
    "norm",  # chl
    "")    ,  # id
  predictorMatrix = pred
)

where id is not specified to be imputed or to be used for imputation in the predictor matrix, and the imputation method is ""

So my questions are:

  1. Why is this warning generated ?
  2. How can I prevent this warning, without removing the ID variable from the data. Of course I don't use the ID variable for imputing other variables - it's just a bit inconvenient to remove it.
  3. If it can't be prevented, can I safely ignore the warning and use the predictor matrix for imputing my data (after setting the id column to zeros) ?
$\endgroup$

closed as off-topic by kjetil b halvorsen, Peter Flom Nov 15 '18 at 11:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – kjetil b halvorsen, Peter Flom
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ Have you contacted the package maintainer yet? In my experience, he is very open to suggestions (especially if you provide him with a reproducible example). $\endgroup$ – Nick Sabbe Apr 10 '13 at 13:09
4
$\begingroup$

Interesting. This is subtle R behavior. If we do

nhanes$id <- LETTERS[1:25] 

the matrix nhanes gets transformed into a data.frame, but it does not convert id into a factor, as one might expect, and leaves it character (mice does not handle character variables). I did not know that this was possible, but it may make sense in particular applications.

On the other hand, using the preferred syntax

nhanes <- data.frame(nhanes, id = LETTERS[1:25])

will also convert nhanes into a data.frame and converts id into factor, which eliminates the warning.

$\endgroup$
  • $\begingroup$ Thanks Stef. My data has 700,000 unique obs so I'm not sure it's a good idea to have a factor variable with this many levels. At the moment I am removing the variable prior to imputation then adding it back with cbind.mids afterwards. I'd prefer not to do this if possible - is it safe to ignore the warning ? Also, after cbind.mids, is there an easy way to rename the variable, since it seems to always be named y ? $\endgroup$ – Robert Long Apr 12 '13 at 9:15
  • $\begingroup$ Ah yes, making a factor of 700,000 unique values is a recipe for disaster. It is safe to ignore the warning, but I will adapt the code in the next release to prevent it. Current behavior is that character variables are simply copied, and not imputed. This seems appropriate in your case. $\endgroup$ – Stef van Buuren Apr 12 '13 at 20:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.