-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptII_quan_Bayes_case_wordcounts-PPC.r
1179 lines (942 loc) · 40 KB
/
ptII_quan_Bayes_case_wordcounts-PPC.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
### (C) 2005-2023 by Leo Guertler
### R-code supplement
### to the book
###
### "Subjektive Ansichten und objektive Betrachtungen"
###
### written by Gürtler & Huber (2023)
###
### All R-code is published under the GPL v3 license:
###
### https://www.gnu.org/licenses/gpl-3.0.en.html
###
### except for 'borrowed' code - see links and references.
### For this R-code the original license of the respective
### authors is valid.
###
### R-code published on
###
### https://osdn.net/projects/mixedmethod-rcode
### https://github.com/abcnorio/mixedmethod-rcode
# file:
# ptII_quan_Bayes_case_wordcounts-PPC.r
# location:
# chap. 6 [6.7.4.6]
# Forschungsbeispiel — Wortproduktion Humor
# load necessary libraries
library(lme4)
library(lattice)
library(bayesplot)
library(BEST)
# library(brms)
# library(ggplot2)
# load necessary helper functions
# from DBDA (Kruschke)
source("DBDA2E-utilities.R")
source("Jags-Ymet-XmetMulti-Mrobust.R")
source("ptall_generalfuncs.r")
source("ptII_quan_Bayes_case_wordcounts-PPC_helpfuncs.r")
# get data
diss <- read.csv("LG_school-words-raw.tab", header=TRUE, sep="\t")
str(diss)
head(diss)
tail(diss)
namen <- names(diss)
with(diss, cor(W.noSC,uW.noSC))
with(diss, cor(W.wSC,uW.wSC))
with(diss, xtabs(age ~ schooltype +sex))
par(mfrow=c(2,3))
with(diss, plot(W.noSC,uW.noSC, bty="n", col="violetred3", lty=1, lwd=1, type="p", pre.plot=grid()))
with(diss, lines(lowess(uW.noSC ~ W.noSC), col="blue", lwd=2, lty=2))
with(diss, plot(W.noSC,age, bty="n", col="violetred3", lty=1, lwd=1, type="p", pre.plot=grid()))
with(diss, boxplot(W.noSC ~ sex * schooltype, notch=TRUE, col="violetred3"))
with(diss, interaction.plot(schooltype, sex, W.noSC, bty="n", pre.plot=grid(), col=c("darkred","blue")))
with(diss, interaction.plot(sex, schooltype, W.noSC, bty="n", pre.plot=grid(), col=c("darkred","blue")))
pairs(diss[,c(2,3,10,11)], col="violetred3")
diss$SS <- paste(diss$sex,diss$schooltype,sep="")
summary(lm(W.noSC ~ age + schooltype * sex, data=diss))
summary(glm(W.noSC ~ age + schooltype * sex, data=diss, family=poisson()))
# library 'lme4'
summary(glmer(W.noSC ~ age + schooltype + (age|sex), data=diss, family=poisson()))
summary(glmer(W.noSC ~ age + (1|schooltype) + (1|sex), data=diss, family=poisson()))
# library 'lattice'
bwplot(W.noSC ~ schooltype + sex |age, data=diss)
bwplot(W.noSC ~ SS | age, data=diss)
xyplot(W.noSC ~ age, data=diss)
# using scripts from KRUSCHKE DBDA
# prepare data
diss$W.noSC <- as.integer(diss$W.noSC)
diss$W.noSC.log <- log(diss$W.noSC)
diss$age.log <- log(diss$age)
diss$SS <- factor(diss$SS)
diss$sex <- factor(diss$sex)
diss$schooltype <- factor(diss$schooltype)
diss$SSn <- as.numeric(factor(diss$SS)) #as.numeric(factor(paste(diss$sex,diss$schooltype,sep="")))
diss$sexn <- as.numeric(factor(diss$sex))
diss$stypen <- as.numeric(factor(diss$schooltype))
diss$age.cat <- cut(as.numeric(diss$age), breaks=c(13,16,19,25), include.lowest=TRUE, right=TRUE, labels=c("(14-16]","(17-19]","(20-25]"))
diss$age.cat1 <- cut(as.numeric(diss$age), breaks=c(13,19,25), include.lowest=TRUE, right=TRUE, labels=c("(14-19]","(20-25]"))
str(diss)
head(diss)
# tables
table(diss$age)
with(diss, ftable(sex,stype,age))
with(diss, ftable(sex,schooltype,age))
with(diss, ftable(sex,schooltype))
with(diss, ftable(sex,age))
with(diss, ftable(schooltype,age))
bwplot(W.noSC.log ~ sex | stype, data=diss)
bwplot(W.noSC.log ~ stype | sex, data=diss)
xyplot(W.noSC ~ age, data=diss)
xyplot(W.noSC ~ jitter(age), data=diss)
xyplot(W.noSC.log ~ jitter(age), data=diss)
xyplot(W.noSC.log ~ jitter(age.log), data=diss)
xyplot(jitter(W.noSC.log) ~ jitter(age.log), data=diss)
with(diss, ftable(stype,sex,age))
apply(with(diss, ftable(stype,sex,age)),1,sum)
# interaction plots
colos <- colorRampPalette(c("red","green","orange","blue"))
par(mfrow=c(3,3))
with(diss, interaction.plot(stype, sex, W.noSC, bty="n", pre.plot=grid(), col=colos(2)))
with(diss, interaction.plot(sex, stype, W.noSC, bty="n", pre.plot=grid(), col=colos(3)))
with(diss, interaction.plot(stype, sex, W.noSC.log, bty="n", pre.plot=grid(), col=colos(2)))
with(diss, interaction.plot(sex, stype, W.noSC.log, bty="n", pre.plot=grid(), col=colos(3)))
with(diss, interaction.plot(stype, sex, W.noSC.log, bty="n", pre.plot=grid(), col=colos(3)))
with(diss, interaction.plot(age.cat, stype, W.noSC.log, bty="n", pre.plot=grid(), col=colos(3)))
with(diss, interaction.plot(age.cat, sex, W.noSC.log, bty="n", pre.plot=grid(), col=colos(2)))
# posterior predictive check with R after JAGS
# constants
fileNameRoot <- "Diss-"
numSavedSteps <- 1e4
thinSteps <- 50
numSavedSteps <- 1e3
nChains <- 3
# simple model
# response
yName1 <- "W.noSC.log"
# predictors
xName1 <- c("age.log","sexn","stypen")
diss1 <- diss[,c(yName1,xName1)]
naid <- which(is.na(diss1), arr.ind=TRUE)[,1]
diss.nona <- diss1[-naid,]
head(diss.nona)
# run model
mcmc.dm1 <- genMCMC( data=diss.nona, xName=xName1, yName=yName1, numSavedSteps=numSavedSteps, thinSteps=thinSteps, saveName=fileNameRoot, nChains=nChains)
str(mcmc.dm1)
# more complex model
diss.model <- cbind(W.noSC.log=diss.nona$W.noSC.log, with(diss, model.matrix(W.noSC.log ~ age.log + age.cat + stype + sex))[,-1] )
head(diss.model)
yName <- "W.noSC.log"
xName <- colnames(diss.model)[-1]
xName
# run model
mcmc.dm2 <- genMCMC(data=diss.model, xName=xName, yName=yName, numSavedSteps=numSavedSteps, thinSteps=thinSteps, saveName=fileNameRoot, nChains=nChains)
str(mcmc.dm2)
# analysis
# warning... creates a lot of plots
# this won't work without direct X11 access below Linux (like e.g. rstudioserver)
# no connetion to X11 display
mcmc.diag.kruschke(model=mcmc.dm1, dats=diss.nona, xName=xName1, yName=yName1)
mcmc.diag.kruschke(model=mcmc.dm2, dats=diss.model, xName=xName, yName=yName)
mcmc.diag.kruschke(model=mcmc.dm2, dats=diss.model, xName=xName, yName=yName, PLOTmult=TRUE)
mcmc.diag.kruschke(model=mcmc.dm2, dats=diss.model, xName=xName, yName=yName, PLOThist=TRUE)
#
mcmc.diag.kruschke(model=mcmc.dm2, dats=diss.model, xName=xName1, yName=yName)
# extract infos
mcmc.red <- as.mcmc(lapply(mcmc.dm2, function(i) i[,c(1:8,17)]))
str(mcmc.red)
head(mcmc.red)
coda:::plot.mcmc.list(mcmc.dm2,ask=TRUE)
gelman.plot(mcmc.red)
# library(bayesplot)
color_scheme_set("pink")
mcmc_parcoord(mcmc.red, pars=colnames(mcmc.red[[1]]))
mcmc_parcoord(mcmc.red, pars=colnames(mcmc.red[[1]])[-c(9)])
# https://mc-stan.org/bayesplot/reference/MCMC-parcoord.html
color_scheme_set("brightblue")
# scale before
mcmc_parcoord(mcmc.red, pars=colnames(mcmc.red[[1]]), transform = function(x) {(x - mean(x)) / sd(x)})
mcmc_parcoord(mcmc.red, pars=colnames(mcmc.red[[1]])[-c(9)], transform = function(x) {(x - mean(x)) / sd(x)})
#
mcmc_parcoord(mcmc.red, pars=colnames(mcmc.red[[1]])[-c(9)], alpha=0.05)
# just sigma and nu
mcmc_parcoord(mcmc.red, pars=colnames(mcmc.red[[1]])[c(8,9)])
mcmc_pairs(mcmc.red)
color_scheme_set("darkgray")
# selected beta[1] vs. nu
mcmc_scatter(mcmc.red, pars=c("beta[1]","nu"), transform=list(nu="log"))
color_scheme_set("mix-brightblue-gray")
# traceplot MCMCs
mcmc_trace(mcmc.red)
# autocorrelation MCMCs
mcmc_acf(mcmc.red, lags=10)
# https://cran.r-project.org/web/packages/bayesplot/vignettes/visual-mcmc-diagnostics.html
# without sigma and nu
# https://arxiv.org/abs/1709.01449
# https://arxiv.org/pdf/1709.01449.pdf p.6
# Visualization in Bayesian workflow Jonah Gabry, Daniel Simpson, Aki Vehtari, Michael Betancourt, Andrew Gelman
# not run (outtake of full process)
# PPC posterior predictive distribution
dim(diss.model)
mcmc.mat.dm2 <- as.matrix(mcmc.dm2)
d.mcmc.mat.dm2 <- dim(mcmc.mat.dm2)
head(diss.model)
head(mcmc.mat.dm2[,grep("^beta",colnames(mcmc.mat.dm2))])
beta.nams <- grep("^beta",colnames(mcmc.mat.dm2),value=TRUE)
beta.id <- grep("^beta",colnames(mcmc.mat.dm2))
beta.nams
beta.id
# create a random sample (full cases!) from real values
# sample with replacement = bootstrap
seed <- 1432
set.seed(seed)
head(diss.model)
diss.model.d <- dim(diss.model)
diss.model.d #360 7 >>> N=360 persons (rows), k=7 variables (cols)
samp.ids <- sample(1:diss.model.d[1], replace=TRUE)
samp.mat <- diss.model[samp.ids,]
d.samp.mat <- dim(samp.mat)
d.samp.mat
# create y_pred values from posterior values for each of the sample elements
mat.ypred <- matrix(data=NA, ncol=d.samp.mat[1], nrow=d.mcmc.mat.dm2[1])
# cols=360 Persons
# rows=1002 mcmc (=3*334)
# ie. for each person investigated from the sample a full mcmc chain
# we use only the betas
attr(mcmc.mat.dm2,"dimnames")
dim(mat.ypred)
for(i in 1:d.samp.mat[1])
{
mat.ypred[,i] <- mcmc.mat.dm2[,beta.id] %*% c(1,t(samp.mat[i,-c(1)]))
}
dim(mat.ypred)
head(mat.ypred)
tail(mat.ypred)
muPred <- apply(mat.ypred,1,mean)
mean(muPred)
sd(muPred)
head(muPred)
length(muPred)
hist(muPred, prob=TRUE)
# Y_rep is a function of mu_pred + sigma + noise-due-to-prediction-here-based-on-t-dist-and-nu(=df-of-t)
Y_rep <- muPred + mcmc.mat.dm2[,"sigma"] * rt(nrow(mcmc.mat.dm2),df=mcmc.mat.dm2[,"nu"])
# plot
hist(Y_rep, prob=TRUE, pre.plot=grid(), main="Y_rep")
lines(density(diss.model[,"W.noSC.log"]), col="darkred", lwd=2)
lines(density(Y_rep), lwd=2, col="blue")
legend("topright", c("empirical","Y_rep"), col=c("darkred","blue"), lwd=2, bty="n")
# create random sample from random draws from real values, but not full cases
anzbetas <- diss.model.d[2]-1 #-1 because one is the response
anzbetas
mat.ypred.d <- dim(mat.ypred)
mat.ypred.d
for(j in 1:mat.ypred.d[2])
{
case.ids <- sample(1:dim(diss.model)[1], anzbetas, replace=TRUE)
samp.mat[j,2:7] <- diag(diss.model[,-c(1)][case.ids,1:6]) #1:6? #not response
}
mat.ypred <- matrix(data=NA, ncol=d.samp.mat[1], nrow=d.mcmc.mat.dm2[1])
dim(mat.ypred)
dim(samp.mat)
for(i in 1:d.samp.mat[1])
{
mat.ypred[,i] <- mcmc.mat.dm2[,beta.id] %*% c(1,t(samp.mat[i,2:7])) #not response
}
dim(mat.ypred)
muPred <- apply(mat.ypred,1,mean)
mean(muPred)
sd(muPred)
head(muPred)
length(muPred)
hist(muPred, prob=TRUE)
hist(Y_rep, prob=TRUE)
lines(density(diss.model[,"W.noSC.log"]), lwd=2)
lines(density(Y_rep), lwd=2)
# summary statistics
lapply(list(muPred=muPred,Y_rep=Y_rep), function(x) c(summary(x),sd=sd(x)))
# means
mean(muPred)
mean(Y_rep)
# does not make sense
mean(Y_rep == muPred) # = zero
# difference
mean(Y_rep-muPred)
# effect size
mean(Y_rep-muPred)/sd(Y_rep)
# Bayesian p-value
1-mean(Y_rep > muPred) # compare predict vs. predictive
1-mean(Y_rep > mean(diss.model[,"W.noSC.log"])) # compare predictive vs. empirical
# end of not run (outtake of full process)
# full process to perform a PPC based on full cases or category of cases (those sampled)
# Bootstrap approach
# http://doingbayesiandataanalysis.blogspot.com/2017/06/posterior-distribution-of-predictions.html
# convert to matrix and extract betas
# posterior
mcmc.mat.dm2 <- as.matrix(mcmc.dm2)
str(mcmc.mat.dm2)
summary(mcmc.mat.dm2)
d.mcmc.mat.dm2 <- dim(mcmc.mat.dm2)
colnames(mcmc.mat.dm2)
# betas
beta.nams <- grep("^beta",colnames(mcmc.mat.dm2),value=TRUE)
beta.id <- grep("^beta",colnames(mcmc.mat.dm2))
beta.nams
beta.id
set.seed(1823)
anzsim.reps <- 100
mat.ypred <- matrix(data=NA, ncol=dim(diss.model)[1], nrow=d.mcmc.mat.dm2[1])
Y_rep.mat <- matrix(data=NA, nrow=dim(mat.ypred)[1], ncol=anzsim.reps)
mat.ypred.d <- dim(mat.ypred)
Y_rep.mat.d <- dim(Y_rep.mat)
diss.model.d <- dim(diss.model)
mat.ypred.d
Y_rep.mat.d
diss.model.d
fullcase <- FALSE
fullcase <- TRUE
jitt <- FALSE
colnames(diss.model)
length(colnames(diss.model))
varsofinterest <- c(2,3,4,5,6,7) # c(1:6) #without response <<< cols of original table with data
varsofinterest
anzbetas <- diss.model.d[2]-1 #-1 because one is the response
anzbetas
# simulation
for(reps in 1:anzsim.reps)
{
print(reps)
if(fullcase)
{
# sample full cases
samp.ids <- sample(1:dim(diss.model)[1], replace=TRUE)
samp.mat <- diss.model[samp.ids,varsofinterest]
d.samp.mat <- dim(samp.mat)
d.samp.mat
} else if(!fullcase)
{
# create random sample from random draws from real values, but not full cases
# not full cases, but categories ie. values
for(j in 1:mat.ypred.d[2])
{
case.ids <- sample(1:diss.model.d[1], anzbetas, replace=TRUE)
samp.mat[j,] <- diag(diss.model[case.ids,varsofinterest])
# for(i in 1:length(case.ids)) samp.mat[j,i] <- diss.model[case.ids[i],i]
}
} else stop("STOP - fullcase variable not defined")
for(i in 1:d.samp.mat[1])
{
mat.ypred[,i] <- mcmc.mat.dm2[,beta.id] %*% c(1,t(samp.mat[i,]))
}
# mu_predict
muPred <- apply(mat.ypred,1,mean)
# Y=response_predict_rep
Y_rep.mat[,reps] <- muPred + mcmc.mat.dm2[,"sigma"] * rt(nrow(mcmc.mat.dm2),df=mcmc.mat.dm2[,"nu"])
# muPred.mat[,reps] <- apply(mat.ypred,1,mean)
}
# descriptive stats of Y_rep.mat
dim(Y_rep.mat)
# stats for each rep
Y_rep.mat.desc <- t(apply(Y_rep.mat,2, function(x) c(summary(x),SD=sd(x),VAR=var(x),fivenum2(x)[c(2,4)])))
# stats over all reps
Y_rep.mat.desc.sum <- t(apply(Y_rep.mat.desc,2, function(x) c(Min=min(x),Median=median(x),Mean=mean(x),Max=max(x))))
ratio <- Y_rep.mat.desc.sum[,"Max"]/Y_rep.mat.desc.sum[,"Min"]
Y_rep.mat.desc.sum <- data.frame(Y_rep.mat.desc.sum,"Ratio|Max/Min"=ratio, check.names=FALSE)
Y_rep.mat.desc.sum
# plot PPC
dens.orig.resp <- density(diss.model[,"W.noSC.log"])
fac <- 1.15
ylim <- c(0,max(density(Y_rep.mat[,1])$y)*fac)
par(oma=c(2,1,1,1), "cex.axis"=1, bty="l")
# hist(diss.model[,"W.noSC.log"], ylim=ylim, prob=TRUE, pre.plot=grid(), col="violetred3", border="white", main="", ylab="Density", xlab=expression(paste(Y^rep)), cex.lab=1.2)
hist(Y_rep.mat, ylim=ylim, prob=TRUE, pre.plot=grid(), col="orange", border="white", main="", ylab="Density", xlab=expression(paste(Y^rep)), cex.lab=1.2)
nlines <- 30
Y_rep.mat.d <- dim(Y_rep.mat)
chooses <- sample(Y_rep.mat.d[2], nlines, replace=FALSE)
apply(Y_rep.mat[,chooses],2,function(x) lines(density(x),col="violetred4"))
lines(dens.orig.resp, col="green", lwd=4)
mtext("Posterior Predictive Check (PPC)", outer=TRUE, line=-2, cex=1.5, side=3)
par(fig=c(0,1,0,1), oma=c(1,0,0,0), mar=c(0,0,0,0), new=TRUE)
plot(1, type="n", bty="n", xaxt="n", yaxt="n")
legend("bottom", legend=c("empirical","ppc"), lty=1, lwd=2, xpd=TRUE, horiz=TRUE, col=c("green","violetred4"), bty="n", cex=.9)
# end of full process
# https://m-clark.github.io/bayesian-basics/model-exploration.html#model-checking
summaryInfo <- smryMCMC2( mcmc.dm1 )
summaryInfo
anzsim.reps
varsofinterest
colnames(diss.model)[varsofinterest]
colnames(diss.model)[-varsofinterest]
# how to multiply betas and variables of interest
varsofinterest # not response (= 1)
str(diss.model)
model.pred <- mcmc.mat.dm2[,beta.id] %*% t( data.frame(1,diss.model[,varsofinterest]))
str(model.pred)
dim(model.pred)
MW <- apply(model.pred,1,mean)
SD <- apply(model.pred,1,sd)
head(MW)
length(MW)
# MW <- apply( mcmc.mat.dm2[,beta.id] %*% t( data.frame(1,head(diss.model[,-c(1)])) ), 1, mean)
# SD <- apply( mcmc.mat.dm2[,beta.id] %*% t( data.frame(1,head(diss.model[,-c(1)])) ), 1, sd)
# Y_rep.mat <- sapply(1:anzsim.reps, function(s) rnorm(n=dim(diss.nona)[1], mean=MW[i], sd=summaryInfo["sigma","Mean"]))
diss.model.d
anzsim.reps
seed <- 0987
set.seed(seed)
# Y_rep.mat.1 <- sapply(1:anzsim.reps, function(s) rnorm(n=diss.model.d[1], mean=MW[s], sd=SD[s]))
Y_rep.mat.1 <- sapply(1:anzsim.reps, function(s) rnorm(n=d.mcmc.mat.dm2[1], mean=MW[s], sd=SD[s]))
dim(Y_rep.mat.1)
Y_rep.1 <- apply(Y_rep.mat.1,1,mean) + mcmc.mat.dm2[,"sigma"] * rt(nrow(mcmc.mat.dm2),df=mcmc.mat.dm2[,"nu"])
# see above
# Y_rep.mat[,reps] <- muPred + mcmc.mat.dm2[,"sigma"] * rt(nrow(mcmc.mat.dm2),df=mcmc.mat.dm2[,"nu"])
# mat.ypred[,i] <- mcmc.mat.dm2[,beta.id] %*% c(1,t(samp.mat[i,]))# 1002 17 #360 6 -> 1002 360
1-mean(Y_rep.1 > mean(diss.model[,"W.noSC.log"]))
# plot Y_rep
compval <- mean(diss.model[,"W.noSC.log"])
BEST:::plotPost(Y_rep.1, credMass=0.87, compVal=compval, ROPE=c(4.2,5.5), showMode=TRUE, xlab=expression(paste(Y^rep)))
# plot muPred = Y
abline(v=mean(Y_rep.1), col="orange", lwd=3)
abline(v=compval, col="green", lwd=3)
mean(Y_rep.1)
mean(diss.model[,"W.noSC.log"])
head(Y_rep.mat.1)
tail(Y_rep.mat.1)
# plot
# library(grDevices)
colo <- adjustcolor("blue", alpha.f=0.1)
fac <- 1.35
i <- 50 #arbitrary, see
dim(Y_rep.mat.1)
dxy <- density(Y_rep.mat.1[,i])
ylim <- c(0,max(dxy$y)*fac)
hist(Y_rep.mat.1, prob=TRUE, ylim=ylim, pre.plot=grid(), border="white", col="skyblue", xlab="Y_rep", breaks=25)
# Y_reps
for(i in 1:anzsim.reps)
{
dxy <- density(Y_rep.mat.1[,i])
if(jitt) dxy$y <- jitter(dxy$y)
lines(dxy$x, dxy$y, lwd=1, col=colo)
}
# empirical
dens.orig.resp # = density(diss.model[,"W.noSC.log"])
lines(dens.orig.resp, lwd=2, col="red")
legend("topright", c("empirical","Y_rep"), col=c("red",colo), lwd=2, bty="n")
# Bayesian p-value
1-mean(Y_rep.mat[,1] > mean(diss.model[,"W.noSC.log"]))
pvals <- apply(Y_rep.mat.1, 2, function(i) 1-mean(i > mean(diss.model[,"W.noSC.log"])))
pvals
median(pvals)
fivenum.wn(pvals)
# plot adjusted
fac <- 1.2
hist(Y_rep.mat.1, ylim=c(0,max(density(Y_rep.mat.1)$y)*fac), prob=TRUE, pre.plot=grid(), border="white", col="skyblue", xlab="Y_rep", breaks=30)
# Y_reps
dxymax <- 0
for(i in 1:anzsim.reps)
{
dxy <- density(Y_rep.mat.1[,i])
if(jitt) dxy$y <- jitter(dxy$y)
lines(dxy$x, dxy$y, lwd=1, col=colo)
dxymax.temp <- max(dxy$y)
if(dxymax < dxymax.temp) dxymax <- dxymax.temp
}
# empirical
lines(dens.orig.resp, lwd=2, col="red")
# adjusted empirical
fac.dxy <- dxymax/max(dens.orig.resp$y)
fac.dxy
lines(dens.orig.resp$x, dens.orig.resp$y*fac.dxy, lwd=1, col="darkred", lty=2)
# muPred for single cases
# muPred <- mcmc.mat.dm2[,beta.id] %*% matrix(rep(1,length(beta.id)),ncol=1)
# selected case
head(diss.model)
dm.nams <- colnames(diss.model)[varsofinterest]
dm.nams
# 15 years, associated age category, stypeG=1, stypeR=0, sexw=1
mat.case1 <- matrix(data=c(1,log(15),1,0,1,0,1), ncol=1, dimnames=list(c("Intercept",dm.nams)))
t(mat.case1)
muPred.s <- mcmc.mat.dm2[,beta.id] %*% mat.case1
head(muPred.s)
tail(muPred.s)
# different case
mat.case1 <- matrix(data=c(1,log(17),1,0,0,1,0), ncol=1, dimnames=list(c("Intercept",dm.nams)))
# random sample n=1
muPred.s.rn <- mcmc.mat.dm2[,beta.id] %*% c(1,t(diss.model[sample(1:d.samp.mat[1],1),-c(1)]))
# calculate prediction based on predictors + sigma + rt noise
set.seed(1823)
Y_rep.s <- muPred.s.rn + mcmc.mat.dm2[,"sigma"] * rt(nrow(mcmc.mat.dm2),df=mcmc.mat.dm2[,"nu"])
length(Y_rep.s)
head(Y_rep.s)
mean(muPred.s)
mean(Y_rep.s)
mean(muPred.s.rn)
mean(Y_rep.s == muPred.s.rn) #zero
mean(Y_rep.s-muPred.s.rn)
mean(Y_rep.s-muPred.s.rn)/sd(Y_rep.s)
# Bayesian p-value
1-mean(Y_rep > muPred) # compare predict vs. predictive
1-mean(Y_rep > mean(diss.nona[,"W.noSC.log"])) # compare predictive vs. empirical
# plot Y_rep
compval # mean(diss.model[,"W.noSC.log"])
BEST:::plotPost(Y_rep.s, credMass=0.87, compVal=compval, ROPE=c(4.5,5.2), showMode=TRUE)
# plot muPred = Y
mean(Y_rep.s)
mean(muPred.s.rn)
mean(diss[,"W.noSC.log"])
abline(v=mean(Y_rep.s), col="orange", lwd=2)
abline(v=mean(muPred.s.rn), col="violetred3", lwd=2)
abline(v=mean(diss[,"W.noSC.log"]), col="darkred", lwd=2)
# summary
summary(as.mcmc(muPred.s.rn))
summary(as.mcmc(Y_rep.s))
# posterior predictive check
# calculate p-value
mean(Y_rep.s > muPred.s.rn)
mean(Y_rep.s > muPred.s.rn)/(1-mean(Y_rep.s > muPred))
mean(Y_rep.s - muPred.s.rn > 1)
mean(Y_rep.s - muPred.s.rn > 0.1)
mean(Y_rep.s - muPred.s.rn > 0.01)
par(mfrow=c(2,1))
BEST:::plotPost(muPred.s.rn, credMass=0.87, showMode=TRUE, compVal=compval, ROPE=c(5.4,5.5), xlab=expression(paste(mu[pred])), ylab="Density")
BEST:::plotPost(Y_rep.s, credMass=0.87, showMode=TRUE, compVal=compval, ROPE=c(4.9,5.5), xlab=expression(paste(Y^rep)), ylab="Density")
# exercise KRUSCHKE
# Kruschke Code
# Exercise 18.3 (p. 513)
# http://doingbayesiandataanalysis.blogspot.com/2011/04/anova-with-non-homogeneous-variances.html
# Question: Is it statistically acceptable to run the non-homogeneous ANOVA on data that follow non-normal distributions?
# If so, how do you change the code to run such tests? Specifically, I am interested in analyzing data that are gamma distributed.
# Answer: Yes, it's easy. In the code ANOVAonewayNonhomogvarJagSTZ.R, change this
for ( i in 1:Ntotal ) {
y[i] ~ dnorm( mu[i] , tau[x[i]] )
mu[i] <- a0 + a[x[i]]
}
# to this
for ( i in 1:Ntotal ) {
y[i] ~ dgamma( sY[i] , rY[i] )
sY[i] <- pow(mu[i],2) * tau[x[i]]
rY[i] <- mu[i] * tau[x[i]]
mu[i] <- max( a0 + a[x[i]] , 0.0001 )
}
# Also, do NOT standardize the data, because the data must be non-negative to come from a gamma distribution.
# Therefore, in the dataList, use the original data y, not standardized data. And, after JAGS/BUGS has generated an MCMC chain,
# do not convert back from standardized to original scale, because the data weren't standardized in the first place.
# https://people.ucsc.edu/~abrsvn/bayes_winbugs_jags_2.r
# If we re-run the model and monitor the residuals (as we did for the homosced. t-test), we can confirm heteroscedasticity:
cat("model{
# Priors
mu1~dnorm(0, 0.001)
mu2~dnorm(0, 0.001)
sigma1~dunif(0, 1000)
sigma2~dunif(0, 1000)
tau1 <- 1/(sigma1*sigma1)
tau2 <- 1/(sigma2*sigma2)
# Likelihood
for (i in 1:n1) {
y1[i]~dnorm(mu1, tau1)
residual1[i] <- y1[i]-mu1
}
for (i in 1:n2) {
y2[i]~dnorm(mu2, tau2)
residual2[i] <- y2[i]-mu2
}
# Derived quantities
delta <- mu2-mu1
}", fill=TRUE, file="t_test3.txt")
# Data generation
set.seed(seed)
n1 <- 60 # Number of observations in group 1
n2 <- 40 # Number of observations in group 1
mu1 <- 105 # Population mean for group 1
mu2 <- 77.5 # Population mean for group 2
sigma1 <- 3 # Population SD for group 1
sigma2 <- 2.5 # Population SD for group 2
n <- n1+n2 # Total sample size
y1 <- rnorm(n1, mu1, sigma1) # Data for group 1
y2 <- rnorm(n2, mu2, sigma2) # Data for group 2
# data -- same as before
win.data <- list(y1=y1, y2=y2, n1=n1, n2=n2)
# inits -- same as before
inits <- function() {
list(mu1=rnorm(1), mu2=rnorm(1), sigma1=rlnorm(1), sigma2=rlnorm(1))
}
params <- c("mu1", "mu2", "delta", "sigma1", "sigma2", "residual1", "residual2")
# MCMC settings -- same as before
nc <- 3
ni <- 2000
nb <- 500
nt <- 1
# run OpenBUGS
# win
# library("R2WinBUGS")
# pkg <- "package:R2WinBUGS"
# detach(pkg, character.only = TRUE)
# out <- bugs(data=win.data, inits=inits, parameters=params, model="t_test3.txt", n.thin=nt, n.chains=nc, n.burnin=nb, n.iter=ni, debug=TRUE, DIC=TRUE, clearWD=TRUE)
# linux
# requires openbugs as cmd tool
# version used: OpenBUGS version 3.2.3 rev 1012
library("R2OpenBUGS")
out <- bugs(data=win.data, inits=inits, parameters=params, model="t_test3.txt",
n.thin=nt, n.chains=nc, n.burnin=nb, n.iter=ni,
debug=FALSE, DIC=TRUE, clearWD=FALSE)
# Confirm heteroscedasticity:
par(mfrow=c(1, 2))
plot(c(out$mean$residual1, out$mean$residual2), col=c("black", "blue"), pch=c(1, 19), ylab="Residual RTs")
abline(h=0)
faktor <- factor(c(rep("Group A",each=n1),rep("Group B",each=n2)))
plot(c(out$mean$residual1, out$mean$residual2)~faktor, col="lightblue", xlab="Groups", ylab="Residual RTs")
# Doing it with JAGS
library("R2jags")
outj <- jags(win.data, inits=inits, parameters.to.save=params, model.file="t_test3.txt", n.thin=nt, n.chains=nc, n.burnin=nb, n.iter=ni)
# diagnostics of MCMC
# alot of plots...
traceplot(outj, mfrow=c(4,4))
# Confirm heteroscedasticity:
par(mfrow=c(1, 2))
plot(c(outj$BUGSoutput$mean$residual1, outj$BUGSoutput$mean$residual2), col=c("black", "blue"), pch=c(1, 19), ylab="Residual RTs")
abline(h=0)
plot(c(outj$BUGSoutput$mean$residual1, outj$BUGSoutput$mean$residual2)~faktor, col="lightblue", xlab="Groups", ylab="Residual RTs")
# end of exercise KRUSCHKE
# explanations
# school = school type written out
# stype = school type 3x (B, G, R)
# schooltype = school type 2x (B + R = R)
table(diss[,c("school","stype","schooltype")])
# not run below this point
# brms initial fit
# library(brms)
# see BayesFactors and associated analyses on this data set
# check for model that shows how to handle age as a category or continuous case - wisely!
# ie. re-introduce the uncertainty that was dropped by using integer age categories like 12, 13, 14, ...
# see brm() and pp_check()
# categorical covariates in JAGS
# http://www.mikemeredith.net/blog/2017/Categories_in_JAGS.htm
# Bayesian ANOVA: Powerful inference with within-group sample size of 1
# http://www.petrkeil.com/?p=2819
# OLD!! to save all parts of the model for further calculations
# save_all_pars=TRUE
# NOW:
# save_pars=save_pars(all=TRUE)
diss.res01 <- brm(W.noSC ~ age + schooltype * sex, data=diss, family=gaussian(), save_pars=save_pars(all=TRUE))
diss.res0 <- brm(log(W.noSC) ~ age + schooltype * sex, data=diss, family=gaussian(), save_pars=save_pars(all=TRUE))
# better fit (see Y_pred)
diss.res <- brm(log(W.noSC) ~ log(age) + schooltype * sex, data=diss, family=gaussian(), save_pars=save_pars(all=TRUE))
diss.res.t1 <- brm(log(W.noSC) ~ log(age) + schooltype + sex, data=diss, family=gaussian(), save_pars=save_pars(all=TRUE))
diss.res.t2 <- brm(log(W.noSC) ~ schooltype + sex, data=diss, family=gaussian(), save_pars=save_pars(all=TRUE))
diss.res.t3 <- brm(log(W.noSC) ~ age + schooltype + sex, data=diss, family=gaussian(), save_pars=save_pars(all=TRUE))
diss.res.t4 <- brm(log(W.noSC) ~ log(age) + schooltype + sex, data=diss, family=student(), save_pars=save_pars(all=TRUE))
diss.res.t5 <- brm(log(W.noSC) ~ log(age) + age.cat + schooltype + sex, data=diss, family=student(), save_pars=save_pars(all=TRUE))
diss.res.t6 <- brm(log(W.noSC) ~ log(age) + age.cat + schooltype * sex, data=diss, family=student(), save_pars=save_pars(all=TRUE))
diss.res.t7 <- brm(log(W.noSC) ~ log(age) + age.cat1 + schooltype * sex, data=diss, family=student(), save_pars=save_pars(all=TRUE))
diss.res.t8 <- brm(log(W.noSC) ~ log(age) + age.cat1 + schooltype + sex, data=diss, family=student(), save_pars=save_pars(all=TRUE))
diss.res.t9 <- brm(bf(log(W.noSC) ~ log(age) + age.cat1 + schooltype + sex, sigma ~ 0 + schooltype + sex), data=diss, family=student(), save_pars=save_pars(all=TRUE))
diss.res.t10 <- brm(bf(log(W.noSC) ~ log(age) + age.cat1 + schooltype + sex, sigma ~ 0 + schooltype * sex), data=diss, family=student(), save_pars=save_pars(all=TRUE))
# best model?
diss.res.t11 <- brm(bf(log(W.noSC) ~ log(age) + age.cat1 + stype + sex, sigma ~ 0 + stype * sex), data=diss, family=student(), save_pars=save_pars(all=TRUE))
diss.res.t11 <- brm(bf(W.noSC.log ~ age.log + age.cat + stype + sex, sigma ~ 0 + stype * sex), data=diss, family=student(), save_pars=save_pars(all=TRUE))
diss.res.t12.0 <- brm(bf(log(W.noSC) ~ log(age) + age.cat1 + stype + sex, sigma ~ 0 + stype + sex), data=diss, family=student(), save_pars=save_pars(all=TRUE))
diss.res.t12.1 <- brm(bf(W.noSC.log ~ age.log + age.cat + stype * sex, sigma ~ 0 + stype * sex), data=diss, family=student(), save_pars=save_pars(all=TRUE))
diss.res.t12.2 <- brm(bf(W.noSC.log ~ age.log + age.cat + stype * sex), data=diss, family=gaussian(), save_pars=save_pars(all=TRUE))
diss.res.t13 <- brm(bf(log(W.noSC) ~ log(age) + age.cat1 + stype + sex, sigma ~ 0 + stype * sex), data=diss, family=lognormal(), save_pars=save_pars(all=TRUE))
diss.res.t14 <- brm(bf(W.noSC ~ age + age.cat1 + stype + sex, sigma ~ 0 + stype * sex), data=diss, family=lognormal(), save_pars=save_pars(all=TRUE))
#
diss.res.t12 <- brm(bf(W.noSC.log ~ age.log + age.cat + stype + sex), data=diss, family=student(), save_pars=save_pars(all=TRUE))
summary(diss.res.t12)
pp_check(diss.res.t12, nsamples=100)
pp.t12 <- posterior_predict(diss.res.t12)
# unequal variances...
diss <- diss[1:363,]
diss$stype <- factor(diss$stype)
diss$sex <- factor(diss$sex)
# diss$age.cat1 <- factor(diss$age.cat1)
with(diss, tapply(W.noSC, stype, FUN=sd))
# with(diss, tapply(W.noSC.log, stype, FUN=sd))
with(diss, tapply(W.noSC, sex, FUN=sd))
# with(diss, tapply(W.noSC, age.cat1, FUN=sd))
boxplot(W.noSC~sex*stype,data=diss)
# plot pp_check for all models so far
modelstoplot <- c(paste("diss.res.t",c(1:14),sep=""), paste("diss.res.12.",0:2,sep=""),"diss.res.S")
modelstoplot
# requires all models above to be calculated - requires time!
for(i in modelstoplot)
{
par(ask=TRUE)
texte <- parse(text=paste("pp_check(",i,")",sep=""))
cat("\n",texte)
plot(eval(texte))
}
# schooltype verus stype ????
diss.res.S <- brm(bf(log(W.noSC) ~ log(age) + schooltype * sex, sigma ~ 0 + schooltype*sex), data=diss, family=gaussian(), save_pars=save_pars(all=TRUE))
bayes_factor(diss.res, diss.res.S)
bayes_factor(diss.res.t11, diss.res.S)
# output:
# Estimated Bayes factor in favor of bridge1 over bridge2: 27.68141
# best model?
diss.res.t11 <- brm(bf(log(W.noSC) ~ log(age) + age.cat1 + stype + sex, sigma ~ 0 + stype * sex), data=diss, family=student(), save_pars=save_pars(all=TRUE))
#***
diss.res.t11 <- brm(bf(W.noSC.log ~ age.log + age.cat + stype + sex, sigma ~ 0 + stype * sex), data=diss, family=student(), save_pars=save_pars(all=TRUE))
# best model? see above - t11.PRIORE
model.stan <- bf(log(W.noSC) ~ log(age) + age.cat1 + stype + sex, sigma ~ 0 + stype * sex)
priors <- get_prior(model.stan, data=diss)
priors
priore <- c(
# prior(normal(0,3), class=b), # remove because newer versions of brms complain about it
prior(normal(0,3), class=b, coef="age.cat120M25"),
prior(normal(0,3), class=b, coef="logage"),
prior(normal(0,3), class=b, coef="sexw"),
prior(normal(0,3), class=b, coef="stypeG"),
prior(normal(0,3), class=b, coef="stypeR"),
prior(student_t(3,5,10), class=Intercept),
# prior(cauchy(0,3), class=b, dpar=sigma), # remove because newer versions of brms complain about it
prior(cauchy(0,3), class=b, coef="sexw", dpar=sigma),
prior(cauchy(0,3), class=b, coef="stypeB", dpar=sigma),
prior(cauchy(0,3), class=b, coef="stypeG", dpar=sigma),
prior(cauchy(0,3), class=b, coef="stypeG:sexw", dpar=sigma),
prior(cauchy(0,3), class=b, coef="stypeR", dpar=sigma),
prior(cauchy(0,3), class=b, coef="stypeR:sexw", dpar=sigma)
)
priore
# check priors
make_stancode(model.stan, data=diss, family=student(), prior=priore, save_pars=save_pars(all=TRUE))
# run model
diss.res.t11.pr1 <- brm(model.stan, data=diss, family=student(), prior=priore, save_pars=save_pars(all=TRUE))
model.diss.t11 <- stancode(diss.res.t11)
model.diss.t11
# plot sigmas
# TODO - arrange next to each other
sigmas <- exp(posterior_samples(diss.res.t11, "^b_sigma_"))
# library(ggplot2)
# does not look good (layout), would require new layout
ggplot(stack(sigmas), aes(values)) + geom_density(aes(fill = ind))
# post prior values
prior_summary(diss.res.t11)
prior_summary(diss.res.t11.pr1)
# nonsense! almost same results... but different update of the prior knowledge!
bayes_factor(diss.res.t11, diss.res.t11.pr1)
# output:
# Estimated Bayes factor in favor of bridge1 over bridge2: 27298732393.50789
# =27'298'732'393.50789
# just don't believe a BF, because it is "just" a change of expectation and not absolte result
# due to versions of brms, OS, etc. those numbers differ(!)
# summaries
summary(diss.res.t11)
summary(diss.res.t11.pr1)
# compare models
crits <- c("loo","waic","kfold","bayes_R2", "loo_R2", "marglik") #"loo_subsample"
diss.t11.ac <- add_criterion(diss.res.t11, criterion=crits)
diss.t11.pr1.ac <- add_criterion(diss.res.t11.pr1, criterion=crits)
######### NOTE!!!
# older brms versions require different calls and store infos differently
# IF something does not work, better do RTFM and adjust the code accordingly
# to any possible changes in brms
loo:::loo(diss.res.t11)
loo:::loo(diss.res.t11.pr1)
#1: Found 1 observations with a pareto_k > 0.7 in model 'diss.res.t11'. It is recommended to set 'moment_match = TRUE' in order to perform moment matching for problematic observations.
#3 (0.8%) p_waic estimates greater than 0.4. We recommend trying loo instead.
#1: Found 1 observations with a pareto_k > 0.7 in model 'diss.res.t11.pr1'. It is recommended to set 'moment_match = TRUE' in order to perform moment matching for problematic observations.
# (0.8%) p_waic estimates greater than 0.4. We recommend trying loo instead.
diss.t11.ac
diss.t11.pr1.ac
loo:::loo(diss.res.t11, moment_match=TRUE)
loo:::loo(diss.res.t11.pr1, moment_match=TRUE)
loo:::waic(diss.res.t11)
loo:::waic(diss.res.t11.pr1)
loo:::kfold(diss.res.t11)
loo:::kfold(diss.res.t11.pr1)
brms:::bayes_R2.brmsfit(diss.res.t11)
brms:::bayes_R2.brmsfit(diss.res.t11.pr1)
t(summary(diss.t11.ac$criteria$loo_R2))
t(summary(diss.t11.pr1.ac$criteria$loo_R2))
diss.t11.ac$criteria$marglik
diss.t11.pr1.ac$criteria$marglik
loo_compare(diss.t11.ac, diss.t11.pr1.ac, criterion="loo")
loo_compare(diss.t11.ac, diss.t11.pr1.ac, criterion="waic")
loo_compare(diss.t11.ac, diss.t11.pr1.ac, criterion="kfold")
#does not work here
#loo_subsample(diss.t11.ac)
# OLD versions (calls)
crits <- c("loo","waic","kfold","R2","loo_R2","marglik")
diss.t11.ac <- add_criterion(diss.res.t11, criterion=crits, reloo=TRUE)
diss.t11.pr1.ac <- add_criterion(diss.res.t11.pr1, criterion=crits, reloo=TRUE)
# leave out observation 317 -> check it! (= LOO)
# print out information criteria
crits.print <- c("loo","waic","kfold","bayes_R2","marglik") #diss.t11.ac$criteria$bayes_R2
# OLD
for(i in crits.print) print(diss.t11.ac[[i]])
for(i in crits.print) print(diss.t11.pr1.ac[[i]])
# NEW
for(i in crits.print) print(diss.t11.ac$criteria[[i]])
for(i in crits.print) print(diss.t11.pr1.ac$criteria[[i]])
# comparison plot
par(mfrow=c(1,2))
# OLD
plot(diss.t11.pr1.ac$loo)
plot(diss.t11.pr1.ac$loo)
# NEW
plot(diss.t11.ac$criteria$loo)
plot(diss.t11.pr1.ac$criteria$loo)
# OLD
# R^2
R2 <- t(rbind(
c(summary(as.vector(diss.t11.ac$R2)), SD=sd(diss.t11.ac$R2), quantile(diss.t11.ac$R2)),
c(summary(as.vector(diss.t11.pr1.ac$R2)), SD=sd(diss.t11.pr1.ac$R2), quantile(diss.t11.pr1.ac$R2))
))
colnames(R2) <- c("R2|t11","R2|t11.pr1")
R2 <- data.frame(R2,Ratio=R2[,1]/R2[,2])
R2
# NEW
R2 <- t(rbind(
c( summary(as.vector(diss.t11.ac$criteria$bayes_R2)), SD=sd(diss.t11.ac$criteria$bayes_R2), quantile(diss.t11.ac$criteria$bayes_R2)),
c(summary(as.vector(diss.t11.pr1.ac$criteria$bayes_R2)), SD=sd(diss.t11.pr1.ac$criteria$bayes_R2), quantile(diss.t11.pr1.ac$criteria$bayes_R2))
))
colnames(R2) <- c("R2|t11","R2|t11.pr1")
R2 <- data.frame(R2,Ratio=R2[,1]/R2[,2])
R2
# OLD
# comparisons
for(i in c("loo","waic","kfold"))
{