Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mice() changes the predictorMatrix #191

Closed
markseeto opened this issue Sep 3, 2019 · 2 comments
Closed

mice() changes the predictorMatrix #191

markseeto opened this issue Sep 3, 2019 · 2 comments

Comments

@markseeto
Copy link

Thanks for the mice package. I'm using version 3.6.0.

The example below shows mice() changing the predictorMatrix. It appears to be related to the similarity of the variable names, because it doesn't happen if one of the variable names is changed.

Am I misunderstanding something? Thanks.

library(mice)

set.seed(1)

# Repeated measurements of 'y' on each subject.
# 'score' and 'score.previous' are constant within each subject.

d <- data.frame(subject = 1:10,
                score = rnorm(10),
                score.previous = rnorm(10))
d[1:2, "score"] <- NA

DataA <- d[rep(1:10, each = 3), ]  # 3 observations per subject

DataA$y <- rnorm(nrow(DataA))
DataA[4:5, "y"] <- NA

iniA <- mice(DataA, maxit=0)

methA <- iniA$method
methA["score"] <- "2lonly.pmm"
methA["y"] <- "2l.pan"

predMatA <- iniA$predictorMatrix
predMatA[c("score", "score.previous", "y"), "subject"] <- -2

predMatA

##                subject score score.previous y
## subject              0     1              1 1
## score               -2     0              1 1
## score.previous      -2     1              0 1
## y                   -2     1              1 0

miceA <- mice(data = DataA, m = 5, maxit = 10,
              method = methA, predictorMatrix = predMatA)

miceA$predictorMatrix

##                subject score score.previous y
## subject              0     1              1 1
## score               -2     0              0 1
## score.previous      -2     1              0 1
## y                   -2     1              1 0

# Note that miceA$predictorMatrix["score", "score.previous"] is 0.

# Next, use the same data but change the name 'score.previous' to 'sp'.

DataB <- DataA

names(DataB)[names(DataB) == "score.previous"] <- "sp"

predMatB <- predMatA
rownames(predMatB) <- colnames(predMatB) <- names(DataB)

methB <- methA

names(methB) <- names(DataB)

miceB <- mice(data = DataB, m = 5, maxit = 10,
              method = methB, predictorMatrix = predMatB)

miceB$predictorMatrix

##         subject score sp y
## subject       0     1  1 1
## score        -2     0  1 1
## sp           -2     1  0 1
## y            -2     1  1 0

# miceB$predictorMatrix["score", "sp"] is 1.

stefvanbuuren added a commit that referenced this issue Sep 8, 2019
@stefvanbuuren
Copy link
Member

Thanks. That wasn't right indeed.

Now fixed.

@markseeto
Copy link
Author

Thanks Stef.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants