Learn R Programming

mice (version 2.46.0)

cbind.mids: Columnwise combination of a mids object.

Description

Append mids objects by columns

Usage

# S3 method for mids
cbind(x, y = NULL, ...)

Arguments

x

A mids object.

y

A mids object or a data.frame, matrix, factor or vector.

Additional data.frame, matrix, vector or factor. These can be given as named arguments.

Value

An S3 object of class mids

Details

This function combines two mids objects columnwise into a single object of class mids, or combines a mids object with a vector, matrix, factor or data.frame columnwise into a mids object. The rows in the (incomplete) data x$data and y (or y$data if y is a mids object) should match. If y is a mids, then cbind only works if the number of imputations in x and y is equal.

See Also

rbind.mids, ibind, mids

Examples

Run this code
# NOT RUN {
# impute four variables at once (default)
imp <- mice(nhanes, m = 1, maxit = 1, print = FALSE)
imp$predictorMatrix

# impute two by two
data1 <- nhanes[, c("age", "bmi")]
data2 <- nhanes[, c("hyp", "chl")]
imp1 <- mice(data1, m = 2, maxit = 1, print = FALSE)
imp2 <- mice(data2, m = 2, maxit = 1, print = FALSE)

# Append two solutions
imp12 <- cbind(imp1, imp2)

# This is a different imputation model
imp12$predictorMatrix

# Append the other way around
imp21 <- cbind(imp2, imp1)
imp21$predictorMatrix

# Append 'forgotten' variable chl
data3 <- nhanes[, 1:3]
imp3  <- mice(data3, maxit = 1,m = 2, print = FALSE)
imp3a <- cbind(imp3, chl = nhanes$chl)

# Of course, chl was not imputed
head(complete(imp3a))

# Note: If one of the arguments is a data.frame 
# we need to explicitly call mice:::cbind.mids()
imp3b <- mice:::cbind.mids(imp3, data.frame(chl = nhanes$chl))
# }

Run the code above in your browser using DataLab