mice
mice copied to clipboard
mice::ampute() not working properly when adding character variables
I was using the mice::ampute()
under MAR with a very simple dataset. Probabilities will be based on a continuous distribution.
set.seed(123)
age <- rnorm(50, 40, 10)
out <- 20 + 2 * age
anl <- data.frame(SUBJID = subject, AGE = age, OUT = out)
Only out variable is at risk of being amputed.
pattern_missing <- matrix(c(1, 1, 0), nrow = 1)
Missingness will depend entirely on the AGE variable
weights <- matrix(c(0, 1, 0), nrow = 1)
Now I am going over the amputation process under MAR:
result <- ampute(
anl, prop = 0.4,
patterns = pattern_missing,
mech = "MAR",
weights = weights,
type = "RIGHT")
While I was checking the missingness as a function of age, to see whether missingness has been applied under MAR, I saw the following unexpected result (I was checking for different missing proportions), where missingness is not generated under MAR.
The issue comes from the following line under sumscores. Since the function is converting the SUBJID
column to NA
, this matrix multiplication is returning NA, while this should have returned the weighted sum scores.
scores <- apply(candidates, 1, function(x) weights[i, ] %*% x)
See the scores
output full of NA
s.
Could the function somehow return an error, instead of an incorrect result, so that the user can prevent from adding character variables such as subject id? This issue is somehow related to this one I thought that just adding character variables was not harming the process, but I see I was not under the correct assumption.