-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorrelation_K8_noX.R
157 lines (114 loc) · 3.67 KB
/
correlation_K8_noX.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
##### Performance in characterizing the correlation among the traits #####
##### when annotations have no role (eight traits) #####
# Supplementary Figure S2
library(MASS)
library(pbivnorm)
library(mvtnorm)
K <- 8 # No. of traits
M <- 100000 # No. of SNPs
beta0 <- -1 # intercept of the probit model
beta0 <- rep(beta0, K)
set.seed(1)
alpha <- c(0.2, 0.35, 0.5, 0.3, 0.45, 0.55, 0.25, 0.4) # parameter in the Beta distribution
R <- matrix(0, K, K) # Correlation matrix for the traits
R[1, 2] <- 0.7
R[1, 3] <- 0.4
R[2, 3] <- 0.2
R[4, 5] <- 0.6
R[4, 6] <- 0.3
R[5, 6] <- 0.1
R[7, 8] <- 0.5
R <- R + t(R)
diag(R) <- 1
rep <- 50 # repeat times
##### LPM #####
library(LPM)
# function to generate data
generate_data <- function(M, K, beta0, alpha, R){
Z <- matrix(rep(beta0, each = M), M, K) + mvrnorm(M, rep(0, K), R)
indexeta <- (Z > 0)
eta <- matrix(as.numeric(indexeta), M, K)
Pvalue <- NULL
for (k in 1:K){
Pvalue_tmp <- runif(M)
Pvalue_tmp[indexeta[, k]] <- rbeta(sum(indexeta[, k]), alpha[k], 1)
Pvalue <- c(Pvalue, list(data.frame(SNP = seq(1, M), p = Pvalue_tmp)))
}
names(Pvalue) <- paste("P", seq(1, K), sep = "")
return( list(Pvalue = Pvalue, eta = eta))
}
est_bLPM <- NULL
est_LPM <- NULL
for (i in 1:rep){
data <- generate_data(M, K, beta0, alpha, R)
Pvalue <- data$Pvalue
fit <- bLPM(Pvalue, coreNum = 10)
est <- c(est, list(fit))
fitLPM <- LPM(fit)
est_LPM <- c(est_LPM, list(fitLPM))
}
est_rho_LPM <- array(0, c(K, K, rep))
test_rho_LPM <- array(0, c(K, K, rep))
for(i in 1:rep){
est_rho_LPM[, , i] <- est_LPM[[i]]$R
rho_pvalue <- test_rho(est_blPM[[i]])
test_rho_LPM[, , i] <- (rho_pvalue < 0.05/((K-1)*K/2))
}
# results to get Supplementary Figure S2a
test_rho_LPM <- apply(test_rho_LPM, c(1, 2), mean)
colnames(test_rho_LPM) <- paste("P", 1:8, sep = "")
rownames(test_rho_LPM) <- colnames(test_rho_LPM)
##### GPA #####
library(GPA)
# function to generate data
generate_data_GPA <- function(M, K, beta0, alpha, R){
Z <- matrix(rep(beta0, each = M), M, K) + mvrnorm(M, rep(0, K), R)
indexeta <- (Z > 0)
eta <- matrix(as.numeric(indexeta), M, K)
Pvalue <- matrix(runif(M*K), M, K)
for (k in 1:K){
Pvalue[indexeta[, k], k] <- rbeta(sum(indexeta[, k]), alpha[k], 1)
}
return( list(Pvalue = Pvalue, eta = eta))
}
test_rho_GPA <- array(0, c(K, K, rep))
for (k in 1:rep){
data <- generate_data_GPA(M, K, beta0, alpha, R)
Pvalue <- data$Pvalue
for (i in 1:(K-1)){
for (j in (i+1):K){
fit <- GPA(Pvalue[, c(i, j)])
fit.H0 <- GPA(Pvalue[, c(i, j)], pleiotropyH0 = TRUE)
test <- pTest(fit, fit.H0)
test_rho_GPA[i, j, k] <- (test$pvalue < 0.05/28)
}
}
}
# results to get Supplementary Figure S2b
test_rho_GPA <- apply(test_rho_GPA, c(1, 2), mean)
test_rho_GPA <- test_rho_GPA + t(test_rho_GPA)
diag(test_rho_GPA) <- 1
colnames(test_rho_GPA) <- paste("P", 1:8, sep = "")
rownames(test_rho_GPA) <- colnames(test_rho_GPA)
##### GGPA #####
library(GGPA)
# function to generate data
generate_data_GGPA <- function(M, K, beta0, alpha, R){
Z <- matrix(rep(beta0, each = M), M, K) + mvrnorm(M, rep(0, K), R)
indexeta <- (Z > 0)
eta <- matrix(as.numeric(indexeta), M, K)
Pvalue <- matrix(runif(M*K), M, K)
for (k in 1:K){
Pvalue[indexeta[, k], k] <- rbeta(sum(indexeta[, k]), alpha[k], 1)
}
return( list(Pvalue = Pvalue, eta = eta))
}
est_GGPA <- NULL
for (i in 1:rep){
data <- generate_data_GGPA(M, K, beta0, alpha, R)
fit_GGPA <- GGPA(data$Pvalue)
est_GGPA <- c(est_GGPA, list(fit_GGPA))
}
# result to get Supplementary Figure S2c
plot(est_GGPA[[1]])
plot(est_GGPA[[2]])