# NOT RUN {
# impute the nhanes dataset
imp <- mice(nhanes, print = FALSE)
# extract the data in long format
X <- complete(imp, action = "long", include = TRUE)
# create dataset with .imp variable as numeric
X2 <- X
X2$.imp <- as.numeric(levels(X$.imp))[X$.imp]
# nhanes example without .id
test1 <- as.mids(X)
is.mids(test1)
all(complete(test1, action = "long", include = TRUE) == X, na.rm = TRUE)
# nhanes example without .id where .imp is numeric
test2 <- as.mids(X2)
is.mids(test2)
all(complete(test2, action = "long", include = TRUE) == X, na.rm = TRUE)
# nhanes example, where we explicitly specify .id as column 2
test3 <- as.mids(X, .id = 2)
is.mids(test3)
all(complete(test3, action = "long", include = TRUE) == X, na.rm = TRUE)
# nhanes example with .id where .imp is numeric
test4 <- as.mids(X2, .id = 2)
is.mids(test4)
all(complete(test4, action = "long", include = TRUE) == X, na.rm = TRUE)
# example without an .id variable
# variable .id not preserved
X3 <- X[, -2]
test5 <- as.mids(X3)
is.mids(test5)
all(complete(test5, action = "long", include = TRUE)[, -2] == X[, -2], na.rm = TRUE)
#
# }
Run the code above in your browser using DataLab