Diff of /PoissonDistance.R [000000] .. [81de4e]

Switch to unified view

a b/PoissonDistance.R
1
PoissonDistance <-
2
function(x,beta=1,type=c("mle","deseq","quantile"),transform=TRUE, alpha=NULL,perfeature=FALSE){
3
  type <- match.arg(type)
4
  if(!transform && !is.null(alpha)) stop("You have asked for NO transformation but have entered alpha.")
5
  if(transform && !is.null(alpha)){
6
    if(alpha>0 && alpha <= 1) x <- x^alpha
7
    if(alpha<=0 || alpha>1) stop("alpha must be between 0 and 1")
8
  }  
9
  if(transform && is.null(alpha)){
10
    alpha <- FindBestTransform(x)
11
    x <- x^alpha
12
  }
13
  dd <- matrix(0, nrow=nrow(x), ncol=nrow(x))
14
  ddd <- NULL
15
  if(perfeature) ddd <- array(0, dim=c(nrow(x), nrow(x), ncol(x)))
16
  for(i in 2:nrow(dd)){
17
    xi <- x[i,]
18
    for(j in 1:(i-1)){
19
      xj <- x[j,]
20
      n <- NullModel(x[c(i,j),],type=type)$n
21
      ni <- n[1,]
22
      nj <- n[2,]
23
      di <- (xi+beta)/(ni+beta)
24
      dj <- (xj+beta)/(nj+beta)
25
      dd[i,j] <- sum(ni+nj-ni*di-nj*dj+xi*log(di)+xj*log(dj))
26
      if(perfeature) ddd[j,i,] <- ddd[i,j,] <- ni+nj-ni*di-nj*dj+xi*log(di)+xj*log(dj)
27
    }
28
  }
29
  save <- list(dd=as.dist(dd+t(dd)), alpha=alpha, x=x, ddd=ddd, alpha=alpha, type=type)
30
  class(save) <- "poidist"
31
  return(save)
32
}
33
34
print.poidist <- function(x,...){
35
  if(!is.null(x$alpha)) cat("Value of alpha used to transform data: ", x$alpha, fill=TRUE)
36
  if(is.null(x$alpha)) cat("No transformation performed.",fill=TRUE)
37
  cat("This type of normalization was performed:", x$type, fill=TRUE)
38
  cat("Dissimilarity computed for ", nrow(x$x), " observations.", fill=TRUE)
39
}
40