Takes an object of class mids
, fills in the missing data, and returns
the completed data in a specified format.
Usage
# S3 method for class 'mids'
complete(
data,
action = 1L,
include = FALSE,
mild = FALSE,
order = c("last", "first"),
...
)
Arguments
- data
An object of class
mids
as created by the functionmice()
.- action
A numeric vector or a keyword. Numeric values between 1 and
data$m
return the data with imputation numberaction
filled in. The value ofaction = 0
return the original data, with missing values.action
can also be one of the following keywords:"all"
,"long"
,"broad"
and"repeated"
. See the Details section for the interpretation. The default isaction = 1L
returns the first imputed data set.- include
A logical to indicate whether the original data with the missing values should be included.
- mild
A logical indicating whether the return value should always be an object of class
mild
. Settingmild = TRUE
overridesaction
keywords"long"
,"broad"
and"repeated"
. The default isFALSE
.- order
Either
"first"
or"last"
. Only relevant whenaction == "long"
. Writes the".imp"
and".id"
in columns 1 and 2. The default isorder = "last"
. Included for backward compatibility with"< mice 3.16.0"
.- ...
Additional arguments. Not used.
Value
Complete data set with missing values replaced by imputations.
A data.frame
, or a list of data frames of class mild
.
Details
The argument action
can be length-1 character, which is
matched to one of the following keywords:
"all"
produces a
mild
object of imputed data sets. Wheninclude = TRUE
, then the original data are appended as the first list element;"long"
produces a data set where imputed data sets are stacked vertically. The columns are added: 1)
.imp
, integer, referring the imputation number, and 2).id
, character, the row names ofdata$data
;"stacked"
same as
"long"
but without the two additional columns;"broad"
produces a data set with where imputed data sets are stacked horizontally. Columns are ordered as in the original data. The imputation number is appended to each column name;
"repeated"
same as
"broad"
, but with columns in a different order.
Note
Technical note: mice 3.7.5
renamed the complete()
function
to complete.mids()
and exported it as an S3 method of the
generic tidyr::complete()
. Name clashes between
mice::complete()
and tidyr::complete()
should no
longer occur.
Examples
# obtain first imputed data set
sum(is.na(nhanes2))
#> [1] 27
imp <- mice(nhanes2, print = FALSE, maxit = 1)
dat <- complete(imp)
sum(is.na(dat))
#> [1] 0
# obtain stacked third and fifth imputation
dat <- complete(imp, c(3, 5))
# obtain all datasets, with additional identifiers
head(complete(imp, "long"))
#> age bmi hyp chl .imp .id
#> 1 20-39 30.1 no 229 1 1
#> 2 40-59 22.7 no 187 1 2
#> 3 20-39 33.2 no 187 1 3
#> 4 60-99 21.7 no 284 1 4
#> 5 20-39 20.4 no 113 1 5
#> 6 60-99 21.7 no 184 1 6
# same, but now as list, mild object
dslist <- complete(imp, "all")
length(dslist)
#> [1] 5
# same, but also include the original data
dslist <- complete(imp, "all", include = TRUE)
length(dslist)
#> [1] 6
# select original + 3 + 5, store as mild
dslist <- complete(imp, c(0, 3, 5), mild = TRUE)
names(dslist)
#> [1] "0" "3" "5"