-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBayesianCorrelation.R
76 lines (67 loc) · 2.59 KB
/
BayesianCorrelation.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# This code implements R code for Bayesian correlation computations for sequence count
# data, as described in the paper "Bayesian Correlation Analysis for Sequence Count Data"
# To use, simply 'source' the file, and then run whichever functions you like on the data.
# The first function allows you to specify your own condition-specific priors, while the
# subsequent functions implement the three priors described in the paper.
#
#
# The following is an R function to compute the Bayesian correlation coefficients
# for a given data matrix of counts, X.
# The rows represent entities and the columns represent conditions
# INPUTs: alpha0 and beta0 are row vectors giving condition-specific priors
# OUTPUTs: Bayesian correlation square matrix
#######################
# Defining FUNCTION to compute the Bayesian correlation coefficients
Bayes_Corr <- function(alpha0, beta0, X){
nrowsX <- nrow(X)
k <- ncol(X)
cs <- colSums(X)
alphas <- matrix(rep(alpha0,nrowsX), nrow=nrowsX, byrow=TRUE) + X
betas <- matrix(rep(beta0,nrowsX), nrow=nrowsX, byrow=TRUE) + matrix(rep(cs,nrowsX), nrow=nrowsX, byrow=TRUE) - X
alphasPLUSbetas <- alphas + betas
# First BIG product term for covariance formula
Psi <- alphas/alphasPLUSbetas - matrix(rep(rowSums(alphas/alphasPLUSbetas)/k, k), ncol=k, byrow=FALSE)
# Covariance matrix
cov_mtrx <- Psi %*% t(Psi) / k
# Variances (this is a column vector of length = nrowsX)
var_vec <- as.matrix( ( rowSums( (alphas*betas)/( (alphasPLUSbetas^2)*(alphasPLUSbetas+1) ) ) + rowSums(Psi^2) )/k )
Bcorrvals <- cov_mtrx / sqrt( var_vec %*% t(var_vec) )
diag(Bcorrvals) <- 1
return(Bcorrvals)
}
# End of FUNCTION
#######################
#######################
# Computing the Bayesian correlations assuming first (uniform) prior
Bayes_Corr_Prior1 <- function(X){
d <- dim(X)
alpha0 <- rep(1,d[2])
beta0 <- rep(1,d[2])
Bcorrvals <- Bayes_Corr(alpha0,beta0,X)
return(Bcorrvals)
}
# End of FUNCTION
#######################
#######################
# Computing the Bayesian correlations assuming second (Dirichlet-marginalized) prior
Bayes_Corr_Prior2 <- function(X){
d <- dim(X)
alpha0 <- rep(1/d[1],d[2])
beta0 <- rep(1-1/d[1],d[2])
Bcorrvals <- Bayes_Corr(alpha0,beta0,X)
return(Bcorrvals)
}
# End of FUNCTION
#######################
#######################
# Computing the Bayesian correlations assuming third (zero count-motivated) prior
Bayes_Corr_Prior3 <- function(X){
d <- dim(X)
cs <- colSums(X)
alpha0 <- (cs+1)/(max(cs)+1)
beta0 <- rep(1,d[2])
Bcorrvals <- Bayes_Corr(alpha0,beta0,X)
return(Bcorrvals)
}
# End of FUNCTION
#######################