R/rel_table.r

Defines functions rel_table

Documented in rel_table

#' Creates a table with the reliability estimates of a SEM or CFA
#' 
#' Creates a table that includes reliability parameters (Cronbach's alpha, omegas, and average variance extracted).
#' 
#' @param object An object of class \code{lavaan} created by using \code{cfa()} or \code{sem()} from the package 'lavaan'.
#' @param var Character value that indicates which variables should be printed. If not specified, all variables will be returned.
#' @param total Logical value indicating if total estimates should be printed. 
#' @param print Logical value indicating whether the resulting table should be formatted acccording to APA guidelines. 
#' @examples 
#' library(lavaan)
#' model <- '
#'   # latent variables
#'   ind60 =~ x1 + x2 + x3
#'   dem60 =~ y1 + y2 + y3 + y4
#'   dem65 =~ y5 + y6 + y7 + y8
#'   
#'   # regressions
#'   dem60 ~ ind60
#'   dem65 ~ ind60 + dem60
#'   
#'   # residual covariances
#'   y1 ~~ y5
#'   y2 ~~ y4 + y6
#'   y3 ~~ y7
#'   y4 ~~ y8
#'   y6 ~~ y8
#' '
#' fit <- sem(model,
#'            data = PoliticalDemocracy)
#' 
#' rel_table(fit)
#' @export
rel_table <- function(object,
                      var = NULL,
                      total = TRUE,
                      print = FALSE){
  
  # dependencies
  library(semTools)
  library(tidyverse)
  library(papaja)
  
  # function
  temp <- object %>%
    reliability %>% t %>%
    as.data.frame %>%
    rownames_to_column("variable") 
  
  if (isTRUE(print)) {
    temp <- temp %>%
      mutate_at(vars(alpha, omega, omega2, omega3, avevar),
                funs(printnum(., gt1 = F)))
  }
  
  if (!isTRUE(total)) {
    temp <- temp %>%
      subset(., variable != "total")
  }
  
  if (!is.null(var)) {
    temp <- temp %>%
      subset(., variable == var)
  }
  return(temp %>% as.tibble)
}
masurp/pmstats documentation built on Oct. 6, 2020, 9:24 p.m.