R/createProbOfEqMatrix.R

Defines functions createProbOfEqMatrix

Documented in createProbOfEqMatrix

#' Create a matrix of P values representing the probability that two paired samples have distributions yielding the same predications.
#' 
#' This function computes the probability of hypothesis that two distribution functions desribing desnity of input samples
#' produce the same predictions given a threshold "acceptable" difference of predicted values. 
#' The data is outputed as matrix of paiwise comparisons performed upon inputed columns.
#' 
#' @param df A dataframe where columns represent vectors of some values belonging to an individual identity (e.g. sample, individual)
#' @param threshold An "acceptable difference" threshold of the predicted values. It is an open question what the good acceptable 
#' @export

createProbOfEqMatrix <- function(df,threshold){
  combinations <- combn(ncol(df),2)
  predDF <- matrix(nrow = length(density(df[,1])$y)) 
  for(i in 1:ncol(df)){
    predCol <- density(df[,i])$y 
    predDF <- cbind(predDF,predCol)
  }
  predDF <- predDF[,2:ncol(predDF)]
  predDF <- as.matrix(predDF)
  out.mx.1 <- apply(
    X=combinations,MARGIN = 2,FUN = function(x) {
      diff <- abs(predDF[,x[1]]-predDF[,x[2]])
      boolean <- diff<threshold
      acceptCount <- length(boolean[boolean==TRUE])
      probability <- acceptCount/length(diff)
    }
    )
  out.mx.2 <- matrix(nrow =(ncol(df)),ncol = (ncol(df)))
  for(i in 1:ncol(combinations)) {
    a <- combinations[1,i]
    b <- combinations[2,i]
    out.mx.2[a,b] <- out.mx.2[b,a] <- out.mx.1[i] # not sure how this bit of code works but it can be used to create pairwise matrix
    }
out.df <- as.data.frame(out.mx.2)
colnames(out.df) <- names(df)
row.names(out.df) <- names(df)
return(out.df)
}
msxakk89/dat documentation built on Aug. 3, 2020, 6:39 p.m.