#' Summary Table Maker
#'
#' @param family e.g. Gaussian; argument for glm
#' @param analysis Analysis that produced data for regression analyses: "cfa", "bf", or "lca"
#' @param n.classes Number of classes if LCA
#' @param outcomeVar Outcome variable name
#' @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 data Data to analyze
#'
#' @return table matrix
#' @export
summaryTableFx <- function(family = 'gaussian', analysis = 'cfa', n.classes = NULL, preds,
outcomeVar, n.roundDigits = 2, minDecimals = 2, data) {
require(stats); require(data.table)
#outcomeVar = outcomes[outcomeVar]
## Use standardized names for data to load data
if (analysis == 'lca') {
data <- lca
preds <- paste0('Class', 1:n.classes) # predictors ('Class1' factor variable for 1-class lca, etc.)
} else {
preds <- paste0(preds, collapse = ' + ') # adding predictors (factors)
}
# if (analysis == 'cfa') {
# eval(parse(text = paste0('data <- data_cfa_', modelName))) # if cfa model names
# }
if (analysis == 'bf') {
#eval(parse(text = paste0('data <- data_bf_', modelName))) # if cfa model names
preds <- paste0('GENERAL + ', preds) # add general factor to predictor list for bifactor models
}
# if (analysis == 'bcfa') {
# eval(parse(text = paste0('data <- data_bcfa_', modelName))) # if cfa model names
# }
formula <- paste0(outcomeVar, ' ~ ', preds) # create formula
if (family == 'gaussian') {
p <- 'Pr(>|t|)' # p-value name if continuous outcome
} else {
p <- 'Pr(>|z|)' # p-value name if binary outcome
}
force(n.roundDigits); force(minDecimals)
table <- eval(parse(text = paste0('format(round(coef(summary(
glm(family = family, formula = formula, data = data)))
[, c(\"Estimate\", \"Std. Error\", \"', p, '\")], ',
n.roundDigits, '), nsmall = ', minDecimals, ')')))
#dimnames(table)[[2]] <- c("Estimate", "SE", p)
#dimnames(table)[[1]] <- gsub(dimnames(table)[[1]], pattern = '_', replacement = ' ')
# tableRows <- dim(table)[1]
# tableCols <- dim(table)[2]
# table <- matrix(as.numeric(table), nrow = tableRows, ncol = tableCols)
## Warnings
if (is.factor(data$outcomeVar) | is.character(data$outcomeVar)) {
if (family == 'gaussian') {
warning('Family argument is set to Gaussian by default.
The specified outcome variable is a factor or string. Re-check these specifications.')
}
}
print(table)
return(table)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.