R/cor2.prob.R

Defines functions cor2.prob

Documented in cor2.prob

#' Extensió de la funció cor {stats}
#' La justificació d'haver fet aquesta funció:
#' 1) la funció cor {stats} no retorna els p-values
#' 2) la funció cor.test {stats} no permet passar un conjunt de dades
#' Aquesta extensió permet obtenir el p-value de les correlacions calculades a partir d'un conjunt de dades
#' 
#' @param X a numeric vector, matrix or data frame
#' @param use an optional character string giving a method for computing covariances in the presence of missing values. This must be (an abbreviation of) one of the strings "everything", "all.obs", "complete.obs", "na.or.complete", or "pairwise.complete.obs".
#' @param method a character string indicating which correlation coefficient (or covariance) is to be computed. One of "pearson" (default), "kendall", or "spearman".
#' @param alternative a character string specifying the alternative hypothesis, must be one of "two.sided" (default), "greater" or "less".
#' @param conf.int a logical indicating whether a confidence interval should be computed.
#' @param conf.level confidence level of the interval.
#' @param continuity logical: if TRUE, a continuity correction is used for Kendall's tau and Spearman's rho when not computed exactly. Default is FALSE
#' @param ... further arguments to be passed to or from methods.
#' @return A list with two matrices: one with correlation values and other one with p-values.
#' @export
cor2.prob <- function(X, use = "everything", method = c("pearson", "kendall", "spearman"),
                      alternative = c("two.sided", "less", "greater"), exact = NULL,
                      conf.int = FALSE,
                      conf.level = 0.95, continuity = FALSE, ...){
  # la correlació es calcula amb cor {stats} sense cap problema
  R <- cor(x = X, y = NULL, use = use, method = method)

  # els p-value es calculen amb cor.test {stats}. Cal passar-li dos a dos. Es calcula una vegada per cada parell i s'omple la matriu amb els p-value.
  Rp <- R

  var.names <- rownames(R)

  if(conf.int == TRUE){
    Rlb <- Rub <- R
    for(i in var.names){
      var.names <- var.names[-which(var.names == i)]
      for(j in var.names){
        Rp[i, j] <- Rp[j, i] <- cor.test(x = X[, i], y = X[, j], alternative = alternative, method = method, exact = exact, conf.level = conf.level, continuity = continuity)$p.value
        ci <- cor.test2(x = X[, i], y = X[, j], alternative = alternative, method = method, exact = exact, conf.level = conf.level, continuity = continuity)
        Rlb[i, j] <- Rlb[j, i] <- ci[[2]]
        Rub[i, j] <- Rub[j, i] <- ci[[3]]
      }
    }
    for(i in 1:nrow(Rp)) Rp[i,i] <- NA   #poso NAs a la diagonal, doncs no procedeix calcular els p-value de la correlació d'una variable amb sí mateixa
    for(i in 1:nrow(Rlb)) Rlb[i,i] <- NA
    for(i in 1:nrow(Rub)) Rub[i,i] <- NA

    # La funció retorna quatre matrius (correlacions, p-value, límits inferior/superior del IC)
    return(list(cor.matrix = R, pval.matrix = Rp, LB = Rlb, UB = Rub))
  }

  if(conf.int == FALSE){
    for(i in var.names){
      var.names <- var.names[-which(var.names == i)]
      for(j in var.names){
        Rp[i, j] <- Rp[j, i] <- cor.test(x = X[, i], y = X[, j], alternative = alternative, method = method, exact = exact, conf.level = conf.level, continuity = continuity)$p.value
      }
    }
    for(i in 1:nrow(Rp)) Rp[i, i] <- NA   #poso NAs a la diagonal, doncs no procedeix calcular els p-value de la correlació d'una variable amb ella mateixa

    # La funcio retorna les dues matrius (correlacions, p-value)
    return(list(cor.matrix = R, pval.matrix = Rp))
  }
}
IRBLleida/UdBRpackage documentation built on Dec. 24, 2019, 9:10 p.m.