R/corrTableFx.R

Defines functions corrTableFx

Documented in corrTableFx

#' Correlation Table Maker
#'
#' @param analysis Analysis that produced data for regression analyses: "cfa", "bf", or "lca"
#' @param outcomes Which outcomes to include
#' @param data Data for analyses
#' @param n.roundDigits Number of digits to round to
#' @param minDecimals Minimum number of decimal places to retain (to line up, in case trailing .00)
#'
#' @return
#' @export
corrTableFx <- function(analysis = 'cfa', preds, outcomes, data,
                        n.roundDigits = NULL, minDecimals = 2) {

  require(boot); require(stats)

  if (is.null(n.roundDigits)) {
    n.roundDigits <- minDecimals # if not otherwise specified, make rounding correspond to specified minimum decimal places
  }

  #force(preds)
  if (analysis == 'bf') preds <- c('GENERAL', preds)

  table <- matrix(data = NA, nrow = length(preds), ncol = length(outcomes))

  for (y in 1:length(outcomes)) {
    for (p in 1:length(preds)) {
      table[p, y] <- stats::cor(data[, outcomes[y]], data[, preds[p]], use = 'na.or.complete')
    }
  }

  colnames(table) <- outcomes

  for (y in 1:length(outcomes)) {
    #table[, y] <- as.numeric(as.character(table[, y]))
    #eval(parse(text = paste0('table[, ', outcomes[y], '] <- format(round(table[, ', outcomes[y], '], digits = 3), nsmall = 3)')))
    table[, y] <- format(round(as.numeric(as.character(table[, y])), digits = n.roundDigits), nsmall = minDecimals)
  }

  rownames(table) <- preds

# ------------------------------------------------- #
# ------------------------------------------------- #

##### Place to put rules to modify column names ####

  dimnames(table)[[2]] <- trimws(gsub(dimnames(table)[[2]], pattern = 'SF-12', replacement = ''))
  dimnames(table)[[2]] <- trimws(gsub(dimnames(table)[[2]], pattern = ' or ', replacement = '/'))
  dimnames(table)[[2]] <- trimws(gsub(dimnames(table)[[2]], pattern = 'Down/', replacement = ''))
  dimnames(table)[[2]] <- trimws(gsub(dimnames(table)[[2]], pattern = '/Peaceful', replacement = ''))

# ------------------------------------------------- #
# ------------------------------------------------- #

  print(table)
  return(table)

}
enaY15/TabulationAutomation documentation built on March 18, 2020, 8:35 p.m.