R/Cor_test.R

Defines functions Cor_test

Documented in Cor_test

#' Computed the RV coefficient between the metrics.
#'
#' `Cor_test()` Return matrix of correlation between all metrics.
#'
#' This function is meant to be used with the tab generated by formultivariate()
#' The RV coefficient is meant to compute the correlation between two tabs.
#' In this case metric's values at each scale are considered as tab. So a RV coefficient is computed
#' for each pairwise metrics with an associeted p-value.
#' @param Varia_paysage_multi Tab generated by formodel()
#'
#' @param dist Vector of scales you choosed during you analusis in Chloe
#' @param method_cor correlation method, spearman or pearson
#' @return Return matrix of correlation between all metrics.
#' @export
Cor_test=function(Varia_paysage_multi,method_cor){
  coefRV = matrix(ncol = length(Varia_paysage_multi), nrow = length(Varia_paysage_multi))
  coefRV[lower.tri(coefRV)] = 1
  diag(coefRV) = 1
  pvalue = matrix(ncol = length(Varia_paysage_multi), nrow = length(Varia_paysage_multi))
  colnames(coefRV) = row.names(coefRV) = colnames(Varia_paysage_multi)
  colnames(pvalue) = row.names(pvalue) = colnames(Varia_paysage_multi)
  pb <- txtProgressBar(min = 0, max = length(Varia_paysage_multi),
                       style = 3)
  rep = 0
  for (i in colnames(Varia_paysage_multi)) {
    Sys.sleep(0.1)
    rep = rep + 1
    setTxtProgressBar(pb, rep)
    for (j in colnames(Varia_paysage_multi)) {
      if (!is.na(coefRV[i, j])) {
        next
      }
      temp = cor.test(Varia_paysage_multi[, i], Varia_paysage_multi[,
                                                                    j], method = method_cor)
      coefRV[i, j] = temp$estimate
      pvalue[i, j] = temp$p.value
    }
  }

  coefRV[lower.tri(coefRV)] = t(coefRV)[lower.tri(coefRV)]
  # t(coefRV)[upper.tri(coefRV)] = coefRV[upper.tri(coefRV)]
  diag(coefRV) = 1
  pvalue[lower.tri(pvalue)] = t(pvalue)[lower.tri(pvalue)]
  diag(pvalue) = 0.04
  # t(pvalue)[upper.tri(pvalue)]=pvalue[upper.tri(pvalue)]
  return(list(coefRV,pvalue))
}
Pintademijote/multipack documentation built on Sept. 24, 2019, 7:54 a.m.