R/random_imputation.R

#' Random Imputation
#'
#' Perfom random imputation of factor and numeric variables using marginal
#' distribution information.
#'
#' @export
#'
#' @author David Navega
#'
#' @param x a data.frame with missing values
#' @param seed value for the random number generator.
#'
random_imputation <- function(x, seed = 1984) {

  # RNG
  set.seed(seed)

  # Parameters
  n <- nrow(x)
  m <- ncol(x)
  is_missing <- is.na(x)

  # Create a data distribution envelope & sample from it
  distribution_envelope <- create_envelope(x, type = "data")
  sampled_data <- sample_envelope(distribution_envelope, n = n)

  # Replace missing values
  x[is_missing] <- sampled_data[is_missing]

  # return ----
  rout <- x
  return(rout)

}
dsnavega/imputeForest documentation built on May 8, 2019, 2:43 p.m.