-
Notifications
You must be signed in to change notification settings - Fork 1
/
RRmix_Simulation_Final.R
executable file
·18189 lines (10351 loc) · 437 KB
/
RRmix_Simulation_Final.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#----------------------------------#
# Simulation Study: RRmix Model #
# Stephen Salerno Jr. (ss2658) #
# Last Update: September 23, 2016 #
#----------------------------------#
#----------------------------#
# Inverse-Gamma Optimization #
#----------------------------#
load("AX-RRmix.RData") # Change Path Based on Directory
sample_sig2g <- result.AX.RRmix$sig2_g
hist(sqrt(sample_sig2g))
IGFunction <- function(x,A,B){(B^A)/gamma(A) * x^(-A-1) * exp(-B/x)}
library(MASS)
IGParams <- fitdistr(sample_sig2g, IGFunction, list(A=1, B=1))$estimate
#------------------#
# is.wholenumber() #
#------------------#
is.wholenumber <- function(x, tol = .Machine$double.eps^0.5){ # From integer{base} help file
abs(x - round(x)) < tol
}
#---------------------#
# Simulation Function #
#---------------------#
simRRmix <- function(nsims=1, n=1, G=1, A=3, B=1, p=0.05, psi=1.5, QC=NULL,
sig20=1, sig21=0.1, trmt=0.5, mu=0, q=0, Lam=NA){
#-----------------------------------------------------------------------------#
# #
# Function to simulate 'nsims' data sets of size 'n' x 'G data from the #
# RRmix Model Distributional Assumptions. #
# #
# nsims - Number of simulated data sets generated - Positive Integer #
# n - Number of observations per simulated data set - Positive Integer #
# G - Number of genes per simulated data set - Positive Integer #
# A - Shape parameter for Inverse-Gamma Distribution - [1, inf) #
# B - Scale parameter for Inverse-Gamma Distribution - [1, inf) #
# p - Proportion of genes differentially expressed - [0, 1] #
# psi - Second mean value for Bg ~ Multivariate Normal - [0, inf) #
# QC - Proportion of genes set as quality controls - [0, 1] #
# sig20 - First variance component for Bg ~ MV Normal - [0, inf) #
# sig21 - Second variance component for Bg ~ MV Normal - [0, inf) #
# trmt - Proportion of observations in treatment group - [0, 1] #
# mu - Overall mean abundance - (-inf, inf) #
# q - Number of latent factors - Positive Integer #
# Lam - Loading matrix of size n x q - (-inf, inf) #
# #
#-----------------------------------------------------------------------------#
## ASSERTIONS ##
if (mode(c(nsims, n, G, A, B, p, psi, QC, sig20, # Assert parameters are numeric
sig21, trmt, q, mu)) != "numeric"){
stop("Argument is not numeric")
}
if ((nsims<=0) | (n<=0) | (G<=0) | # Assert parameters are positive
(A<=0) | (B<=0) | (p<0 | p>1) |
(psi<0) | (sig20<0) | (sig21<0) |
(trmt<0 | trmt>1) | (q<0)){
stop("Argument out of range")
}
integer.check <- is.wholenumber(c(nsims, n, G, q)) # Check if nsims, n, G, & q are integers
if (all.equal(integer.check, c(T,T,T,T)) != TRUE){ # Assert all are integers
stop("nsims, n, or G are not all integers")
}
if ((q==0) & any(!is.na(Lam))){ # Assertions on Lam
stop("Set number of factors to ncol(Lam)")
}
if ((q>0) & (mode(Lam) != "numeric")){
stop("Lam is not a numeric matrix")
}
if ((q > 0) & (class(Lam) != "matrix")){
stop("Lam is not a numeric matrix")
}
if (!is.null(QC)){
if ((QC < 0) | (QC > 1-p)){ # Assertion on QC
stop("Argument out of range: QC < 0 or QC > 1 - p")
}
}
## SIMULATE DATA ##
library(MCMCpack) # Package for rinvgamma() function
library(MASS) # Package for mvrnorm() function
trmt.vect <- c(rep(1, n*trmt), # Treatment Status
rep(0, n*(1-trmt)))
diff.vects <- as.list(rep(NA, nsims)) # Container for latent indicators
QC.vects <- as.list(rep(NA, nsims)) # Container for quality controls
sets <- as.list(rep(NA, nsims)) # Container for simulated data sets
signal.mats <- as.list(rep(NA, nsims)) # Container for true biological signal
factor.mats <- as.list(rep(NA, nsims)) # Container for latent variation
noise.mats <- as.list(rep(NA, nsims)) # Container for random noise
for (i in 1:nsims) { # For-loop to generate each data set
diff.genes <- rep(0, G) # Gene-specific latent indicators
set <- matrix(nrow=n, ncol=G) # Initialize each data set as matrix
mu.set <- matrix(nrow=n, ncol=G) # Initialize overall mean component
XB.set <- matrix(nrow=n, ncol=G) # Initialize biological signal component
LamF.set <- matrix(nrow=n, ncol=G) # Initialize latent factor component
W.set <- matrix(nrow=n, ncol=G) # Initialize random noise component
for (j in 1:G) { # For-loop for gene-specific observations
sig2g <- rinvgamma(1, A, B) # Simulate gene-specific error variance
bg <- rbinom(1, 1, p) # Simulate gene-specific latent indicator
diff.genes[j] <- bg # Store gene-specific latent indicators
mu.Bg <- c(0, bg*psi) # Mean vector for simulated Bg
Sigma.Bg <- ((1-bg)*matrix(c(sig20,0,0,0),2,2)) + # Covariance matrix for simulated Bg
((bg)*matrix(c(sig20,0,0,sig21),2,2))
Bg <- mvrnorm(1, mu.Bg, Sigma.Bg) # Simulated Bg vector
X <- as.matrix(cbind(rep(1,n), # Simulated X matrix
c(rep(1, n*trmt),
rep(0, n*(1-trmt)))))
XBg <- X%*%Bg # Gene indicator term in model
XB.set[,j] <- XBg # Save gene-specific signal to matrix
if (q > 0){ # Simulate latent factors/loadings
Fg <- rnorm(q) # Simulated latent factors
LamFg <- Lam%*%Fg # Latent term in model
LamF.set[,j] <- LamFg # Save gene-specific latent var. to matrix
}
Wg <- rnorm(n, mean=0, sd=sig2g) # Simulate residual terms
W.set[,j] <- Wg # Save random noise to matrix
mu.vect <- rep(mu, n) # Overall mean vector
mu.set[,j] <- mu.vect # Save mean vector to matrix
if(!is.null(QC)){ # "Spike-In" Quality Controls
G.QC <- round(QC*G)
QC.index <- which(diff.genes == 0)[1:G.QC]
XB.set[,QC.index] <- 0
}
if (q > 0){
set <- mu.set + XB.set + LamF.set + W.set # Create Simulated Data With Loadings
}
else{
set <- mu.set + XB.set + W.set # Create Simulated Data Without Loadings
}
}
diff.vects[[i]] <- diff.genes # Store differential genes for set
if (!is.null(QC)){
QC.vects[[i]] <- QC.index # Store quality controls for set
}
set <- data.frame(set) # Convert data set to data frame
sets[[i]] <- set # Append data frame to list
signal.mats[[i]] <- XB.set # Append biological signal to list
factor.mats[[i]] <- LamF.set # Append latent variation to list
noise.mats[[i]] <- W.set # Append random noise to list
}
result <- list(Treatment.Groups = trmt.vect, # Store Results
Differential.Compounds = diff.vects,
Quality.Controls = QC.vects,
Simulated.Data = sets,
Biological.Signal = signal.mats,
Latent.Variation = factor.mats,
Random.Noise = noise.mats)
return(result) # Return Results
}
#------------------------------------------#
# Small Data Simulation - 0 Latent Factors #
#------------------------------------------#
set.seed (1212)
simulations <- simRRmix(nsims=50, n=6, G=265, A=3, B=1, p=0.05, psi=0.5, QC=0.05,
sig20=.55, sig21=.23, trmt=0.5, mu=13, q=0, Lam=NA)
trmt.id <- which(simulations$Treatment.Groups == 1)
cont.id <- which(simulations$Treatment.Groups == 0)
nsims <- length(simulations$Simulated.Data)
#--------------------#
# Individual t-Tests #
#--------------------#
ttest.tstats <- as.list(rep(NA, nsims))
ttest.null <- as.list(rep(NA, nsims))
ttest.TPR <- as.list(rep(NA, nsims))
ttest.FPR <- as.list(rep(NA, nsims))
ttest.PPV <- as.list(rep(NA, nsims))
ttest.FNR <- as.list(rep(NA, nsims))
ttest.FDR <- as.list(rep(NA, nsims))
ttest.PWR <- as.list(rep(NA, nsims))
i <- 1
for (set in simulations$Simulated.Data){
G <- ncol(set)
tstat <- rep(NA, G)
nullp <- rep(NA, G)
for (j in seq(G)){
tstat[j] <- t.test(set[trmt.id, j], set[cont.id, j])$statistic
nullp[j] <- 1 - t.test(set[trmt.id, j], set[cont.id, j])$p.value
}
ttest.tstats[[i]] <- tstat
ttest.null[[i]] <- nullp
i <- i + 1
}
t.crits <- seq(0.0, 50.0, by=0.1)
diff.genes <- simulations$Differential.Compounds
i <- 1
for (tstats in ttest.tstats){
TPR.vect <- rep(NA, length(t.crits))
FPR.vect <- rep(NA, length(t.crits))
PPV.vect <- rep(NA, length(t.crits))
FNR.vect <- rep(NA, length(t.crits))
FDR.vect <- rep(NA, length(t.crits))
PWR.vect <- rep(NA, length(t.crits))
j <- 1
for (t.crit in t.crits){
diff <- which(diff.genes[[i]] == 1) # Truth = +
pos <- which((tstats > t.crit) | (tstats < -t.crit)) # Test = +
TP <- length(intersect(pos, diff)) # TP
TN <- length(intersect(setdiff((1:G), diff), setdiff((1:G), pos))) # TN
FP <- length(setdiff(pos, diff)) # FP
FN <- length(setdiff(diff, pos)) # FN
TPR <- TP/(TP+FN)
FPR <- FP/(FP+TN)
TPR.vect[j] <- TPR
FPR.vect[j] <- FPR
PPV <- TP/(TP+FP)
PPV.vect[j] <- PPV
FNR <- FN/(TP+FN)
FNR.vect[j] <- FNR
FDR <- FP/(TP+FP)
PWR <- TP/(TP+FN)
FDR.vect[j] <- FDR
PWR.vect[j] <- PWR
j <- j + 1
}
ttest.TPR[[i]] <- TPR.vect
ttest.FPR[[i]] <- FPR.vect
ttest.PPV[[i]] <- PPV.vect
ttest.FNR[[i]] <- FNR.vect
ttest.FDR[[i]] <- FDR.vect
ttest.PWR[[i]] <- PWR.vect
i <- i + 1
}
## ttest Simulated ROC Curves
par(pty="s")
plot(c(0,1), c(0,1),
type="l",
lty=2,
col="gray",
xlab="False Positive Rate (1-Specificity)",
ylab="True Positive Rate (Sensitivity)",
main="Independent t-test ROC Curves",
asp=1)
for (i in seq(length(ttest.TPR))){
lines(ttest.FPR[[i]], ttest.TPR[[i]])
}
legend("bottomright",
legend = c("Random Guessing", "t-test"),
col = c("gray",1),
lty = c(2,1),
lwd = c(1,1),
cex = 0.6)
## TPR Averages
ttest.TPR.avmat <- ttest.TPR[[1]]
for (i in (2:length(ttest.TPR))){
ttest.TPR.avmat <- cbind(ttest.TPR.avmat, ttest.TPR[[i]])
}
ttest.TPR.avg <- rowMeans(ttest.TPR.avmat, na.rm=T)
## FPR Averages
ttest.FPR.avmat <- ttest.FPR[[1]]
for (i in (2:length(ttest.FPR))){
ttest.FPR.avmat <- cbind(ttest.FPR.avmat, ttest.FPR[[i]])
}
ttest.FPR.avg <- rowMeans(ttest.FPR.avmat, na.rm=T)
## PPV Averages
ttest.PPV.avmat <- ttest.PPV[[1]]
for (i in (2:length(ttest.PPV))){
ttest.PPV.avmat <- cbind(ttest.PPV.avmat, ttest.PPV[[i]])
}
ttest.PPV.avg <- rowMeans(ttest.PPV.avmat, na.rm=T)
## FNR Averages
ttest.FNR.avmat <- ttest.FNR[[1]]
for (i in (2:length(ttest.FNR))){
ttest.FNR.avmat <- cbind(ttest.FNR.avmat, ttest.FNR[[i]])
}
ttest.FNR.avg <- rowMeans(ttest.FNR.avmat, na.rm=T)
## null Averages (By Rank)
ttest.null.avmat <- ttest.null[[1]][order(ttest.null[[1]], decreasing=T)]
for (i in (2:length(ttest.null))){
ttest.null.avmat <- cbind(ttest.null.avmat, ttest.null[[i]][order(ttest.null[[i]], decreasing=T)])
}
ttest.null.avg <- rowMeans(ttest.null.avmat, na.rm=T)
#-------#
# LIMMA #
#-------#
library(limma)
limma.tstats <- as.list(rep(NA, nsims))
limma.null <- as.list(rep(NA, nsims))
limma.TPR <- as.list(rep(NA, nsims))
limma.FPR <- as.list(rep(NA, nsims))
limma.PPV <- as.list(rep(NA, nsims))
limma.FNR <- as.list(rep(NA, nsims))
limma.FDR <- as.list(rep(NA, nsims))
limma.PWR <- as.list(rep(NA, nsims))
trmt.ind <- simulations$Treatment.Groups # Set Treatment Groups
i <- 1
for (set in simulations$Simulated.Data){
design <- model.matrix(~factor(trmt.ind)) # Create Design Matrix
fit <- lmFit(t(set),design) # Fit Model
ebayes <- eBayes(fit) # Get Statistics
limma.tstats[[i]] <- ebayes$t[,2]
limma.null[[i]] <- 1 - exp(ebayes$lods[,2]) / ( 1 + exp(ebayes$lods[,2]))
i <- i + 1
}
t.crits <- seq(0.0, 50.0, by=0.1)
diff.genes <- simulations$Differential.Compounds
i <- 1
for (tstats in limma.tstats){
TPR.vect <- rep(NA, length(t.crits))
FPR.vect <- rep(NA, length(t.crits))
PPV.vect <- rep(NA, length(t.crits))
FNR.vect <- rep(NA, length(t.crits))
FDR.vect <- rep(NA, length(t.crits))
PWR.vect <- rep(NA, length(t.crits))
j <- 1
for (t.crit in t.crits){
diff <- which(diff.genes[[i]] == 1)
pos <- which((tstats > t.crit) | (tstats < -t.crit))
TP <- length(intersect(pos, diff)) # TP
TN <- length(intersect(setdiff((1:G), diff), setdiff((1:G), pos))) # TN
FP <- length(setdiff(pos, diff)) # FP
FN <- length(setdiff(diff, pos)) # FN
TPR <- TP/(TP+FN)
FPR <- FP/(FP+TN)
TPR.vect[j] <- TPR
FPR.vect[j] <- FPR
PPV <- TP/(TP+FP)
PPV.vect[j] <- PPV
FNR <- FN/(TP+FN)
FNR.vect[j] <- FNR
FDR <- FP/(TP+FP)
PWR <- TP/(TP+FN)
FDR.vect[j] <- FDR
PWR.vect[j] <- PWR
j <- j + 1
}
limma.TPR[[i]] <- TPR.vect
limma.FPR[[i]] <- FPR.vect
limma.PPV[[i]] <- PPV.vect
limma.FNR[[i]] <- FNR.vect
limma.FDR[[i]] <- FDR.vect
limma.PWR[[i]] <- PWR.vect
i <- i + 1
}
## LIMMA Simulated ROC Curves
par(pty="s")
plot(c(0,1), c(0,1),
type="l",
lty=2,
col="gray",
xlab="False Positive Rate (1-Specificity)",
ylab="True Positive Rate (Sensitivity)",
main="LIMMA ROC Curves", asp=1)
for (i in seq(length(limma.TPR))){
lines(limma.FPR[[i]], limma.TPR[[i]])
}
legend("bottomright",
legend = c("Random Guessing", "LIMMA"),
col = c("gray",1),
lty = c(2,1),
lwd = c(1,1),
cex = 0.6)
## TPR Averages
limma.TPR.avmat <- limma.TPR[[1]]
for (i in (2:length(limma.TPR))){
limma.TPR.avmat <- cbind(limma.TPR.avmat, limma.TPR[[i]])
}
limma.TPR.avg <- rowMeans(limma.TPR.avmat, na.rm=T)
## FPR Averages
limma.FPR.avmat <- limma.FPR[[1]]
for (i in (2:length(limma.FPR))){
limma.FPR.avmat <- cbind(limma.FPR.avmat, limma.FPR[[i]])
}
limma.FPR.avg <- rowMeans(limma.FPR.avmat, na.rm=T)
## PPV Averages
limma.PPV.avmat <- limma.PPV[[1]]
for (i in (2:length(limma.PPV))){
limma.PPV.avmat <- cbind(limma.PPV.avmat, limma.PPV[[i]])
}
limma.PPV.avg <- rowMeans(limma.PPV.avmat, na.rm=T)
## FNR Averages
limma.FNR.avmat <- limma.FNR[[1]]
for (i in (2:length(limma.FNR))){
limma.FNR.avmat <- cbind(limma.FNR.avmat, limma.FNR[[i]])
}
limma.FNR.avg <- rowMeans(limma.FNR.avmat, na.rm=T)
## null Averages (By Rank)
limma.null.avmat <- limma.null[[1]][order(limma.null[[1]], decreasing=T)]
for (i in (2:length(limma.null))){
limma.null.avmat <- cbind(limma.null.avmat, limma.null[[i]][order(limma.null[[i]], decreasing=T)])
}
limma.null.avg <- rowMeans(limma.null.avmat, na.rm=T)
#-------#
# RRmix #
#-------#
RRmix.post <- as.list(rep(NA, nsims))
RRmix.TPR <- as.list(rep(NA, nsims))
RRmix.FPR <- as.list(rep(NA, nsims))
RRmix.PPV <- as.list(rep(NA, nsims))
RRmix.FNR <- as.list(rep(NA, nsims))
RRmix.FDR <- as.list(rep(NA, nsims))
RRmix.PWR <- as.list(rep(NA, nsims))
trmt.ind <- simulations$Treatment.Groups # Set Treatment Groups
source('HEFT-RRmix.R') # Source RRmix Script
i <- 1
for (set in simulations$Simulated.Data){
G <- ncol(set) # Number of Metabolites
n <- nrow(set) # Number of Observations
Xc <- matrix(nrow=0, ncol=0) # Covariate Matrix
mu.0 <- 1/G * as.matrix(set) %*% rep(1,G) # Initialize mu
eta.0 <- matrix(0, 2+ncol(Xc), G) # Initialize eta
betac.0 <- matrix(nrow=0, ncol=0) # Initialize beta_c
sig20.0 <- 1 # Initialize sig^2_0
sig21.0 <- 0.1 # Initialize sig^2_1
result <- runHEFTmix(G.in=G, # Run RRmix Model
n.in=n,
Xc.in=Xc,
Y.in=as.matrix(set),
SNP.in=trmt.ind,
mu.0=mu.0,
betac.0=betac.0,
sig20.0=sig20.0,
sig21.0=sig21.0,
p.0=0.05,
er_tol.in=10^(-3),
q.in=2)
RRmix.post[[i]] <- result[['b_g']]
i <- i + 1
}
post.probs <- seq(0.0, 1.0, by=0.0001)
diff.genes <- simulations$Differential.Compounds
i <- 1
for (posts in RRmix.post){
TPR.vect <- rep(NA, length(post.probs))
FPR.vect <- rep(NA, length(post.probs))
PPV.vect <- rep(NA, length(post.probs))
FNR.vect <- rep(NA, length(post.probs))
FDR.vect <- rep(NA, length(post.probs))
PWR.vect <- rep(NA, length(post.probs))
j <- 1
for (post.prob in post.probs){
diff <- which(diff.genes[[i]] == 1)
pos <- which(posts > post.prob)
TP <- length(intersect(pos, diff)) # TP
TN <- length(intersect(setdiff((1:G), diff), setdiff((1:G), pos))) # TN
FP <- length(setdiff(pos, diff)) # FP
FN <- length(setdiff(diff, pos)) # FN
TPR <- TP/(TP+FN)
FPR <- FP/(FP+TN)
TPR.vect[j] <- TPR
FPR.vect[j] <- FPR
PPV <- TP/(TP+FP)
PPV.vect[j] <- PPV
FNR <- FN/(TP+FN)
FNR.vect[j] <- FNR
FDR <- FP/(TP+FP)
PWR <- TP/(TP+FN)
FDR.vect[j] <- FDR
PWR.vect[j] <- PWR
j <- j + 1
}
RRmix.TPR[[i]] <- TPR.vect
RRmix.FPR[[i]] <- FPR.vect
RRmix.PPV[[i]] <- PPV.vect
RRmix.FNR[[i]] <- FNR.vect
RRmix.FDR[[i]] <- FDR.vect
RRmix.PWR[[i]] <- PWR.vect
i <- i + 1
}
## RRmix Simulated ROC Curves
par(pty="s")
plot(c(0,1), c(0,1),
type="l",
lty=2,
col="gray",
xlab="False Positive Rate (1-Specificity)",
ylab="True Positive Rate (Sensitivity)",
main="RRmix ROC Curves", asp=1)
for (i in seq(length(RRmix.TPR))){
lines(RRmix.FPR[[i]], RRmix.TPR[[i]])
}
legend("bottomright",
legend = c("Random Guessing", "RRmix"),
col = c("gray",1),
lty = c(2,1),
lwd = c(1,1),
cex = 0.6)
## TPR Averages
RRmix.TPR.avmat <- RRmix.TPR[[1]]
for (i in (2:length(RRmix.TPR))){
RRmix.TPR.avmat <- cbind(RRmix.TPR.avmat, RRmix.TPR[[i]])
}
RRmix.TPR.avg <- rowMeans(RRmix.TPR.avmat, na.rm=T)
## FPR Averages
RRmix.FPR.avmat <- RRmix.FPR[[1]]
for (i in (2:length(RRmix.FPR))){
RRmix.FPR.avmat <- cbind(RRmix.FPR.avmat, RRmix.FPR[[i]])
}
RRmix.FPR.avg <- rowMeans(RRmix.FPR.avmat, na.rm=T)
## PPV Averages
RRmix.PPV.avmat <- RRmix.PPV[[1]]
for (i in (2:length(RRmix.PPV))){
RRmix.PPV.avmat <- cbind(RRmix.PPV.avmat, RRmix.PPV[[i]])
}
RRmix.PPV.avg <- rowMeans(RRmix.PPV.avmat, na.rm=T)
## FNR Averages
RRmix.FNR.avmat <- RRmix.FNR[[1]]
for (i in (2:length(RRmix.FNR))){
RRmix.FNR.avmat <- cbind(RRmix.FNR.avmat, RRmix.FNR[[i]])
}
RRmix.FNR.avg <- rowMeans(RRmix.FNR.avmat, na.rm=T)
## FDR Averages
RRmix.FDR.avmat <- RRmix.FDR[[1]]
for (i in (2:length(RRmix.FDR))){
RRmix.FDR.avmat <- cbind(RRmix.FDR.avmat, RRmix.FDR[[i]])
}
RRmix.FDR.avg <- rowMeans(RRmix.FDR.avmat, na.rm=T)
## PWR Averages
RRmix.PWR.avmat <- RRmix.PWR[[1]]
for (i in (2:length(RRmix.PWR))){
RRmix.PWR.avmat <- cbind(RRmix.PWR.avmat, RRmix.PWR[[i]])
}
RRmix.PWR.avg <- rowMeans(RRmix.PWR.avmat, na.rm=T)
## post Averages (By Rank, Not Index)
RRmix.post.avmat <- RRmix.post[[1]][order(RRmix.post[[1]])]
for (i in (2:length(RRmix.post))){
RRmix.post.avmat <- cbind(RRmix.post.avmat, RRmix.post[[i]][order(RRmix.post[[i]])])
}
RRmix.post.avg <- rowMeans(RRmix.post.avmat, na.rm=T)
RRmix.null.avg <- 1 - RRmix.post.avg
#----------------#
# FAMT - NBF Set #
#----------------#
library(FAMT)
FAMT.NBF.Fstats <- as.list(rep(NA, nsims))
FAMT.NBF.null <- as.list(rep(NA, nsims))
FAMT.NBF.TPR <- as.list(rep(NA, nsims))
FAMT.NBF.FPR <- as.list(rep(NA, nsims))
FAMT.NBF.PPV <- as.list(rep(NA, nsims))
FAMT.NBF.FNR <- as.list(rep(NA, nsims))
FAMT.NBF.FDR <- as.list(rep(NA, nsims))
FAMT.NBF.PWR <- as.list(rep(NA, nsims))
trmt.ind <- simulations$Treatment.Groups # Set Treatment Groups
i <- 1
for (set in simulations$Simulated.Data){
expr.FAMT.NBF <- t(set) # Set Data Matrix
colnames(expr.FAMT.NBF) <- (1:ncol(expr.FAMT.NBF))
cov.FAMT.NBF <- data.frame(id = colnames(expr.FAMT.NBF), # Set Covariates Matrix
trmt = as.factor(trmt.ind))
data.FAMT.NBF <- as.FAMTdata(expression = expr.FAMT.NBF, # Make Data Structure
covariates = cov.FAMT.NBF,
idcovar = 1)
fit.FAMT.NBF <- modelFAMT(data.FAMT.NBF, # Fit FAMT Model
x = 2,
test = 2,
nbf = 0)
FAMT.NBF.Fstats[[i]] <- fit.FAMT.NBF$adjtest
FAMT.NBF.null[[i]] <- 1 - fit.FAMT.NBF$adjpval
i <- i + 1
}
Fcrits <- seq(0, 1000.0, by=0.1)
diff.genes <- simulations$Differential.Compounds
i <- 1
for (Fstats in FAMT.NBF.Fstats){
TPR.vect <- rep(NA, length(Fcrits))
FPR.vect <- rep(NA, length(Fcrits))
PPV.vect <- rep(NA, length(Fcrits))