| impute_missing_values | R Documentation |
Replaces missing (NA) values in a dataset using either simple summary
statistics or k-nearest-neighbours (kNN) imputation.
impute_missing_values(
data,
...,
method = "auto",
constant = NULL,
k = 5,
ignore = NULL
)
data |
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). |
... |
< |
method |
Imputation method. One of |
constant |
Value used to replace missing values when
|
k |
Number of nearest neighbours to use when |
ignore |
< |
Two families of imputation are provided:
Each selected variable (column) is imputed independently using a summary statistic of its own non-missing values.
"auto" (default): numeric variables are imputed with their
median and non-numeric variables with their mode (most frequent value).
"median" / "mean": numeric variables are imputed with
the chosen statistic. Non-numeric variables fall back to the mode (a
message is emitted) as means and medians are undefined for them.
"mode": all selected variables are imputed with their mode.
"constant": all selected variables have missing values
replaced with constant.
method = "knn")Missing values are imputed from
the k most similar rows (samples), using all other variables as
predictors. Similarity is measured with Gower distance, which handles mixed
numeric and categorical data. This method uses kNN from
the VIM package, which is only required (Suggests) when this
method is used. Unlike the simple methods, imputed values depend on each
row's own profile, better preserving relationships between variables.
Only variables (columns) selected via ... are imputed; if none are
supplied all variables are eligible. For method = "knn", all variables
(except any supplied to ignore) are still used to compute distances,
even if they are not themselves imputed.
Imputation never changes the class of a variable:
Integer variables are imputed with a rounded integer, as the median or mean of a set of integers is frequently fractional. The rounding is reported via a message.
Factors gain the imputed value as an additional level if it is not already one.
Any other type mismatch (e.g. a character constant for a
numeric variable) is an error rather than a silent conversion of the whole
variable.
Variables which contain no non-missing values are skipped (there is no
statistic to impute from) and reported as such, except under
method = "constant" where constant does not depend on the
observed values.
Imputation is deterministic: the same input always yields the same output.
The number of values imputed per variable is reported via a message (silence
with suppressMessages). Value-level changes can be
reviewed with review_quality_ctrl.
data with missing values in the selected variables imputed.
Kowarik, A. & Templ, M. Imputation with the R Package VIM. Journal of Statistical Software 74, 1-16 (2016). \Sexpr[results=rd]{tools:::Rd_expr_doi("10.18637/jss.v074.i07")}
Gower, J. C. A General Coefficient of Similarity and Some of Its Properties. Biometrics 27, 857-871 (1971). \Sexpr[results=rd]{tools:::Rd_expr_doi("10.2307/2528823")}
Little, R. J. A. & Rubin, D. B. Statistical Analysis with Missing Data. (John Wiley & Sons, 2019).
Other high level functionality:
apply_quality_ctrl(),
assess_quality(),
review_quality_ctrl(),
semantic_enrichment()
data(example_data)
# median (numeric) / mode (categorical) imputation of all variables:
res <- impute_missing_values(example_data)
# impute only specific variables:
res <- impute_missing_values(example_data, tumoursize, diabetes,
method = "median")
# k-nearest-neighbours imputation (requires the 'VIM' package),
# excluding the identifier from the distance calculation:
if (requireNamespace("VIM", quietly = TRUE)) {
res <- impute_missing_values(example_data, method = "knn",
ignore = patient_id)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.