-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.Rhistory
146 lines (146 loc) · 4.14 KB
/
.Rhistory
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
Sys.getenv()
Sys.getenv('PATH')
library(GPN)
library(GPN)
#'
#' @examples
#' N <- 10000
#' y1 <- replicate(2, sample(c(0,1), N, replace = TRUE, prob = c(0.999, 0.001)))
#' y2 <- replicate(2, sample(c(0,1), N, replace = TRUE, prob = c(0.5, 0.5)))
#' y3 <- replicate(2, rnorm(N))
#' y <- cbind(y1, y2, y3)
#' x <- rbinom(N,2,0.3)
#' CLC_ave(x, y, cov = NULL)
#'
CLC_ave <- function(x, y, cov = NULL){
x <- as.matrix(x)
y <- as.matrix(y)
# Check dimension of the individual-level data: x and y
if (nrow(x) != nrow(y)){
stop("Error: Please check the sample size of x and y. They should be the same!")
}
# Check the existing of the covariance matrix
if (!is.null(cov)){
cov <- as.matrix(cov)
if (nrow(cov) != nrow(x)){
stop("Error: Please check the sample size of covariance matrix, which should be the same as the sample size of individual-level genotyps and phenotypes!")
}
}
# score test statistic
Tstat <- apply(y, 2, function(ytemp){
if (!all(ytemp %in% c(0,1))){
stat <- SCORE(x, ytemp, cov)$Tstat
} else {
ccratio <- sum(ytemp)/length(ytemp)
stat <- ifelse((ccratio >= 0.4 & ccratio <= 0.6),
SCORE(x, ytemp, cov)$Tstat,
Adjusted_Tstat(x,ytemp,cov = NULL))
}
return(stat)
})
Tstat <- as.matrix(Tstat)
# Check if adjust for the covariates
if (!is.null(cov)){
x1 <- apply(x, 2, function(t) return(residuals(lm(t ~ 1+cov))))
y1 <- apply(y, 2, function(t){
if (all(t %in% c(0,1))){
return(residuals(glm(t ~ 1+cov, family = binomial(link = "logit"))))
} else {
return(residuals(lm(t ~ 1+cov)))
}
})
x <- as.matrix(x1)
y <- as.matrix(y1)
}
# Test
K <- ncol(y)
if (K == 1){
pv <- pchisq(Tstat^2, 1, lower.tail = FALSE)
} else if ( K == 2){
Sigma <- cor(y)
W <- ginv_ratio(Sigma)
U <- t(W)%*%ginv_ratio(W%*%Sigma%*%t(W))%*%W
CLC <- t(Tstat)%*%U%*%Tstat
pv <- pchisq(CLC,2,lower.tail = FALSE)
} else {
# Hierarchical clustering based on the number of clusters with minimum height
Sigma <- as.matrix(cor(y))
dist <- 1 - Sigma
hc <- hclust(as.dist(dist),"average")
H <- which.min(diff(hc$height))
index <- cutree(hc,H)
L <- max(index)
B <- sapply(1:L, function(t) as.numeric(index==t))
# CLC test
W <- ginv_ratio( t(B)%*%ginv_ratio(Sigma)%*%B ) %*% t(B) %*% ginv_ratio(Sigma)
CLC <- t( W%*%Tstat ) %*% ginv_ratio( W%*%Sigma%*%t(W) ) %*% ( W%*%Tstat )
pv <- pchisq(CLC,L,lower.tail = FALSE)
}
return(pv)
}
N <- 10000
y1 <- replicate(1, sample(c(0,1), N, replace = TRUE, prob = c(0.999, 0.001)))
x <- rbinom(N,2,0.3)
CLC_ave(x, y1, cov = NULL)
library(GPN)
N <- 10000
x <- rbinom(N,2,0.3)
y1 <- replicate(1, sample(c(0,1), N, replace = TRUE, prob = c(0.999, 0.001)))
CLC_ave(x, y1, cov = NULL)
y1 <- replicate(2, sample(c(0,1), N, replace = TRUE, prob = c(0.999, 0.001)))
CLC_ave(x, y1, cov = NULL)
y1 <- replicate(2, sample(c(0,1), N, replace = TRUE, prob = c(0.999, 0.001)))
y2 <- replicate(2, sample(c(0,1), N, replace = TRUE, prob = c(0.5, 0.5)))
y3 <- replicate(2, rnorm(N))
y <- cbind(y1, y2, y3)
CLC_ave(x, y1, cov = NULL)
devtools::document()
devtools::document()
devtools::document()
devtools::document()
devtools::document()
devtools::document()
devtools::document()
x <- rbinom(N,2,0.3)
x <- rbinom(10,2,0.3)
x <- as.matrix(x)
factor(x)
devtools::document()
devtools::document()
devtools::document()
devtools::document()
devtools::document()
library(GPN)
N <- 10000
y1 <- replicate(2, sample(c(0,1), N, replace = TRUE, prob = c(0.999, 0.001)))
y2 <- replicate(2, sample(c(0,1), N, replace = TRUE, prob = c(0.5, 0.5)))
y3 <- replicate(2, rnorm(N))
y <- cbind(y1, y2, y3)
x <- rbinom(N,2,0.3)
B <- matrix(c(1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1), nrow = 6)
Network_Test(x, y, B, test.method = "ceCLC")
Network_Test(x, y, B, test.method = "CLC")
Network_Test(x, y, B, test.method = "HCLC")
Network_Test(x, y, B, test.method = "OBrien")
Network_Test(x, y, B, test.method = "omnibus")
Network_Test(x, y, B, test.method = "TATES")
Network_Test(x, y, B, test.method = "MultiPhen")
devtools::document()
devtools::load_all()
set.seed(123)
Net <- matrix(rnorm(2000)^2, nrow = 500, ncol = 4)
p <- get_pvalue(Net)$p
out <- adjust_p(p, method = "qvalue")
library(GPN)
library(GPN)
library(GPN)
library(GPN)
rm(list = ls())
save.image()
rm(list = ls())
save.image()
devtools::document()
devtools::build()
library(GPN)
devtools::load_all(".")
library(GPN)