R/datexp_assonum.R

Defines functions datexp_assonum

Documented in datexp_assonum

#' Evaluate the associations (pearson, kendall, spearman or conditional proability) between several pairs of numeric variables.
#' @param x      Dataframe or Tibble. Table containing the numeric variables for which the association should be computed.
#' @param method Character string. "pearson", "kendall", "spearman", or "conditional".
#' @return A tibble indicating the proportion of missing values per variable.
#' @importFrom psych corr.test
#' @importFrom purrr map_lgl
#' @export


datexp_assonum <- function(x, method = "pearson"){
  
  # Select categorical variables
  x <- x[, map_lgl(x, is.numeric)==TRUE]
  
  # Prepare the association matrix
  if (method == "conditional") {
    matrix <- as.data.frame(matrix(nrow = length(x), ncol = length(x)))
    names(matrix) <- names(x)
    row.names(matrix) <- names(x)
    for (i in 1:length(x)){
      for (j in i:length(x)){
        test <- mean(x[,i] >= x[,j])
        matrix[j,i] <- test
        matrix[i,j] <- 1-test
      }
    }
  } else {
    matrix <- corr.test(x, method = method)$r
  }
  
  matrix
  
}
NicolasJBM/datexp documentation built on May 14, 2019, 10:36 a.m.