Functions cbind()
and rbind()
are defined in
the mice
package in order to
enable dispatch to cbind.mids()
and rbind.mids()
when one of the arguments is a data.frame
.
Arguments
- ...
Arguments passed on to
base::cbind
deparse.level
integer controlling the construction of labels in the case of non-matrix-like arguments (for the default method):
deparse.level = 0
constructs no labels;
the defaultdeparse.level = 1
typically anddeparse.level = 2
always construct labels from the argument names, see the ‘Value’ section below.
Details
The standard base::cbind()
and base::rbind()
always dispatch to
base::cbind.data.frame()
or base::rbind.data.frame()
if one of the arguments is a
data.frame
. The versions defined in the mice
package intercept the user command
and test whether the first argument has class "mids"
. If so,
function calls cbind.mids()
, respectively rbind.mids()
. In
all other cases, the call is forwarded to standard functions in the
base
package.
The cbind.mids()
function combines two mids
objects
columnwise into a single
object of class mids
, or combines a single mids
object with
a vector
, matrix
, factor
or data.frame
columnwise into a mids
object.
If both arguments of cbind.mids()
are mids
-objects, the
data
list components should have the same number of rows. Also, the
number of imputations (m
) should be identical.
If the second argument is a matrix
,
factor
or vector
, it is transformed into a
data.frame
. The number of rows should match with the data
component of the first argument.
The cbind.mids()
function renames any duplicated variable or block names by
appending ".1"
, ".2"
to duplicated names.
The rbind.mids()
function combines two mids
objects rowwise into a single
mids
object, or combines a mids
object with a vector, matrix,
factor or data frame rowwise into a mids
object.
If both arguments of rbind.mids()
are mids
objects,
then rbind.mids()
requires that both have the same number of multiple
imputations. In addition, their data
components should match.
If the second argument of rbind.mids()
is not a mids
object,
the columns of the arguments should match. The where
matrix for the
second argument is set to FALSE
, signalling that any missing values in
that argument were not imputed. The ignore
vector for the second argument is
set to FALSE
. Rows inherited from the second argument will therefore
influence the parameter estimation of the imputation model in any future
iterations.
Note
The cbind.mids()
function constructs the elements of the new mids
object as follows:
data | Columnwise combination of the data in x and y |
imp | Combines the imputed values from x and y |
m | Taken from x$m |
where | Columnwise combination of x$where and y$where |
blocks | Combines x$blocks and y$blocks |
call | Vector, call[1] creates x , call[2]
is call to cbind.mids() |
nmis | Equals c(x$nmis, y$nmis) |
method | Combines x$method and y$method |
predictorMatrix | Combination with zeroes on the off-diagonal blocks |
visitSequence | Combined as c(x$visitSequence, y$visitSequence) |
formulas | Combined as c(x$formulas, y$formulas) |
post | Combined as c(x$post, y$post) |
blots | Combined as c(x$blots, y$blots) |
ignore | Taken from x$ignore |
seed | Taken from x$seed |
iteration | Taken from x$iteration |
lastSeedValue | Taken from x$lastSeedValue |
chainMean | Combined from x$chainMean and y$chainMean |
chainVar | Combined from x$chainVar and y$chainVar |
loggedEvents | Taken from x$loggedEvents |
version | Current package version |
date | Current date |
The rbind.mids()
function constructs the elements of the new mids
object as follows:
data | Rowwise combination of the (incomplete) data in x and y |
imp | Equals rbind(x$imp[[j]], y$imp[[j]]) if y is mids object; otherwise
the data of y will be copied |
m | Equals x$m |
where | Rowwise combination of where arguments |
blocks | Equals x$blocks |
call | Vector, call[1] creates x , call[2] is call to rbind.mids |
nmis | x$nmis + y$nmis |
method | Taken from x$method |
predictorMatrix | Taken from x$predictorMatrix |
visitSequence | Taken from x$visitSequence |
formulas | Taken from x$formulas |
post | Taken from x$post |
blots | Taken from x$blots |
ignore | Concatenate x$ignore and y$ignore |
seed | Taken from x$seed |
iteration | Taken from x$iteration |
lastSeedValue | Taken from x$lastSeedValue |
chainMean | Set to NA |
chainVar | Set to NA |
loggedEvents | Taken from x$loggedEvents |
version | Taken from x$version |
date | Taken from x$date |
References
van Buuren S and Groothuis-Oudshoorn K (2011). mice
:
Multivariate Imputation by Chained Equations in R
. Journal of
Statistical Software, 45(3), 1-67.
doi:10.18637/jss.v045.i03
Examples
# --- cbind ---
# impute four variables at once (default)
imp <- mice(nhanes, m = 1, maxit = 1, print = FALSE)
imp$predictorMatrix
#> age bmi hyp chl
#> age 0 1 1 1
#> bmi 1 0 1 1
#> hyp 1 1 0 1
#> chl 1 1 1 0
# 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
#> age bmi hyp chl
#> age 0 1 0 0
#> bmi 1 0 0 0
#> hyp 0 0 0 1
#> chl 0 0 1 0
# Append the other way around
imp21 <- cbind(imp2, imp1)
imp21$predictorMatrix
#> hyp chl age bmi
#> hyp 0 1 0 0
#> chl 1 0 0 0
#> age 0 0 0 1
#> bmi 0 0 1 0
# Append 'forgotten' variable chl
data3 <- nhanes[, 1:3]
imp3 <- mice(data3, maxit = 1, m = 2, print = FALSE)
imp4 <- cbind(imp3, chl = nhanes$chl)
# Of course, chl was not imputed
head(complete(imp4))
#> age bmi hyp chl
#> 1 1 20.4 1 NA
#> 2 2 22.7 1 187
#> 3 1 20.4 1 187
#> 4 3 21.7 1 NA
#> 5 1 20.4 1 113
#> 6 3 24.9 2 184
# Combine mids object with data frame
imp5 <- cbind(imp3, nhanes2)
head(complete(imp5))
#> age bmi hyp age.1 bmi.1 hyp.1 chl
#> 1 1 20.4 1 20-39 NA <NA> NA
#> 2 2 22.7 1 40-59 22.7 no 187
#> 3 1 20.4 1 20-39 NA no 187
#> 4 3 21.7 1 60-99 NA <NA> NA
#> 5 1 20.4 1 20-39 20.4 no 113
#> 6 3 24.9 2 60-99 NA <NA> 184
# --- rbind ---
imp1 <- mice(nhanes[1:13, ], m = 2, maxit = 1, print = FALSE)
#> Warning: Number of logged events: 1
imp5 <- mice(nhanes[1:13, ], m = 2, maxit = 2, print = FALSE)
#> Warning: Number of logged events: 1
mylist <- list(age = NA, bmi = NA, hyp = NA, chl = NA)
nrow(complete(rbind(imp1, imp5)))
#> Warning: iterations differ, so no convergence diagnostics calculated
#> [1] 26
nrow(complete(rbind(imp1, mylist)))
#> [1] 14
nrow(complete(rbind(imp1, data.frame(mylist))))
#> [1] 14
nrow(complete(rbind(imp1, complete(imp5))))
#> [1] 26