R/imputedTotals.R

#' Calculate the totals over an imputed dataset
#' @param data the orginal dataset with missing values
#' @param imputations the multiple imputations generated by e.g. \code{\link{imputeINLA}}
#' @param variable the name of the variable for which the imputations are calculated
#' @param rhs the right hand side of the formula for the aggregation
#' @return a dataset containing the totals for each imputations. The first few columns will contains the \code{rhs} variables.
#' @template deprecated
#' @export
imputedTotals <- function(data, imputations, variable, rhs){
  #nocov start
  .Deprecated(
    new = "aggregate_impute"
  )
  totals <- lapply(seq_len(ncol(imputations)), function(i){
    data[, variable][is.na(data[, variable])] <- imputations[, i]
    aggregate(
      as.formula(paste(variable, rhs, sep = "~")),
      data = data,
      FUN = sum
    )
  })
  rhs <- totals[[1]][, -ncol(totals[[1]])]
  totals <- sapply(totals, function(x){
    x[, ncol(x)]
  })
  colnames(totals) <- sprintf(
    paste(
      "Imputation%0",
      ceiling(log10(ncol(totals))),
      "i",
      sep = ""
    ),
    seq_len(ncol(totals))
  )
  cbind(
    rhs,
    totals
  )
  # nocov end
}
ThierryO/multimput documentation built on May 9, 2019, 4:42 p.m.