-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiM_Bretthorst_PG.r
1635 lines (1460 loc) · 64.9 KB
/
DiM_Bretthorst_PG.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
################################################################################
# ON THE DIFFERENCE OF MEANS
################################################################################
# original Mathematica code by Phil Gregory
# this was used as a role model to write the R code
# Ch. 9: Bayesian analysis of two independent samples
# Introduction
# This is a Mathematica implementation of the probability calculations discussed in the book in the section entitled,
# "Bayesian Comparison of Two Samples?".
#
# based on G.L. Bretthorst (1993) "On the difference of means"
#
# see also Mathematica code by UM Studer (1996 + 1998) on the same problem (paper)
# taken code from there to allow calculations based only on summary statistics
# and not on raw data (see also Bretthorst, 1993, for an example taken from Jaynes)
# R code by Leo G 2017
# first = 2017-04-19
# last = 2017-27-04
# last = 2024-05-21
# notes:
# *- introduce logs to integral calculations, but probably that won't help...
# *- very small numbers are slightly different from Mathematica -> e.g. e-230
# additional note:
# *- we use the sdame variable names and labels like P Gregory to allow comparison
# with his Mathematica notebook. This is different from the UMS script where the
# labels from UMS are used.
################################################################################
# pre-calculations taken from UMS 1998
ums2pg <- function(inputvalues)
#convert values from UMS implementation to PG implementation
{
#mapping
#n
#NN <- Ni+Nii
#
#dbar
#DD <- (Ni * Di + Nii * Dii) / NN
#
#d1squbar
#Dsi <- (Ni-1) / Ni * si^2 + Di^2
#
#d2squbar
#Dsii <- (Nii-1) / Nii * sii^2 + Dii^2
#
#dsqubar
#DsD <- (Ni * Dsi + Nii * Dsii) / NN
#
#dstd
#ss <- sqrt(NN * (DsD - DD^2) / (NN-1))
d1bar <- inputvalues[["Di"]]
d2bar <- inputvalues[["Dii"]]
d1std <- inputvalues[["si"]]
d2std <- inputvalues[["sii"]]
n1 <- inputvalues[["Ni"]]
n2 <- inputvalues[["Nii"]]
low <- inputvalues[["L"]]
high <- inputvalues[["H"]]
sigma.low <- inputvalues[["sL"]]
sigma.high <- inputvalues[["sH"]]
snames <- inputvalues[["snames"]]
if(is.null(inputvalues[["ndelta"]])) ndelta <- 1000 else ndelta <- inputvalues[["ndelta"]]
if(is.null(inputvalues[["nr"]])) nr <- 1000 else nr <- inputvalues[["nr"]]
# manual update of the data
n <- n1 + n2
dbar <- (d1bar*n1 + d2bar*n2)/n#(Ni+Nii)
# Compute the mean square average of the 1st data set
d1squbar <- (n1-1) / n1 * d1std^2 + d1bar^2
# Compute the mean square average of the 2nd data set
d2squbar <- (n2-1) / n2 * d2std^2 + d2bar^2
# Compute the mean square average of the combined data set
dsqubar <- (n1 * d1squbar + n2 * d2squbar) / n
# Compute the standard deviation of the combined data set
dstd <- sqrt(n * (dsqubar - dbar^2) / (n-1))
res <- list(snames=snames,
n1=n1, n2=n2, n=n,
low=low, high=high, sigma.low=sigma.low, sigma.high=sigma.high,
d1bar=d1bar, d2bar=d2bar, dbar=dbar,
d1std=d1std, d2std=d2std, dstd=dstd,
d1squbar=d1squbar, d2squbar=d2squbar, dsqubar=dsqubar,
ndelta=ndelta, nr=nr)
return(res)
}
# call:
#
################################################################################
################################################################################
DiM.pg <- function(invtyp=NULL, inputvalues=NULL, print.res=TRUE, dig=4, Nsteps=100, method="normal", ndelta=1000, nr=1000)
{
#Bretthorst DiM
#adapted from Mathematica code by Phil Gregory
#for large numbers
if(method=="brob")
{
library(Brobdingnag)
cat("\nUsing log() and package 'Brobdingnag'\nAll results are expressed as log(RESULT)\n")
}
####################
# START INPUT VALUES
if(invtyp=="ums")
{
d1 <- NULL
d2 <- NULL
ums2pg.res <- ums2pg(inputvalues)
snames <- ums2pg.res[["snames"]]
n1 <- ums2pg.res[["n1"]]
n2 <- ums2pg.res[["n2"]]
n <- ums2pg.res[["n"]]
low <- ums2pg.res[["low"]]
high <- ums2pg.res[["high"]]
sigma.low <- ums2pg.res[["sigma.low"]]
sigma.high <- ums2pg.res[["sigma.high"]]
d1bar <- ums2pg.res[["d1bar"]]
d2bar <- ums2pg.res[["d2bar"]]
dbar <- ums2pg.res[["dbar"]]
d1std <- ums2pg.res[["d1std"]]
d2std <- ums2pg.res[["d2std"]]
dstd <- ums2pg.res[["dstd"]]
d1squbar <- ums2pg.res[["d1squbar"]]
d2squbar <- ums2pg.res[["d2squbar"]]
dsqubar <- ums2pg.res[["dsqubar"]]
if(!is.null(inputvalues[["ndelta"]])) ndelta <- inputvalues[["ndelta"]]
if(!is.null(inputvalues[["nr"]])) nr <- inputvalues[["nr"]]
} else if(invtyp=="pg")
{
# input values PG scheme
snames <- inputvalues[["snames"]]
d1 <- inputvalues[["d1"]]
d2 <- inputvalues[["d2"]]
ndelta <- inputvalues[["ndelta"]]
nr <- inputvalues[["nr"]]
high <- inputvalues[["high"]]
low <- inputvalues[["low"]]
sigma.low <- inputvalues[["sigma.low"]]
sigma.high <- inputvalues[["sigma.high"]]
# sample sizes
n1 <- length(d1)
n2 <- length(d2)
n <- n1 + n2
# Compute the average of the 1st data set
d1bar <- sum(d1)/n1 #mean
# Compute the average of the 2nd data set
d2bar <- sum(d2)/n2
# Compute the average of the combined data set
dbar <- sum(d1,d2)/n
# Compute the mean square average of the 1st data set
d1squbar <- sum(d1 * d1)/n1
# Compute the mean square average of the 2nd data set
d2squbar <- sum(d2 * d2)/n2
# Compute the mean square average of the combined data set
dsqubar <- (sum(d1 * d1) + sum(d2 * d2))/n
# Neater way of computing the above using a vector dot product
# (d1 %*% d1 + d2 %*% d2)/n
# Compute the standard deviation of the 1st data set
d1std <- sd(d1)
# Compute the standard deviation of the 2nd data set
d2std <- sd(d2)
# Compute the standard deviation of the combined data set
dstd <- sd(c(d1,d2))
} else
{
stop(paste("\nNo valid input method for input values given.\n\n",sep=""))
}
#### constants
# Set prior limits (assumed the same for each data set) on mean (low,high),
# and prior limits (assumed the same for each data set) on the
# standard deviation (sigmalow, sigmahigh).
# range mean
Rc <- high - low
# range sd
Rsigma <- sigma.high / sigma.low
# END INPUT VALUES
################################################################################
# pCSk
# Compute pCSk = p(C,S|D_1,D_2,I) * p(D_1,D_2|I)
# according to GL Bretthorst (1993) formulas and appendix C of Gregory (2005)
z <- n * (dsqubar - dbar^2)
uL <- sqrt(n/2) * (low - dbar)
uH <- sqrt(n/2) * (high - dbar)
(uH/uL) > 0
# error function
errf <- function(ERR) return( 2*pnorm(ERR*sqrt(2))-1 )
# formula to calculate area below the curve / integral
pCSk.hypo <- function(n, Rc, Rsigma, sigma1, z, uH, uL)
{
fnc1 <- function(sigma1)
{
( (2*pi)^(-n/2)*sqrt(pi/(2*n)) ) /
( 4*Rc*log(Rsigma) ) *
sigma1^(-n) *
exp(-z/(2*sigma1^2)) *
( errf(ERR=uH/sigma1) - errf(ERR=uL/sigma1) )
}
# integrate
pCSk <- integrate(fnc1, lower=sigma.low, upper=sigma.high)$value
return(pCSk)
}
#call:
#pCSk <- pCSk.hypo(n=n, Rc=Rc, Rsigma=Rsigma, z=z, uH=uH, uL=uL)
# BROB
# error function
errf.brob <- function(ERR) return( as.brob( 2*pnorm(ERR*sqrt(2))-1 ))
# formula to calculate area below the curve / integral
pCSk.hypo.brob <- function(n,Rc,Rsigma,sigma1,z,uH,uL,Nsteps)
{
fnc1.brob <- function(sigma1, Nsteps=Nsteps)
{
as.brob( (2*pi)^(-n/2)*sqrt(pi/(2*n)) ) /
as.brob( 4*Rc*log(Rsigma) ) *
as.brob( sigma1)^(-n) *
exp(as.brob( -z/(2*sigma1^2)) ) *
( errf.brob(ERR=uH/sigma1) - errf.brob(ERR=uL/sigma1) )
}
# integrate
#sL=lower, sH=upper
pCSk.brob <- simpsonrule.nlb(fx=fnc1.brob, lower=sigma.low, upper=sigma.high, method="brob", Nsteps=Nsteps)
return(pCSk.brob)
}
#call:
#pCSk.brob <- pCSk.hypo.brob(n=n, Rc=Rc, Rsigma=Rsigma, z=z, uH=uH, uL=uL, Nsteps=Nsteps)
#fnc1.brob(sigma.low)
#fnc1.brob(sigma.high)
################################################################################
# pCSbark
# Compute pCSbark = p(C,Sbar|D_1,D_2,I) * p(D_1,D_2|I)
u1A <- function(A) return( n1/2 * (d1squbar - 2 * A * d1bar + A^2) )
u2A <- function(A) return( n2/2 * (d2squbar - 2 * A * d2bar + A^2) )
# formula to calculate area below the curve / integral
pCSbark.hypo <- function(n, n1, n2, Rc, Rsigma, sigma.high, sigma.low, low, high)
{
fnc2 <- function(A)
{
(2*pi)^(-n/2) *
gamma(n1/2) *
gamma(n2/2) /
( 16*Rc*log(Rsigma)^2 ) *
u1A(A)^(-n1/2) *
u2A(A)^(-n2/2) *
( pgamma(u1A(A)/sigma.high^2,n1/2) - pgamma(u1A(A)/sigma.low^2,n1/2) ) *
( pgamma(u2A(A)/sigma.high^2,n2/2) - pgamma(u2A(A)/sigma.low^2,n2/2) )
}
# integrate
pCSbark <- integrate(fnc2, lower=low, upper=high)$value
return(pCSbark)
}
#call:
#pCSbark <- pCSbark.hypo(n=n, n1=n1, n2=n2, Rc=Rc, Rsigma=Rsigma, sigma.high=sigma.high, sigma.low=sigma.low, low=low, high=high)
# BROB
u1A.brob <- function(A) return( as.brob( n1/2 * (d1squbar - 2 * A * d1bar + A^2) ))
u2A.brob <- function(A) return( as.brob( n2/2 * (d2squbar - 2 * A * d2bar + A^2) ))
# formula to calculate area below the curve / integral
pCSbark.hypo.brob <- function(n, n1, n2, Rc, Rsigma, sigma.high, sigma.low, low, high, Nsteps)
{
fnc2.brob <- function(A)
{
as.brob( (2*pi))^(-n/2) *
brob( lgamma(n1/2) ) *
brob( lgamma(n2/2)) /
as.brob( 16*Rc*log(Rsigma)^2 ) *
as.brob( u1A(A))^(-n1/2) *
as.brob( u2A(A))^(-n2/2) *
( as.brob( pgamma(u1A(A)/sigma.high^2,n1/2, log.p=FALSE) ) - as.brob( pgamma(u1A(A)/sigma.low^2,n1/2, log.p=FALSE) ) ) *
( as.brob( pgamma(u2A(A)/sigma.high^2,n2/2, log.p=FALSE) ) - as.brob( pgamma(u2A(A)/sigma.low^2,n2/2, log.p=FALSE) ) )
}
# integrate
pCSbark.brob <- simpsonrule.nlb(fx=fnc2.brob, lower=low, upper=high, method="brob", Nsteps=Nsteps)
#simpsonrule.nlb(fx=fnc2.brob, lower=low, upper=high, method="brob", Nsteps=Nsteps)
#simpsonrule.brob(fx=fnc2.brob, sL=low, sH=high, Nsteps=Nsteps)
#
## taken from sintegral from Bolstad2
#sek <- seq(sL,sH,length=Nsteps)
#l.intv <- 2*Nsteps+1
#intv.x <- approx(sek,sek,n=l.intv)$x
#as.list(sapply(seq_along(1:l.intv), function(x) fx(intv.x[x])))
#
return(pCSbark.brob)
}
#call:
#pCSbark.brob <- pCSbark.hypo.brob(n=n, n1=n1, n2=n2, Rc=Rc, Rsigma=Rsigma, sigma.high=sigma.high, sigma.low=sigma.low, low=low, high=high, Nsteps=Nsteps)
################################################################################
# pCbarSk
# Compute pCbarSk = p(Cbar,S|D_1,D_2,I) * p(D_1,D_2|I)
z1 <- n1 * (d1squbar - d1bar^2)
u1H <- sqrt(n1/2) * (high - d1bar)
u1L <- sqrt(n1/2) * (low - d1bar)
z2 <- n2 * (d2squbar - d2bar^2)
u2H <- sqrt(n2/2) * (high - d2bar)
u2L <- sqrt(n2/2) * (low - d2bar)
(u1H/u1L) > 0
(u2H/u2L) > 0
# formula to calculate area below the curve / integral
pCbarSk.hypo <- function(n, Rc, Rsigma, n1, n2, z1, z2, u1H, u1L, u2H, u2L)
{
fnc3 <- function(sigma1)
{
(2*pi)^(-n/2) * pi / ( 8 * Rc^2 * log(Rsigma) * sqrt(n1*n2) ) * sigma1^(-n+1) *
exp(-(z1+z2)/(2*sigma1^2)) *
(errf(ERR=u1H/sigma1) - errf(ERR=u1L/sigma1)) *
(errf(ERR=u2H/sigma1) - errf(ERR=u2L/sigma1))
}
# integrate
pCbarSk <- integrate(fnc3, lower=sigma.low, upper=sigma.high)$value
return(pCbarSk)
}
#call:
#pCbarSk <- pCbarSk.hypo(n=n, Rc=Rc, Rsigma=Rsigma, n1=n1, n2=n2, z1=z1, z2=z2, u1H=u1H, u1L=u1L, u2H=u2H, u2L=u2L)
# BROB
pCbarSk.hypo.brob <- function(n, Rc, Rsigma, n1, n2, z1, z2, u1H, u1L, u2H, u2L, Nsteps)
{
fnc3.brob <- function(sigma1)
{
as.brob(2*pi)^(-n/2) * pi /
as.brob( ( 8 * Rc^2 * log(Rsigma) * sqrt(n1*n2) )) *
as.brob(sigma1)^(-n+1) *
exp(as.brob( -(z1+z2)/(2*sigma1^2)) ) *
(errf.brob(ERR=u1H/sigma1) - errf.brob(ERR=u1L/sigma1)) *
(errf.brob(ERR=u2H/sigma1) - errf.brob(ERR=u2L/sigma1))
}
# integrate
pCbarSk.brob <- simpsonrule.nlb(fx=fnc3.brob, lower=sigma.low, upper=sigma.high, method="brob", Nsteps=Nsteps)
return(pCbarSk.brob)
}
#call:
#pCbarSk.brob <- pCbarSk.hypo.brob(n=n, Rc=Rc, Rsigma=Rsigma, n1=n1, n2=n2, z1=z1, z2=z2, u1H=u1H, u1L=u1L, u2H=u2H, u2L=u2L, Nsteps=Nsteps)
################################################################################
# pCbarSbark
# Compute pCbarSbark = p(Cbar,Sbar|D_1,D_2,I) * p(D_1,D_2|I)
(u1H/u1L) > 0
(u2H/u2L) > 0
# formula to calculate area below the curve / integral
pCbarSbark.A.hypo <- function(n1, z1, u1H, u1L)
{
fnc4.A <- function(sigma1)
{
sigma1^(-n1) * exp(-z1/(2*sigma1^2)) *
(errf(ERR=u1H/sigma1) - errf(ERR=u1L/sigma1))
}
# integrate
pCbarSbark.A <- integrate(fnc4.A, lower=sigma.low, upper=sigma.high)$value
return(pCbarSbark.A)
}
#call:
#pCbarSbark.A <- pCbarSbark.A.hypo(n1=n1, z1=z1, u1H=u1H, u1L=u1L)
# BROB
pCbarSbark.A.hypo.brob <- function(n1, z1, u1H, u1L, Nsteps)
{
fnc4.A.brob <- function(sigma1)
{
as.brob(sigma1) ^(-n1) *
exp(as.brob(-z1/(2*sigma1^2)) ) *
(errf.brob(ERR=u1H/sigma1) - errf.brob(ERR=u1L/sigma1))
}
# integrate
pCbarSbark.A.brob <- simpsonrule.nlb(fx=fnc4.A.brob, lower=sigma.low, upper=sigma.high, method="brob", Nsteps=Nsteps)
return(pCbarSbark.A.brob)
}
#call:
#pCbarSbark.A.brob <- pCbarSbark.A.hypo.brob(n1=n1, z1=z1, u1H=u1H, u1L=u1L, Nteps=Nsteps)
# formula to calculate area below the curve / integral
pCbarSbark.B.hypo <- function(n1, z1, u2H, u2L)
{
fnc4.B <- function(sigma2)
{
sigma2^(-n2) * exp(-z2/(2*sigma2^2)) *
(errf(ERR=u2H/sigma2) - errf(ERR=u2L/sigma2))
}
# integrate
pCbarSbark.B <- integrate(fnc4.B, lower=sigma.low, upper=sigma.high)$value
return(pCbarSbark.B)
}
#call:
#pCbarSbark.B <- pCbarSbark.B.hypo(n1=n1, z1=z1, u2H=u2H, u2L=u2L)
# BROB
pCbarSbark.B.hypo.brob <- function(n1, z1, u2H, u2L, Nsteps)
{
fnc4.B.brob <- function(sigma2)
{
as.brob( sigma2)^(-n2) *
exp( as.brob(-z2/(2*sigma2^2)) ) *
(errf.brob(ERR=u2H/sigma2) - errf.brob(ERR=u2L/sigma2))
}
# integrate
pCbarSbark.B.brob <- simpsonrule.nlb(fx=fnc4.B.brob, lower=sigma.low, upper=sigma.high, method="brob", Nsteps=Nsteps)
return(pCbarSbark.B.brob)
}
#call:
#pCbarSbark.B.brob <- pCbarSbark.B.hypo.brob(n1=n1, z1=z1, u2H=u2H, u2L=u2L, Nsteps=Nsteps)
################################################################################
# compile results
# independent from BROB
# inputs
# table/ dataframe with input values
desc.df <- data.frame("Data set" = c(snames,"combined"),
"Sample Size" = c(n1,n2,n1+n2),
"Standard Deviation" = c(d1std,d2std,dstd),
"Variance" = c(d1std^2,d2std^2,dstd^2),
"Mean" = c(d1bar,d2bar,dbar),
check.names = FALSE)
# table/ dataframe with prior values/ information
prior.df <- data.frame("Type" = c("Prior Mean lower bound",
"Prior Mean upper bound",
"Prior Standard Deviation lower bound",
"Prior Standard Deviation upper bound",
"Number of steps for plotting p(delta | D_1, D_2, I)",
"Number of steps for plotting p(r | D_1, D_2, I)"),
"Value" = c(low,high,sigma.low,sigma.high,ndelta,nr),
check.names = FALSE)
cat("\nMethod = ",method,"\n",sep="")
# compute single hypotheses
if(method=="normal")
{
cat("\nCalculate pCSk")
pCSk <- pCSk.hypo(n=n, Rc=Rc, Rsigma=Rsigma, z=z, uH=uH, uL=uL)
cat("\nCalculate pCSbark")
pCSbark <- pCSbark.hypo(n=n, n1=n1, n2=n2, Rc=Rc, Rsigma=Rsigma, sigma.high=sigma.high, sigma.low=sigma.low, low=low, high=high)
cat("\nCalculate pCbarSk")
pCbarSk <- pCbarSk.hypo(n=n, Rc=Rc, Rsigma=Rsigma, n1=n1, n2=n2, z1=z1, z2=z2, u1H=u1H, u1L=u1L, u2H=u2H, u2L=u2L)
cat("\nCalculate pCbarSbark.A")
pCbarSbark.A <- pCbarSbark.A.hypo(n1=n1, z1=z1, u1H=u1H, u1L=u1L)
cat("\nCalculate pCbarSbark.B")
pCbarSbark.B <- pCbarSbark.B.hypo(n1=n1, z1=z1, u2H=u2H, u2L=u2L)
# compile pCbarSbark.A and pCbarSbark.B
pCbarSbark <- ( (2*pi)^(-n/2) * pi * pCbarSbark.A * pCbarSbark.B ) / ( 8 * Rc^2 * log(Rsigma)^2 * sqrt(n1*n2) )
} else if(method=="brob")
{
# BROB
cat("\nCalculate pCSk")
pCSk <- pCSk.hypo.brob(n=n, Rc=Rc, Rsigma=Rsigma, z=z, uH=uH, uL=uL, Nsteps=Nsteps)
cat("\nCalculate pCSbark")
pCSbark <- pCSbark.hypo.brob(n=n, n1=n1, n2=n2, Rc=Rc, Rsigma=Rsigma, sigma.high=sigma.high, sigma.low=sigma.low, low=low, high=high, Nsteps=Nsteps)
cat("\nCalculate pCbarSk")
pCbarSk <- pCbarSk.hypo.brob(n=n, Rc=Rc, Rsigma=Rsigma, n1=n1, n2=n2, z1=z1, z2=z2, u1H=u1H, u1L=u1L, u2H=u2H, u2L=u2L, Nsteps=Nsteps)
cat("\nCalculate pCbarSbark.A")
pCbarSbark.A <- pCbarSbark.A.hypo.brob(n1=n1, z1=z1, u1H=u1H, u1L=u1L, Nsteps=Nsteps)
cat("\nCalculate pCbarSbark.B")
pCbarSbark.B <- pCbarSbark.B.hypo.brob(n1=n1, z1=z1, u2H=u2H, u2L=u2L, Nsteps=Nsteps)
# compile pCbarSbark.A and pCbarSbark.B
pCbarSbark <- ( as.brob(2*pi)^(-n/2) * pi * pCbarSbark.A * pCbarSbark.B ) / ( 8 * Rc^2 * log(Rsigma)^2 * sqrt(n1*n2) )
}
# compute total probability
# Compute p(D_1,D_2|I) and the normalized probabilities
cat("\nCompile results\n")
# tot = pCSk + pCSbark + pCbarSk + pCbarSbark = p(D_1,D_2|I)^2
tot <- pCSk + pCSbark + pCbarSk + pCbarSbark
# pCS = probability that the means & standard deviations are the same
pCS <- pCSk / tot
# pCSbar = probability that the means are the same & standard deviations are different
pCSbar <- pCSbark / tot
# pCbarS = probability that the means are different & standard deviations are the same
pCbarS <- pCbarSk / tot
# pCbarSbar = probability that the means& standard deviations are different
pCbarSbar <- pCbarSbark / tot
# pC = probability that the means are the same independent of whether the standard deviations are the same or different
pC <- pCS + pCSbar
# pCbar = probability that the means are different independent of whether the standard deviations are the same or different
pCbar <- pCbarS + pCbarSbar
# Odds in favor of different means
oddCbarC <- pCbar / pC
# pS = probability that the standard deviations are the same independent of whether the means are the same or different
pS <- pCS + pCbarS
# pSbar = probability that the standard deviations are different independent of whether the means are the same or different
pSbar <- pCSbar + pCbarSbar
# Odds in favor of different standard deviations
oddSbarS <- pSbar / pS
# Odds in favor of different means and/or standard deviations
odddiff <- (1 - pCS) / pCS
# create output tables
# table/ dataframe with resulting probabilities
posterior.probs <- list(pCS,pCbarS,pCSbar,pCbarSbar,pC,pCbar,pS,pSbar,pCS,1-pCS)
names(posterior.probs) <- c("pCS","pCbarS","pCSbar","pCbarSbar","pC","pCbar","pS","pSbar","pCS","One-minus-pCS")
posterior.df <- data.frame("short" = c("C,S","Cbar,S","C,Sbar","Cbar,Sbar","C","Cbar","S","Sbar","C, S","Cbar, Sbar"),
"Hypothesis" = c(
"Same Mean, same Standard Deviation",
"Different Means, same Standard Deviation",
"Same Mean, different Standard Deviations",
"Different Means, different Standard Deviations",
"The Means are the same",
"The Means are different",
"The Standard Deviations are the same",
"The Standard Deviations are different",
"Same Means and Standard Deviations",
"One or Both are different"),
"Probability" = NA,
"exp(Probability)" = NA,
"sign" = NA,
check.names = FALSE)
# table/ dataframe with odds ratio results
OR.post <- list(oddCbarC,oddSbarS,odddiff, 1/oddCbarC,1/oddSbarS,1/odddiff)
names(OR.post) <- c("oddCbarC","oddSbarS","odddiff", "One-div-oddCbarC","One-div-oddSbarS","One-div-odddiff")
OR.res.df <- data.frame("Hypothesis" = c("The odds Ratio in favour of a difference (means)",
"The Odds Ratio in favour of a difference (standard deviations)",
"The Odds Ratio in favour of a difference (means and standard deviations)",
"The Odds Ratio in favour of the same (means)",
"The Odds Ratio in favour of the same (standard deviations)",
"The Odds Ratio in favour of the same (means and standard deviations)"),
"Odds Ratio" = NA,
"exp(Odds Ratio)" = NA,
"sign" = NA,
check.names = FALSE)
if(method=="normal")
{
posterior.df[,"Probability"] <- unlist(posterior.probs)
OR.res.df["Odds Ratio"] <- unlist(OR.post)
tot.out <- as.numeric(tot)
} else if(method=="brob")
{
# posterior probs
posterior.df[,"Probability"] <- signif(unlist(lapply(posterior.probs,as.numeric)), digits=dig)
posterior.df[,"exp(Probability)"] <- paste("exp(",signif(unlist(lapply(posterior.probs, function(x) x@x)),digits=dig),")",sep="")
posterior.df[,"sign"] <- unlist(lapply(posterior.probs, function(x) x@positive))
nullID <- which(posterior.df[,"Probability"] == 0)
posterior.df[,"sign"][nullID] <- ""
# ORs
OR.res.df[,"Odds Ratio"] <- signif(unlist(lapply(OR.post,as.numeric)), digits=dig)
OR.res.df[,"exp(Odds Ratio)"] <- paste("exp(",signif(unlist(lapply(OR.post, function(x) x@x)),digits=dig),")",sep="")
OR.res.df[,"sign"] <- unlist(lapply(OR.post, function(x) x@positive))
tot.out <- paste("exp(",signif(tot@x,digits=dig+2),") [sign=",tot@positive,"]",sep="")
}
#results
inputvalues[["low"]] <- low
inputvalues[["high"]] <- high
inputvalues[["ndelta"]] <- ndelta
inputvalues[["n1"]] <- n1
inputvalues[["n2"]] <- n2
inputvalues[["n"]] <- n
inputvalues[["d1bar"]] <- d1bar
inputvalues[["d2bar"]] <- d2bar
inputvalues[["Rc"]] <- Rc
inputvalues[["Rsigma"]] <- Rsigma
inputvalues[["dsqubar"]] <- dsqubar
inputvalues[["dbar"]] <- dbar
inputvalues[["sigma.high"]] <- sigma.high
inputvalues[["sigma.low"]] <- sigma.low
inputvalues[["d1squbar"]] <- d1squbar
inputvalues[["d2squbar"]] <- d2squbar
inputvalues[["d1std"]] <- d1std
inputvalues[["d2std"]] <- d2std
inputvalues[["nr"]] <- nr
res <- list(invtyp = invtyp,
inputvalues = inputvalues,
prior.df = prior.df,
desc.df = desc.df,
tot = tot,
tot.out = tot.out,
posterior.probs = posterior.probs,
posterior.df = posterior.df,
OR.post = OR.post,
OR.res.df = OR.res.df,
method = method
)
# print tables
if(print.res)
{
DiM.print.pg(res)
}
return(res)
}
# call:
# DiM.pg(invtyp="ums", inputvalues=res.SIB.NRFtotal, print.res=TRUE, method="brob")
################################################################################
################################################################################
DiM.extract.limits <- function(DiM.res, scaleL=NULL, scaleH=NULL, low=NULL, high=NULL, sigma.high=NULL, sigma.low=NULL, change=FALSE, dig=4, fac.brob=1)
{
#prior
if(is.null(low)) low <- DiM.res[["inputvalues"]]$low
if(is.null(high)) high <- DiM.res[["inputvalues"]]$high
ndelta <- DiM.res[["inputvalues"]]$ndelta
nr <- DiM.res[["inputvalues"]]$nr
if(is.null(sigma.low)) sigma.low <- DiM.res[["inputvalues"]]$sigma.low
if(is.null(sigma.high)) sigma.high <- DiM.res[["inputvalues"]]$sigma.high
if(is.null(scaleL)) scaleL <- DiM.res[["inputvalues"]]$scaleL
if(is.null(scaleH)) scaleH <- DiM.res[["inputvalues"]]$scaleH
if(length(c(scaleL,scaleH, low, high)) != 4)
{
cat("\nAny of ['scaleL', 'scaleH', 'low', 'high'] is NULL. Stopping.\n\n")
stop()
}
#delta
delta.low <- low - high
delta.high <- high - low
delta.delta <- (delta.high - delta.low) / (ndelta - 1)
delta.sek <- seq(delta.low,delta.high,delta.delta / fac.brob)
delta.sek.l <- length(delta.sek)
#ratio
# upper and lower bounds of the plot can be tweaked with scaleL and scaleH.
# use as input values to allow to change them:
d1std <- DiM.res[["inputvalues"]]$d1std
d2std <- DiM.res[["inputvalues"]]$d2std
r.low <- d1std / (scaleL * d2std)
r.high <- scaleH * d1std / d2std
r.delta <- (r.high - r.low) / (nr - 1)
r.sek <- seq(r.low,r.high,r.delta / fac.brob)
r.sek.l <- length(r.sek)
#output
res <- structure(list(low=low,
high=high,
ndelta=ndelta,
sigma.low=sigma.low,
sigma.high=sigma.high,
delta.low=delta.low,
delta.high=delta.high,
delta.delta=delta.delta,
delta.sek.l=delta.sek.l,
scaleL=scaleL,
scaleH=scaleH,
d1std=d1std,
d2std=d2std,
r.low=r.low,
r.high=r.high,
r.delta=r.delta,
r.sek.l=r.sek.l,
fac.brob=fac.brob
))
cat("\n\n")
cat(paste(format(names(res), width = 15L, justify = "right"),
format(res, digits = dig), sep = " = "), sep = "\n")
cat("\n\n")
#output
#change original values
if(change)
{
DiM.res[["inputvalues"]]$low <- low
DiM.res[["inputvalues"]]$high <- high
DiM.res[["inputvalues"]]$ndelta <- ndelta
DiM.res[["inputvalues"]]$sigma.low <- sigma.low
DiM.res[["inputvalues"]]$sigma.high <- sigma.high
DiM.res[["inputvalues"]]$delta.low <- delta.low
DiM.res[["inputvalues"]]$delta.high <- delta.high
DiM.res[["inputvalues"]]$delta.delta <- delta.delta
DiM.res[["inputvalues"]]$scaleL <- scaleL
DiM.res[["inputvalues"]]$scaleH <- scaleH
DiM.res[["inputvalues"]]$d1std <- d1std
DiM.res[["inputvalues"]]$d2std <- d2std
DiM.res[["inputvalues"]]$r.low <- r.low
DiM.res[["inputvalues"]]$r.high <- r.high
DiM.res[["inputvalues"]]$r.delta <- r.delta
DiM.res[["inputvalues"]]$fac.brob <- fac.brob
return(DiM.res)
}
}
# call:
# DiM.extract.limits(DiM.res, change=FALSE)
# Dim.res.newlimits <- DiM.extract.limits(DiM.res, scaleL=30, scaleH=10, change=TRUE)
# check:
# DiM.extract.limits(DiM.res.newlimits, change=FALSE)
################################################################################
################################################################################
DiM.plot.calc.pg <- function(DiM.res, method="normal", fac.brob=1, cMasses=c(0.89,0.96), low=NULL, high=NULL, sigma.low=NULL, sigma.high=NULL, scaleL=NULL, scaleH=NULL, Nsteps=100)
{
cat("\n\nCalculate graphical output for the various probabilities...\n")
library(progress)
# for large numbers
if(method=="brob")
{
library(Brobdingnag)
fac.brob <- 0.1
cat("\nUsing log() and package 'Brobdingnag'\nAll results are expressed as log(RESULT)\n")
}
# error function
errf <- function(ERR) return( 2*pnorm(ERR*sqrt(2))-1 )
# BROB
errf.brob <- function(ERR) return( as.brob( 2*pnorm(ERR*sqrt(2))-1 ))
# map values
if(is.null(low)) low <- DiM.res[["inputvalues"]]$low
if(is.null(high)) high <- DiM.res[["inputvalues"]]$high
ndelta <- DiM.res[["inputvalues"]]$ndelta
n1 <- DiM.res[["inputvalues"]]$n1
n2 <- DiM.res[["inputvalues"]]$n2
n <- DiM.res[["inputvalues"]]$n
d1bar <- DiM.res[["inputvalues"]]$d1bar
d2bar <- DiM.res[["inputvalues"]]$d2bar
Rc <- DiM.res[["inputvalues"]]$Rc
Rsigma <- DiM.res[["inputvalues"]]$Rsigma
dsqubar <- DiM.res[["inputvalues"]]$dsqubar
dbar <- DiM.res[["inputvalues"]]$dbar
if(is.null(sigma.low)) sigma.low <- DiM.res[["inputvalues"]]$sigma.low
if(is.null(sigma.high)) sigma.high <- DiM.res[["inputvalues"]]$sigma.high
d1squbar <- DiM.res[["inputvalues"]]$d1squbar
d2squbar <- DiM.res[["inputvalues"]]$d2squbar
d1std <- DiM.res[["inputvalues"]]$d1std
d2std <- DiM.res[["inputvalues"]]$d2std
nr <- DiM.res[["inputvalues"]]$nr
if(is.null(scaleL)) scaleL <- DiM.res[["inputvalues"]]$scaleL
if(is.null(scaleH)) scaleH <- DiM.res[["inputvalues"]]$scaleH
if(is.null(fac.brob)) fac.brob <- DiM.res[["inputvalues"]]$fac.brob
# range mean: Rc <- high - low
# range sd: Rsigma <- sigma.high / sigma.low
################################################################################
# p(delta|S,D_1,D_2,I) problem
# plot The difference in the means (delta) if sd is the same
# create sequence for values to calculate the posterior probability density(ies)
delta.low <- low - high
delta.high <- high - low
delta.delta <- (delta.high - delta.low) / (ndelta - 1)
beta.low <- 2 * low
beta.high <- 2 * high
delta.N <- (n1 - n2) / n
b <- (n1 * d1bar - n2 * d2bar) / (2 * n)
# functions
V <- function(delta, BETA)
{
(n/2) * (dsqubar - (2 * delta * b) - (BETA * dbar) + (BETA^2/4) + (delta^2/4) + (delta * BETA * delta.N/ 2) )
}
fndbV <- function(BETA)
{
gamma(n/2) / (8 * Rc^2 * log(Rsigma)) * V(delta=delta,BETA=BETA)^(-n/2) *
( pgamma(V(delta=delta,BETA=BETA)/sigma.high^2,n/2) - pgamma(V(delta=delta,BETA=BETA)/sigma.low^2,n/2))
}
# BROB
V.brob <- function(delta, BETA)
{
as.brob( (n/2) * (dsqubar - (2 * delta * b) - (BETA * dbar) + (BETA^2/4) + (delta^2/4) + (delta * BETA * delta.N/ 2) ) )
}
fndbV.brob <- function(BETA)
{
brob( lgamma(n/2) ) /
as.brob( (8 * Rc^2 * log(Rsigma)) ) *
as.brob( V(delta=delta,BETA=BETA))^(-n/2) *
(
brob( pgamma(V(delta=delta,BETA=BETA) / sigma.high^2,n/2,log.p=TRUE,lower=FALSE)) -
brob( pgamma(V(delta=delta,BETA=BETA) / sigma.low^2,n/2,log.p=TRUE,lower=FALSE))
)
}
# integrate for each element of delta.sek
# fac.brob ~ 10% of non-brob (fac = 1) -> calculation time!
cat("Calculate pdel.SD1D2I and pdelA...\n")
delta.sek <- seq(delta.low,delta.high,delta.delta / fac.brob)
delta.sek.l <- length(delta.sek)
pdel.SD1D2I <- vector(mode="list",length=delta.sek.l)
pdel.SD1D2I.sum <- 0
pb <- progress_bar$new(total = delta.sek.l)
for(i in 1:delta.sek.l)
{
#print(i)
pb$tick()
delta <- delta.sek[i]
if(method=="normal")
{
pdel.SD1D2I[[i]] <- integrate(fndbV, lower=beta.low, upper=beta.high)$value
} else if(method=="brob")
{
pdel.SD1D2I[[i]] <- simpsonrule.nlb(fx=fndbV.brob, lower=beta.low, upper=beta.high, method="brob", Nsteps=Nsteps)
}
pdel.SD1D2I.sum <- pdel.SD1D2I.sum + pdel.SD1D2I[[i]]
}
pdel.SD1D2I[[50]]
pdel.SD1D2I.sum
# compile
pdelA <- lapply(pdel.SD1D2I, function(x) x/ (delta.delta * pdel.SD1D2I.sum))
#pdelA <- pdel.SD1D2I / (delta.delta * sum(pdel.SD1D2I))
# create dataframe with input and results
if(method=="normal")
{
pdelta.df <- data.frame(delta.sek = delta.sek,
pdel.SD1D2I = unlist(pdel.SD1D2I),
pdel.SD1D2I.sign = NA,
pdelA = unlist(pdelA),
pdelA.sign = NA
)
} else if(method=="brob")
{
pdelta.df <- data.frame(delta.sek = delta.sek,
pdel.SD1D2I = unlist(lapply(pdel.SD1D2I,function(x) x@x)),
pdel.SD1D2I.sign = unlist(lapply(pdel.SD1D2I,function(x) x@positive)),
pdelA = unlist(lapply(pdelA,function(x) x@x)),
pdelA.sign = unlist(lapply(pdelA,function(x) x@positive))
)
}
head(pdelta.df)
################################################################################
# p(delta|Sbar,D_1,D_2,I) problem
# plot the difference in means assuming sds are different.
# TODO tweak range for each plot!
#redundant, not necessary
#delta.low <- low - high
#delta.high <- high - low
#delta.delta <- (delta.high - delta.low) / (ndelta - 1)
#beta.low <- 2 * low
#beta.high <- 2 * high
w1 <- function(delta, BETA)
{
(n1/2) * (d1squbar - (BETA + delta) * d1bar + ((BETA + delta)^2/4) )
}
w2 <- function(delta, BETA)
{
(n2/2) * (d2squbar - (BETA - delta) * d2bar + ((BETA - delta)^2/4) )
}
fndbVbar <- function(BETA)
{
gamma(n1/2) * gamma(n2/2) / (16 * Rc^2 * log(Rsigma)^2) *
w1(delta=delta,BETA=BETA)^(-n1/2) * w2(delta=delta,BETA=BETA)^(-n2/2) *
( pgamma(w1(delta=delta,BETA=BETA)/sigma.high^2,n1/2) - pgamma(w1(delta=delta,BETA=BETA)/sigma.low^2,n1/2) ) *
( pgamma(w2(delta=delta,BETA=BETA)/sigma.high^2,n2/2) - pgamma(w2(delta=delta,BETA=BETA)/sigma.low^2,n2/2) )
}
# BROB
w1.brob <- function(delta, BETA)
{
as.brob( (n1/2) * (d1squbar - (BETA + delta) * d1bar + ((BETA + delta)^2/4) ) )
}
w2.brob <- function(delta, BETA)
{
as.brob( (n2/2) * (d2squbar - (BETA - delta) * d2bar + ((BETA - delta)^2/4) ) )
}
fndbVbar.brob <- function(BETA)
{
brob( lgamma(n1/2) ) * brob( lgamma(n2/2) ) / as.brob( (16 * Rc^2 * log(Rsigma)^2) ) *
as.brob( w1(delta=delta,BETA=BETA)^(-n1/2) ) *
as.brob( w2(delta=delta,BETA=BETA)^(-n2/2) ) *
( brob( pgamma(w1(delta=delta,BETA=BETA)/sigma.high^2,n1/2, log.p=TRUE,lower=FALSE) ) -
brob( pgamma(w1(delta=delta,BETA=BETA)/sigma.low^2,n1/2, log.p=TRUE,lower=FALSE) ) ) *
( brob( pgamma(w2(delta=delta,BETA=BETA)/sigma.high^2,n2/2, log.p=TRUE,lower=FALSE) ) -
brob( pgamma(w2(delta=delta,BETA=BETA)/sigma.low^2,n2/2, log.p=TRUE,lower=FALSE) ) )
}
# integrate for each element of delta.sek
# TODO: do it with sapply
cat("Calculate pdel.SbarD1D2I and pdelB...\n")
pdel.SbarD1D2I <- vector(mode="list",length=delta.sek.l)
pdel.SbarD1D2I.sum <- 0
pb <- progress_bar$new(total = delta.sek.l)
for(i in 1:delta.sek.l)
{
#print(i)
pb$tick()
delta <- delta.sek[i]
if(method=="normal")
{
pdel.SbarD1D2I[[i]] <- integrate(fndbVbar, lower=beta.low, upper=beta.high)$value
} else if(method=="brob")
{
pdel.SbarD1D2I[[i]] <- simpsonrule.nlb(fx=fndbVbar.brob, lower=beta.low, upper=beta.high, method="brob", Nsteps=Nsteps)
}
pdel.SbarD1D2I.sum <- pdel.SbarD1D2I.sum + pdel.SbarD1D2I[[i]]
}
pdel.SbarD1D2I[[50]]
pdel.SbarD1D2I.sum
#compile
#pdelB <- pdel.SbarD1D2I / (delta.delta * sum(pdel.SbarD1D2I))
pdelB <- lapply(pdel.SbarD1D2I, function(x) x/ (delta.delta * pdel.SbarD1D2I.sum))
# add to dataframe
if(method=="normal")
{
pdelta.df[,c("pdel.SbarD1D2I","pdel.SbarD1D2I.sign","pdelB","pdelB.sign")] <- data.frame(unlist(pdel.SbarD1D2I), NA, unlist(pdelB),NA)
} else if(method=="brob")
{
pdelta.df[,c("pdel.SbarD1D2I","pdel.SbarD1D2I.sign","pdelB","pdelB.sign")] <- data.frame(
unlist(lapply(pdel.SbarD1D2I,function(x) x@x)),
unlist(lapply(pdel.SbarD1D2I,function(x) x@positive)),
unlist(lapply(pdelB,function(x) x@x)),
unlist(lapply(pdelB,function(x) x@positive))
)
}
head(pdelta.df)
################################################################################
# p(delta|D_1,D_2,I) problem
# plot the difference in means independent of the sds are the same or not
# This is a weighted average of p(delta|S,D_1,D_2,I) and p(delta|Sbar,D_1,D_2,I)
# with weights are pS and pSbar ie. probs of sds are same/ different.
# It also calcs the CIs.
# to remember - s.a.:
# p(delta|S,D_1,D_2,I)
# xdel <- delta.sek #vector
# xda <- pdelta.df[,c("delta.sek","pdelA")] #table/ data.frame with 2 cols
#
# p(delta|Sbar,D_1,D_2,I)
# xdel <- delta.sek #vector
# xdB <- pdelta.df[,c("delta.sek","pdelB")] #table/ data.frame with 2 cols
# taken from results
pS <- DiM.res[["posterior.probs"]]$pS
pSbar <- DiM.res[["posterior.probs"]]$pSbar
#pdelave = average of pdelA and pdelB
cat("Calculate average of pdelA and pdelB...\n")
if(method=="normal")
{
pdelave <- (pS * pdelta.df[,"pdelA"] + pSbar * pdelta.df[,"pdelB"])
pdelta.df[,c("pdelave","pdelave.sign","pdelave.delta.delta","pdelave.delta.delta.sign")] <- data.frame(as.numeric(pdelave / sum(pdelave)),NA,as.numeric(pdelave / sum(pdelave))/delta.delta,NA)
} else if(method=="brob")
{
pS.pdelA <- lapply(pdelA, function(x) x*pS)
pSbar.pdelB <- lapply(pdelB, function(x) x*pSbar)
pS.pdelA.pSbar.pdelB <- sapply(seq_along(1:length(pS.pdelA)), function(x) pS.pdelA[[x]] + pSbar.pdelB[[x]] )
pS.pdelA.pSbar.pdelB.sum <- 0
for(i in 1:length(pS.pdelA.pSbar.pdelB)) pS.pdelA.pSbar.pdelB.sum <- pS.pdelA.pSbar.pdelB[[i]] + pS.pdelA.pSbar.pdelB.sum
pS.pdelA.pSbar.pdelB <- lapply(pS.pdelA.pSbar.pdelB, function(x) x/pS.pdelA.pSbar.pdelB.sum)