forked from epstein-software/GAMuT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGAMuT-functions.R
More file actions
89 lines (73 loc) · 2.92 KB
/
GAMuT-functions.R
File metadata and controls
89 lines (73 loc) · 2.92 KB
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
## GAMuT-functions.R
##
## 2016-March-13
##
## this script contains functions used in the main program
## for GAMuT analysis
##
##----------------------------------------------------------------------
## descriptions of individual functions:
##----------------------------------------------------------------------
##
## * proj_GAMuT_pheno
## constructs projection matrix and corresponding eigenvalues for phenotypes
##
## * linear_GAMuT_pheno
## constructs linear kernel matrix and corresponding eigenvalues for phenotypes
##
## * linear_GAMuT_geno
## constructs linear kernel matrix and corresponding eigenvalues for genotypes
##
## * TestGAMuT
## constructs GAMuT statistic and returns p-value
##----------------------------------------------------------------------
## phenotypic similarity: projection matrix
##----------------------------------------------------------------------
proj_GAMuT_pheno <- function(X){
out = vector("list", 2)
names(out) = c("Kc", "ev_Kc")
out$Kc = X %*% solve(t(X) %*% X) %*% t(X) # projection matrix
out$ev_Kc = rep(1, ncol(X)) # find eigenvalues
return(out)
}
##----------------------------------------------------------------------
## phenotypic similarity: linear kernel
##----------------------------------------------------------------------
linear_GAMuT_pheno <- function(X){
out = vector("list", 2)
names(out) = c("Kc", "ev_Kc")
Kc = t(X) %*% X # transposed kernel to find eigenvalues
ev_Kc = eigen(Kc, symmetric=T, only.values=T)$values
out$Kc = X %*% t(X) # similiarity kernel for test
out$ev_Kc = ev_Kc[ev_Kc > 1e-08]
return(out)
}
##----------------------------------------------------------------------
## genotypic similarity: linear kernel
##----------------------------------------------------------------------
linear_GAMuT_geno <- function(X){
out = vector("list", 2)
names(out) = c("Lc", "ev_Lc")
Lc = t(X) %*% X # transposed kernel to find eigenvalues
ev_Lc = eigen(Lc, symmetric=T, only.values=T)$values
out$Lc = X %*% t(X) # similiarity kernel for test
out$ev_Lc = ev_Lc[ev_Lc > 1e-08]
return(out)
}
##----------------------------------------------------------------------
## constructing the GAMuT statistic and deriving p-value:
##----------------------------------------------------------------------
TestGAMuT <- function(Yc, lambda_Y, Xc, lambda_X) {
## test statistic:
m = nrow(Yc) # number of subjects in study
GAMuT = (1/m) * sum(sum(t(Yc) * Xc))
## populate vector of all pairwise combination of eigenvalues
## from the phenotype and genotype similarity matrices:
Z <- (as.matrix(lambda_Y)) %*% t(as.matrix(lambda_X))
Zsort <- sort(Z, decreasing=T)
## derive p-value of GAMuT statistic:
scoredavies = GAMuT*m^2
results_score <- davies(scoredavies, Zsort)
davies_pvalue <- (results_score$Qq)
return(davies_pvalue)
}