-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance.pcor.R
99 lines (97 loc) · 3.37 KB
/
performance.pcor.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
performance.pcor <- function(inferred.pcor, true.pcor=NULL, fdr=TRUE, cutoff.ggm=0.8,verbose=FALSE,plot.it=FALSE)
{
p <- ncol(inferred.pcor) # number of variables
if (fdr==TRUE){
test.results <-ggm.test.edges(inferred.pcor,verbose=verbose,plot=plot.it)
# extract network containing edges with prob > cutoff.ggm
prob.results<-diag(p)
for (i in 1:length(test.results$prob)){
prob.results[test.results$node1[i],test.results$node2[i]]<-test.results$prob[i]
prob.results[test.results$node2[i],test.results$node1[i]]<-test.results$prob[i]
dim(prob.results)
}
net <- extract.network(test.results, cutoff.ggm=cutoff.ggm)
adj <- diag(p)
if (nrow(net)!=0)
{
for (j in 1:nrow(net))
{
adj[net[j,2], net[j,3]] <- 1
adj[net[j,3], net[j,2]] <- 1
}
}
}
if (fdr==FALSE)
{
adj <- inferred.pcor
adj[adj!=0] <- 1
}
#
num.selected <- ( sum(abs(adj)>0)-p )/2 # number of selected edges
connectivity<-apply(adj,2,FUN=function(x) return(sum(x!=0)))-1
positive.cor<-NULL
if (num.selected>0){
if (fdr==TRUE){
positive.cor<-sum(net[,1]>0)/num.selected
}
if (fdr==FALSE){
positive.cor<-((sum(inferred.pcor>0)-ncol(inferred.pcor))/2)/num.selected
}
}
num.true<-power<-ppv<-NULL
if (is.null(true.pcor)==FALSE){
num.true <- ( sum(abs(true.pcor)>0)-p )/2 # number of true edges
num.true.positives <- ( sum(abs(true.pcor*adj)>0)-p )/2
num.false.positives<-num.selected-num.true.positives
## power
power <- -Inf
if (num.true>0) {
power <- num.true.positives/num.true
}
## true discovery rate (positive predictive value)
ppv <- -Inf
if (num.selected>0) {
ppv <- num.true.positives/num.selected
}
}
## ROC curves and AUC score
auc<-NA
tpr<-fpr<-NA
TPR<-FPR<-NA
if ((fdr==TRUE) & (is.null(true.pcor)==FALSE)){
xx<-seq(0,1,length=500) # scale of the x and y axis of the ROC curve
true.binary=(abs(true.pcor)>0) # logical matrix indicating the true partial correlations
predicted<-sym2vec(prob.results)
if (var(predicted)>0){ # there is an error message if all probabilities are zero, weird
if (plot.it==TRUE){
plot.roc="ROC"
}
if (plot.it==FALSE){
plot.roc=NA
}
myroc<-ROC(predicted,sym2vec(true.binary),plot=plot.roc)
auc<-myroc$AUC # area under the curve
TPR<-myroc$res[,1] # sensitivity =TPR
#cat(paste("length of TPR ", length(TPR),"\n"))
FPR<-1-myroc$res[,2] # 1-specifity =FPR
#cat(paste("length of TPR ", length(TPR),"\n"))
#TPR<-c(1,TPR,0)
#FPR<-c(1,FPR,0)
#yy<-approx(TPR,FPR,xout=xx,yleft=0,yright=1)
#tpr<-xx
#fpr<-yy$y
# rest later
}
}
if ((is.null(true.pcor)==FALSE)){
tpr<-power
#fpr<--Inf
if (num.true>0){
fpr<-num.false.positives/((p^2-p)/2 - num.true) # false positives/true false
}
}
# also return the number of positive and negative correlations
return(list(num.selected=num.selected, power=power,TPR=TPR,FPR=FPR, tpr=tpr,fpr=fpr,ppv=ppv,adj=adj,connectivity=connectivity,positive.cor=positive.cor,auc=auc))
}
# fpr and tpr are for the optimal parameters
# FPR and TPR are vectors