R/autoAnalyzeR.R

Defines functions autoAnalyzeR

Documented in autoAnalyzeR

#' autoAnalyzeR
#'
#' @param outcomeVector Vector of names of variables used as outcomes in analyses
#' @param outcomesSetName Optional string specifying the start of the label for the set of variables in correlation analyses
#' @param family e.g. Gaussian...glm argument
#' @param data Dataset for analyses
#' @param modelPredictorNamesList List of sets of predictors belonging to each model; names attribute should be the model names
#' @param analysis Type of analysis that produced the data to be analyzed (e.g. "cfa", "lca", "bf")
#' @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)
#' @param corrReturn Run correlations as well as regressions? TRUE/FALSE
#'
#' @return Print and return tables with regression results for different predictor models and outcomes
#' @export
#'
#' @examples predictorModelNameVector <- c('predictorSet1', 'predictorSet2')
#' outcomeVector <- c('y1', 'y2', 'y3')
#' analysis = 'cfa'
autoAnalyzeR <- function(outcomeVector, outcomesSetName = 'sf12_outcomes', family = 'gaussian', modelPredictorNamesList,
                         analysis = 'cfa', n.roundDigits = NULL, minDecimals = 2, corrReturn = TRUE) {

  library("rlist")

  fscore_corr <- list()
  corr <- list()
  reg <- list()
  names4reg <- c()

  for (m in 1:length(modelPredictorNamesList)) {

    predModelName <- names(modelPredictorNamesList)[m]
    modelPreds <- modelPredictorNamesList[[m]]
    force(outcomeVector)

    cat('

    ==========================

    ')

    cat(predModelName, '

    ==========================

    ')

    cat('


Correlation Analyses


    ')

    if (corrReturn == TRUE) {

      fscore_corr <- rlist::list.append(fscore_corr, TabulationAutomation::corrTableFx(analysis = analysis, preds = modelPreds,
                                              outcomes = modelPreds,
                                              data = eval(parse(text = paste0('data_', analysis, '_', predModelName))),
                                              n.roundDigits = n.roundDigits, minDecimals = minDecimals))
      cat('

    ')
      corrIter <- TabulationAutomation::corrTableFx(analysis = analysis, preds = modelPreds,
                                        outcomes = outcomeVector,
                                        data = eval(parse(text = paste0('data_', analysis, '_', predModelName))),
                                        n.roundDigits = n.roundDigits, minDecimals = minDecimals)
      corr <- rlist::list.append(corr, corrIter)
      names(corr)[m] <- c(names(corr), paste0('corr_', analysis, '_', names(modelPredictorNamesList)[m]))

    }

    cat('


Regression Analyses


    ')

    for (y in 1:length(outcomeVector)) {

cat(predModelName, ' ... ', outcomeVector[y], '


    ')
      regIter <- TabulationAutomation::summaryTableFx(analysis = analysis, preds = modelPreds,
                                                      outcomeVar = eval(parse(text = paste0('\'', outcomeVector[y], '\''))),
                                                      data = eval(parse(text = paste0('data_', analysis, '_', predModelName))),
                                                      n.roundDigits = n.roundDigits, minDecimals = minDecimals)
      reg <- rlist::list.append(reg, regIter)
      names(reg)[y] <- outcomeVector[y]
      regIter <<- regIter
      eval(envir = globalenv(), parse(text = paste0(paste0(outcomeVector[y], '_', analysis, '_', predModelName), ' <- regIter'))) # vector with names -> global environment
      cat('


    ')
      try(outcomeLabel <- outcomeVector[y])
      try(outcomeLabel <- outcomeVector[[y]])
      names4reg <- c(names4reg, paste0(outcomeLabel, '_', analysis, '_', names(modelPredictorNamesList)[m]))

    }
  }

  names(reg) <- names4reg
  if (corrReturn == TRUE) names(fscore_corr) <- names(modelPredictorNamesList)[m]

  if (corrReturn == TRUE) {
    tableList <- list(fscore_corr, corr, reg)
    names(tableList) <- c('FactorCorrelations', 'Correlations', 'RegressionResults')
  } else {
    tableList <- list(reg)
    names(tableList) <- c('RegressionResults')
  }

  #try(do.call(writeClipboard, tableList)) # if in Windows, copy directly to clipboard

  return(tableList)

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