Thanks for miceadds package. I'm using miceadds 3.5-14 with mice 3.6.0 in R 3.6.1.
Example is given below. Please let me know if I'm doing something incorrectly.
library(mice)
library(miceadds)
set.seed(1)
n.subjects <- 20
n.obs.per.subject <- 4
# x1 varies within subject (e.g. age)
# x2, x3, x4 and x5 are constant within subject
d <- data.frame(subject = rep(1:n.subjects, each = n.obs.per.subject),
x1 = round(rep(c(10, 15, 20, 25), n.subjects) +
rnorm(n.subjects*n.obs.per.subject, 0, 0.5), 2),
x2 = rep(sample(0:1, n.subjects, replace=TRUE), each = n.obs.per.subject),
x3 = factor(rep(sample(0:2, n.subjects, replace=TRUE),
each = n.obs.per.subject)),
x4 = round(rep(rnorm(n.subjects), each = n.obs.per.subject), 2),
x5 = round(rep(rnorm(n.subjects), each = n.obs.per.subject), 2),
y = round(rnorm(n.subjects*n.obs.per.subject), 2))
d[d$subject %in% 1:3, "x2"] <- NA
d[d$subject %in% 1:5, "x3"] <- NA
d[d$subject %in% 4:7, "x4"] <- NA
d[d$subject %in% 6:9, "x5"] <- NA
d[d$subject %in% 9:12, "y"] <- NA
d[c(1:2, 17:18), "y"] <- NA
methA <- c(subject = "",
x1 = "",
x2 = "2lonly.function",
x3 = "2lonly.function",
x4 = "2lonly.pmm",
x5 = "2lonly.pmm",
y = "2l.pan")
predMat <- matrix(1, nrow = ncol(d), ncol = ncol(d))
colnames(predMat) <- rownames(predMat) <- names(d)
predMat[, "subject"] <- -2
predMat["y", "x1"] <- 4
diag(predMat) <- 0
predMat
## subject x1 x2 x3 x4 x5 y
## subject 0 1 1 1 1 1 1
## x1 -2 0 1 1 1 1 1
## x2 -2 1 0 1 1 1 1
## x3 -2 1 1 0 1 1 1
## x4 -2 1 1 1 0 1 1
## x5 -2 1 1 1 1 0 1
## y -2 4 1 1 1 1 0
miceA <- mice(data = d,
method = methA,
predictorMatrix = predMat,
imputationFunction = list(x2 = "logreg", x3 = "polyreg"),
cluster_var = list(x2 = "subject", x3 = "subject"),
seed = 1)
## iter imp variable
## 1 1 x2 x3 x4 x5 y
## 1 2 x2 x3 x4 x5 yError in pan::pan(y1, subj, pred, xcol, zcol, prior, seed = s1, iter = paniter) :
## missing values in pred not allowed
# We also get the error if predMat["y", "x1"] is 1, 2 or 3.
# Different seed values in the call to mice() may get through different numbers of imputed data sets
# before producing the error.
# For comparison, use "polyreg" for x3 instead of "2lonly.function"
methB <- c(subject = "",
x1 = "",
x2 = "2lonly.function",
x3 = "polyreg",
x4 = "2lonly.pmm",
x5 = "2lonly.pmm",
y = "2l.pan")
miceB <- mice(data = d,
method = methB,
predictorMatrix = predMat,
imputationFunction = list(x2 = "logreg"),
cluster_var = list(x2 = "subject"))
# No error