R/getPooledPreds.R

Defines functions getPooledPreds

Documented in getPooledPreds

# =============================================================================
# Pooled predicted values from MICE
# earlycapistran@comunidad.unam.mx - March 2021
# =============================================================================

#' Calculates pooled predicted values from muliptly imputed repeated analyses
#' from MICE.
#'
#' @param mira A 'mira' object generated by MICE
#' @return A dataframe with pooled predicted values
#' @export
#' @usage 
#' getPooledPreds(mira)
#' 
#' @importFrom broom augment
#' @importFrom mice is.mira

getPooledPreds <- function(mira) {
  if (!is.mira(mira)) 
    stop("The object must have class 'mira'")
  # Calculate predictions from m imputed models
  predList <- lapply(mira$analyses, broom::augment)
  # Make a dataframe of fitted values from each model
  predListDf <- as.data.frame(lapply(predList, `[`, ".fitted"))
  means <- apply(predListDf, 1, mean)

  # Extract a variable with predictor data
  predVar <- predList[[1]][,1]
  
  result <- data.frame(predVar, means)
  print(result)
  return(result)
}
earlycapistran/consLettersUtils documentation built on Nov. 22, 2022, 1:22 a.m.