R/datexp_assocat.R

Defines functions datexp_assocat

Documented in datexp_assocat

#' Evaluate the associations (1 - chi-square or conditional proability) between several pairs of categorical variables.
#' @param x    Dataframe or Tibble. Table containing the categorical variables for which the association should be computed.
#' @return A tibble indicating the proportion of missing values per variable.
#' @importFrom stats chisq.test
#' @importFrom stats ftable
#' @importFrom purrr map_lgl
#' @export


datexp_assocat <- function(x){
  
  # Select categorical variables
  x <- x[, map_lgl(x, is.numeric)==FALSE]
  
  # Prepare the association matrix
  matrix <- as.data.frame(matrix(nrow = length(x), ncol = length(x)))
  names(matrix) <- names(x)
  row.names(matrix) <- names(x)
  
  # Fill in the matrix with 1 - the result of the chi-square test
  for (i in 1:length(x)){
    for (j in i:length(x)){
      test <- round(1-chisq.test(ftable(x[,c(i,j)]),
                                 simulate.p.value = TRUE,
                                 B = 100)$p.value,3)
      matrix[j,i] <- test
      matrix[i,j] <- test
    }
  }
  
  matrix
  
}
NicolasJBM/datexp documentation built on May 14, 2019, 10:36 a.m.