Learn R Programming

mice (version 2.46.0)

as.mids: Converts an multiply imputed dataset (long format) into a mids object

Description

This function converts imputed data stored in long format into an object of class mids. The original incomplete dataset needs to be available so that we know where the missing data are. The function is useful to convert back operations applied to the imputed data back in a mids object. It may also be used to store multiply imputed data sets from other software into the format used by mice.

Usage

as.mids(data, .imp = NA, .id = NA)

Arguments

data

A multiply imputed data set in long format, for example produced by a call to complete(..., action = 'long', include = TRUE).

.imp

Optional column number in data that contains the imputation number. The number 0 indicates the original data (with missings) and 1 through m correspond to the m imputation number. If not specified, the function searches for a variable named ".imp".

.id

Optional column number in data indicating the subject identification. If not specified, then the function searches for a variable named .id in data.

Value

An object of class mids

Details

If .id variable is found, row names from the supplied data will be copied to the data elements of the returned mids object.

Examples

Run this code
# 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