R/mic.R

Defines functions MIC

Documented in MIC

#' Maximal information coefficient (MIC)
#'
#' This function calculates the Maximal information coefficient (MIC).
#' Calculate the dependence of pairs of variables. The MIC belongs to a larger
#' classification of nonparametric exploration statistics based on maximum
#' information (MINE).
#'
#' It also provides the p-value for the independence hypothesis, that are
#' generated by permuting the data and seeing how likely it is that the observed
#' MIC value arises from the perturbed data.
#'
#' @param x A vector, matrix or numeric data frame
#' @param y A vector, matrix or numeric data frame
#' @param R Number of replications that will be used to calculate the p-value
#' @param ... Additional parameters defined in the code{mine} function of the
#' code{\link{minerva}} package.
#'
#' @importFrom minerva mine
#' @export
#' @examples
#' #Lineal dependence
#' x<-rnorm(200, sd=2.5)
#' y<-2*x+rnorm(200, sd=2.5)
#' MIC(x, y, R=100)
#'
#' #No dependency
#' x<-rnorm(200, mean=2, sd =1.8)
#' y<-rnorm(200, mean=0, sd =2.5)
#' MIC(x, y, R=100)

MIC <- function(x,y,R=100,...) {
  #MIC
  mic<-mine(x=x,y=y,...)

  #For calculate p-value de MIC
  if (! is.null(R)) {
    R <- floor(R)
    if (R < 1) R <- 100
  } else {
    R <- 100
  }

  Rep<-as.data.frame(rep(as.data.frame(y),R))
  Rep2<-as.data.frame(apply(Rep,2,sample))
  permic<-matrix(NA,nrow=R,ncol=7)
  colnames(permic)<-c("MIC","MAS","MEV","MCN","MIC-R2", "GMIC","TIC")
  for (i in 1:R){
    p<-mine(x=x,y=Rep2[,i],...)
    permic[i,1:7]<-c(p$MIC,p$MAS,p$MEV,p$MCN,p$`MIC-R2`,p$GMIC,p$TIC)
  }

  permic<-as.data.frame(permic)
  pvalor<-nrow(permic[which(permic$MIC>=mic$MIC),])/nrow(permic)

  mic2<-as.data.frame(cbind(mic$MIC,pvalor))
  colnames(mic2)<-c("MIC","p-value")
  return(mic2)
}
AnaBPazos/AlterCorr documentation built on May 20, 2019, 4:24 p.m.