-
Notifications
You must be signed in to change notification settings - Fork 22
/
cooper2019.rmd
2292 lines (1716 loc) · 65.4 KB
/
cooper2019.rmd
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
---
title: "R Code Corresponding to the Book *The Handbook of Research Synthesis and Meta-Analysis* by Cooper et al. (2019)"
author: |
| Wolfgang Viechtbauer
| Maastricht University
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
# code_download: true
df_print: default
toc: true
number_sections: false
toc_depth: 3
toc_float:
collapsed: true
theme: default
# lots of nice themes can be used: https://bootswatch.com/
highlight: haddockadj.theme
# rmarkdown::github_document
# pdf_document:
# toc: true
# number_sections: false
# toc_depth: 3
# word_document
fig_caption: no
# bibliography: references.bib
---
```{r klippy, echo=FALSE, include=TRUE}
# remotes::install_github("rlesur/klippy")
klippy::klippy(position = c("top", "right"), color="gray20")
```
```{r crayon, echo=FALSE, message=FALSE, include=TRUE}
library(crayon)
options(crayon.enabled = TRUE)
knitr::knit_hooks$set(output = function(x, options){
paste0(
'<pre class="r-output"><code>',
fansi::sgr_to_html(x = htmltools::htmlEscape(x), warn = FALSE),
'</code></pre>'
)
})
```
## General Notes / Setup
*The Handbook of Research Synthesis and Meta-Analysis* by Cooper et al. (2019), now in its third edition, has been one of the quintessential texts on meta-analysis and the entire research synthesis process as a whole. In this document, I provide the R code to reproduce the worked examples and analyses from various chapters. Emphasis will be on using the `metafor` package, but several other packages will also be used. To read more about the `metafor` package, see the [package website](https://www.metafor-project.org/) and the [package documentation](https://wviechtb.github.io/metafor/).
The package can be installed with:
```{r, eval=FALSE}
install.packages("metafor")
```
Once the package is installed, we can load it with:
```{r, message=FALSE}
library(metafor)
```
A few additional notes:
1. Results are only reproduced for chapters containing worked examples.
2. Occasionally, there are some minor discrepancies between the results shown in the book and those obtained below. These can result from using different software packages that implement methods in slightly different ways, due to intermittent rounding or using a different rounding scheme, or due to chance when the analyses involve some stochastic process (e.g., when using MCMC sampling in the chapter on Bayesian methods). Minor discrepancies will (usually) not be commented on. However, where discrepancies are more substantial, they will be noted (and the reasons for them if they are known).
3. The results are generally given without discussion or context. The code below is not a substitute for reading the book, but is meant to be used together with it. In other words, readers of the book interested in replicating the results with R can see here how this is possible.
```{r, echo=FALSE, message=FALSE, warning=FALSE}
setmfopt(space=FALSE)
setmfopt(style=list(legend=make_style("gray90"), warning=strip_style))
pointsize <- 14
options(width=94)
```
***
## 1) Research Synthesis as a Scientific Process
To recreate Figure 1.1 (showing the number of citations to articles including the terms 'research synthesis', 'systematic review', or 'meta-analysis' in their titles), I redid the search in the Web of Science Core Collection, which yielded the following data:
```{r}
dat <- read.table(header=TRUE, text = "
year hits
2020 15973
2019 32120
2018 26293
2017 23885
2016 21318
2015 18487
2014 14781
2013 12357
2012 9802
2011 7528
2010 6120
2009 5121
2008 4006
2007 3553
2006 2771
2005 2336
2004 1911
2003 1526
2002 1309
2001 1005
2000 891
1999 832
1998 729
1997 580
1996 466
1995 70
1994 25
1993 11
1992 5
1991 9
1990 20
1989 127
1988 104
")
dat <- dat[-1,] # remove current year (not complete)
dat <- dat[dat$year >= 1995,] # keep only 1995 or later
dat <- dat[nrow(dat):1,] # reverse order
```
We can then create a bar chart based on these data:
```{r, figure01_1, fig.width=8, fig.height=6, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 1.1
par(mar=c(4,4,2,2))
barplot(dat$hits, names.arg=dat$year, las=2, space=0.4, col="#6c9ece", border=NA)
abline(h=seq(0, 30000, by=5000), col="gray")
barplot(dat$hits, space=0.4, col="#6c9ece", border=NA, add=TRUE, axes=FALSE)
```
***
## 10) Evaluating Coding Decisions
Part of the code for this chapter (by Jack Vevea, Nicole Zelinksy, and Robert Orwin) is adapted from the chapter itself (see section 10.5). Below, we will make use of several additional packages that need to be installed (if they are not already installed). So let's do this first.
```{r, message=FALSE, warning=FALSE}
# install the 'irr' package (if it is not already installed) and load it
if (!require(irr)) {
install.packages("irr")
library(irr)
}
```
```{r, message=FALSE, warning=FALSE}
# install the 'psych' package (if it is not already installed) and load it
if (!require(psych)) {
install.packages("psych")
library(psych)
}
```{r, message=FALSE, warning=FALSE}
# install the 'vcd' package (if it is not already installed) and load it
if (!require(vcd)) {
install.packages("vcd")
library(vcd)
}
```{r, message=FALSE, warning=FALSE}
# install the 'lme4' package (if it is not already installed) and load it
if (!require(lme4)) {
install.packages("lme4")
library(lme4)
}
```
```{r}
# Table 10.1
dat <- read.table(header=TRUE, text = "
study c1 c2 c3
1 3 2 3
2 3 1 1
3 2 2 2
4 3 2 3
5 1 1 1
6 3 1 3
7 2 2 1
8 1 1 1
9 2 2 1
10 2 1 3
11 2 2 2
12 3 3 3
13 3 1 2
14 2 1 1
15 1 1 1
16 1 1 2
17 3 3 1
18 2 2 2
19 2 2 2
20 3 1 1
21 2 1 2
22 1 1 3
23 3 2 2
24 3 3 3
25 2 2 3")
```
```{r}
# put ratings for coders 1, 2, and 3 into separate vectors
c1 <- dat$c1
c2 <- dat$c2
c3 <- dat$c3
# combine ratings for coders 1 and 2 into a matrix
c1c2 <- cbind(c1, c2)
# combine ratings from all three coders into a matrix
all3 <- cbind(c1, c2, c3)
```
```{r}
# cell counts and marginal totals for coders 1 and 2
addmargins(table(c2, c1))
# note: first variable is for rows, second is for columns, so to reproduce
# panel A of Table 10.2, we have to use table(c2, c1)
```
```{r}
# agreement rate for coders 1 and 2
mean(c1 == c2)
```
```{r}
# agreement rate for all three coders
mean(c1 == c2 & c1 == c3)
```
```{r}
# agreement rate (in %) between coders 1 and 2
irr::agree(c1c2)
# note: agree(c1c2) would have been sufficient, but due to the large number of
# additional packages being used, I will make it clear by using the :: operator
# which package a function belongs to (unless this is clear from the context)
```
```{r}
# agreement rate (in %) between all three coders
irr::agree(all3)
```
```{r}
# unweighted Cohen's kappa for coders 1 and 2
irr::kappa2(c1c2)
```
```{r}
# unweighted Cohen's kappa for all three coders
irr::kappam.fleiss(all3)
```
```{r}
# weighted Cohen's kappa for coders 1 and 2
irr::kappa2(c1c2, weight=0:2)
```
We can also use the `psych` package to compute Cohen's kappa, which also provides corresponding confidence intervals.
```{r}
# unweighted and weighted Cohen's kappa for coders 1 and 2
W <- outer(1:3, 1:3, FUN = function(x,y) abs(x-y)) # create weight matrix
W
res <- psych::cohen.kappa(c1c2, w=W)
print(res, digits=3)
```
Note that the CI for weighted kappa is not correct! Using the `vcd` package, we can also compute Cohen's kappa and obtain the correct CI for weighted kappa.
```{r}
print(vcd::Kappa(table(c1,c2)), digits=3, CI=TRUE)
# note: the (default) weighting scheme used for computing weighted kappa by
# the function is the one described in the chapter
```
```{r}
# Krippendorff's alpha for coders 1 and 2 when treating the data as ratings on
# a nominal, on an ordinal, or on a ratio scale
irr::kripp.alpha(t(c1c2))
irr::kripp.alpha(t(c1c2), method="ordinal")
irr::kripp.alpha(t(c1c2), method="ratio")
```
```{r}
# correlation between coders 1 and 2
cor(c1, c2)
# note: the cor() function is part of the 'stats' package, which comes with R
```
```{r}
# mean correlation between all pairs of coders
irr::meancor(all3)
```
```{r}
# intraclass correlation coefficient for coders 1 and 2
psych::ICC(c1c2)
# note: this function computes 6 different types of ICCs; the first three are
# discussed in the chapter and correspond to the three different designs
# described on page 187
```
Using the `lmer()` function from the `lme4` package, we can also do these calculations manually.
```{r}
# restructure data into 'long' format
dat <- data.frame(study = 1:25,
rater = rep(1:2, each=25),
rating = c(c1,c2))
# absolute agreement based on one-way random-effects model
res <- lmer(rating ~ (1 | study), data = dat)
vcs <- data.frame(VarCorr(res))
vcs$vcov[1] / (vcs$vcov[1] + vcs$vcov[2])
# absolute agreement based on two-way random-effects model
res <- lmer(rating ~ (1 | study) + (1 | rater), data = dat)
vcs <- data.frame(VarCorr(res))
vcs$vcov[1] / (vcs$vcov[1] + vcs$vcov[2] + vcs$vcov[3])
# absolute agreement based on two-way mixed-effects model
res <- lmer(rating ~ rater + (1 | study), data = dat)
vcs <- data.frame(VarCorr(res))
vcs$vcov[1] / (vcs$vcov[1] + vcs$vcov[2])
```
```{r}
# example data from page 199
dat <- data.frame(
study = 1:25,
rater = rep(1:3, each=25),
rating = c(3,3,2,3,NA,3,2,1,2,2,NA,3,3,2,1,1,3,2,2,3,2,1,3,NA,2,
2,1,NA,2,1,1,2,1,2,1,2,3,1,1,NA,1,3,2,2,1,1,1,2,3,2,
3,1,2,3,1,3,1,1,NA,3,2,3,2,1,1,2,1,2,2,1,2,3,2,3,3))
dat[c(1:4, 71:75),]
```
```{r}
# absolute agreement for all three raters (based on one-way random-effects model)
res <- lmer(rating ~ (1 | study), data = dat)
vcs <- data.frame(VarCorr(res))
vcs$vcov[1] / (vcs$vcov[1] + vcs$vcov[2])
```
***
## 11) Effect Sizes for Meta-Analysis
```{r}
# data for Figure 11.1
dat <- read.table(header=TRUE, text = "
study md n var se pval
A 0.400 60 0.067 0.258 0.121
B 0.200 600 0.007 0.082 0.014
C 0.300 100 0.040 0.201 0.134
D 0.400 200 0.020 0.141 0.005
E 0.300 400 0.010 0.100 0.003
F -0.200 200 0.020 0.141 0.157")
dat
```
```{r, figure11_1, fig.width=8.5, fig.height=5.5, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 11.1
res <- rma(md, var, data=dat, method="EE", slab=study)
tmp <- dat[-1]
tmp$se <- fmtx(tmp$se, digits=3)
tmp$var <- fmtx(tmp$var, digits=3)
size <- sqrt(weights(res))
size <- 2.5 * size / max(size)
par(mar=c(5,4,2,2))
forest(res, xlim=c(-6.5,1.4), psize=size, header=TRUE, mlab="Combined",
efac=c(0,1,2), annotate=FALSE, xlab="Standardized Mean Difference",
ilab=tmp, ilab.xpos=c(-5.0, -4.1, -3.2, -2.3, -1.4))
text(-5.0, 8, "Mean\nDifference", font=2)
text(-4.1, 8, "Sample\nSize", font=2)
text(-3.2, 8, "Variance", font=2)
text(-2.3, 8, "Standard\nError", font=2)
text(-1.4, 8, "p-Value", font=2)
```
### Effect Sizes for a Comparison of Means
```{r}
# mean difference assuming sigma^2_1 = sigma^2_1
dat <- escalc("MD", m1i=103, m2i=100, sd1i=5.5, sd2i=4.5, n1i=50, n2i=50, vtype="HO")
summary(dat) # note: summary() so we can also see the standard error (sei)
# mean difference not assuming sigma^2_1 = sigma^2_1
dat <- escalc("MD", m1i=103, m2i=100, sd1i=5.5, sd2i=4.5, n1i=50, n2i=50)
summary(dat)
# note: since n1i=n2i in this example, the results are exactly the same
```
```{r}
# mean change
dat <- escalc("MC", m1i=105, m2i=100, sd1i=10, sd2i=10, ni=50, ri=0.5)
summary(dat)
```
```{r}
# standardized mean difference (Hedges' g)
dat <- escalc("SMD", m1i=103, m2i=100, sd1i=5.5, sd2i=4.5, n1i=50, n2i=50)
summary(dat)
```
```{r}
# note: by default, the sampling variance is computed in a slightly different way in the
# book compared to the metafor package; by using vtype="LS2", the exact same equation as
# given in the book is used; but the difference is usually negligible (the results below
# differ slightly from those given in the book due to intermittent rounding in the book)
dat <- escalc("SMD", m1i=103, m2i=100, sd1i=5.5, sd2i=4.5, n1i=50, n2i=50, vtype="LS2")
summary(dat)
```
```{r}
# standardized mean change (using raw score standardization)
dat <- escalc("SMCR", m1i=103, m2i=100, sd1i=5.5/sqrt(2*(1-0.7)), ni=50, ri=0.7)
summary(dat)
# note: 5.5 is the SD of the change scores, which we can convert to the SD of
# the raw scores by dividing it by sqrt(2*(1-r)) (this assumes that the SD was
# the same at the pre- and post-test)
```
```{r}
# note: by default, the sampling variance is computed in a slightly different way in the
# book compared to the metafor package; by using vtype="LS2", the exact same equation as
# given in the book is used; but the difference is usually negligible (the results below
# differ slightly from those given in the book due to intermittent rounding in the book
# and also because the equation given in the book contains a mistake)
dat <- escalc("SMCR", m1i=103, m2i=100, sd1i=5.5/sqrt(2*(1-0.7)), ni=50, ri=0.7, vtype="LS2")
summary(dat)
```
```{r}
# standardized mean difference based on ANCOVA results
# note: not implemented in metafor, so we have to do the computations manually
Sw <- 5.5 / sqrt(1 - 0.7^2)
d <- (103 - 100) / Sw
Vd <- (50 + 50) * (1 - 0.7^2) / (50 * 50) + d^2 / (2*(50 + 50 - 2 - 1))
J <- metafor:::.cmicalc(50 + 50 - 2 - 1)
g <- J * d
Vg <- J^2 * Vd
round(g, digits=4)
round(Vg, digits=4)
round(sqrt(Vg), digits=4)
# note: the results given in the book are not quite correct
```
### Correlations
```{r}
# r-to-z transformed correlation coefficient
dat <- escalc("ZCOR", ri=0.50, ni=100)
summary(dat)
# back-transformation
c(transf.ztor(dat$yi))
```
### Effect Sizes for Comparing Risks
```{r}
# risk difference
dat <- escalc("RD", ai=5, n1i=100, ci=10, n2i=100)
summary(dat)
```
```{r}
# risk ratio (log transformed)
dat <- escalc("RR", ai=5, n1i=100, ci=10, n2i=100)
summary(dat)
```
```{r}
# odds ratio (log transformed)
dat <- escalc("OR", ai=5, n1i=100, ci=10, n2i=100)
summary(dat)
```
```{r}
# odds ratio (log transformed) for a case-control study
dat <- escalc("OR", ai=25, bi=20, ci=75, di=80)
summary(dat)
```
***
## 12) Statistically Analyzing Effect Sizes: Equal- and Random-Effects Models
```{r}
# Table 12.1: Data for the Gender Differences in Conformity Example
dat <- read.table(header=TRUE, text = "
study group stdingrp nitems pmaleauth n d v
1 1 1 2 141 25 -0.330 0.029
2 1 2 2 119 25 0.070 0.034
3 2 1 2 191 50 -0.300 0.022
4 3 1 38 254 100 0.350 0.016
5 3 2 30 64 100 0.700 0.066
6 3 3 45 20 100 0.850 0.218
7 3 4 45 90 100 0.400 0.045
8 3 5 45 60 100 0.480 0.069
9 3 6 5 80 100 0.370 0.051
10 3 7 5 125 100 -0.060 0.032")
# note: including the 'percent male authors' variable from Table 12.3
dat
```
```{r}
# equal-effects model analysis
res <- rma(d, v, data=dat, method="EE")
print(res, digits=3)
```
Note: The book uses the term 'fixed-effects model', but I will use the term 'equal-effects model' throughout this document. My reasons for using the latter term are explained [here](https://wviechtb.github.io/metafor/reference/misc-models.html).
```{r}
# random-effects model analysis
res <- rma(d, v, data=dat, method="DL")
print(res, digits=3)
# note: unfortunately, the estimate of tau^2 was not computed correctly in the
# book (c=242.1138, not 269.798) and hence all of the results given in the left
# column on page 251 are incorrect
```
```{r}
# fixed-effects ANOVA-type analysis
res <- rma(d, v, mods = ~ factor(group) - 1, data=dat, method="FE")
print(res, digits=3)
# note: by removing the intercept, the three coefficients directly provide the
# estimated average effect for the three groups
```
```{r}
# weighted grand mean effect size
rma(coef(res), diag(vcov(res)), method="FE", digits=3)
```
```{r}
# partitioning of the Q-statistics
res <- rma(d, v, mods = ~ factor(group), data=dat, method="FE")
res
# not removing the intercept, so the QM-statistic is equal to Q-between
round(res$QM, digits=3) # Q-between
round(res$QE, digits=3) # Q-within
# Q-within for each group
res1 <- rma(d, v, data=dat, method="FE", subset=group==1)
res2 <- rma(d, v, data=dat, method="FE", subset=group==2)
res3 <- rma(d, v, data=dat, method="FE", subset=group==3)
round(res1$QE, digits=3)
round(res2$QE, digits=3) # 0.0004 in the book, but must be exactly 0 since k=1
round(res3$QE, digits=3)
# these add up to Q-within above
round(res1$QE + res2$QE + res3$QE, digits=3)
```
```{r}
# contrast between group 1 and 3
res <- rma(d, v, mods = ~ factor(group) - 1, data=dat, method="FE")
anova(res, X=c(-1,0,1), digits=3)
predict(res, newmods=c(-1,0,1), digits=3)
# note: the results given in the book are slightly off
```
```{r}
# mixed-effects model analysis
# distribution-free (method of moments) estimate of tau^2
res <- rma(d, v, mods = ~ factor(group), data=dat, method="DL")
round(res$tau2, digits=3)
# maximum likelihood estimate of tau^2
res <- rma(d, v, mods = ~ factor(group), data=dat, method="ML")
round(res$tau2, digits=3)
# restricted maximum likelihood estimate of tau^2
res <- rma(d, v, mods = ~ factor(group), data=dat, method="REML")
round(res$tau2, digits=3)
# note: the REML estimate is incorrectly claimed to be 0 in the book
```
```{r}
res <- rma(d, v, mods = ~ factor(group) - 1, data=dat, method="DL")
print(res, digits=3)
# note: by removing the intercept, the three coefficients directly provide the
# estimated average effect for the three groups
# weighted grand mean effect size
rma(coef(res), diag(vcov(res)), method="FE", digits=3)
# contrast between group 1 and 3
anova(res, X=c(-1,0,1), digits=3)
predict(res, newmods=c(-1,0,1), digits=3)
```
```{r}
# meta-regression model
res <- rma(d, v, mods = ~ I(log(nitems)), data=dat, method="FE")
print(res, digits=3)
# note: when doing transformations on predictors (such as taking the log) in
# the model formula, then we need to wrap this inside the I() function
```
```{r}
# mixed-effects meta-regression model
# distribution-free (method of moments) estimate of tau^2
res <- rma(d, v, mods = ~ I(log(nitems)), data=dat, method="DL")
round(res$tau2, digits=3)
# maximum likelihood estimate of tau^2
res <- rma(d, v, mods = ~ I(log(nitems)), data=dat, method="ML")
round(res$tau2, digits=3)
# restricted maximum likelihood estimate of tau^2
res <- rma(d, v, mods = ~ I(log(nitems)), data=dat, method="REML")
round(res$tau2, digits=3)
# note: the REML estimate is incorrectly claimed to be 0 in the book
```
```{r}
# continuing with the distribution-free (method of moments) estimate of tau^2
res <- rma(d, v, mods = ~ I(log(nitems)), data=dat, method="DL")
print(res, digits=3)
```
```{r}
# robust variance estimation
robust(res, cluster=study, digits=3)
# note: the test statistic for log(nitems) is somewhat off in the book
```
***
## 13) Stochastically Dependent Effect Sizes
```{r}
# Table 13.1
dat <- dat.kalaian1996[c(1:8,17:29,9:16,30:31),c(2,4:5,7,6,8)]
dat$study <- rep(1:26, times=rle(dat$study)$lengths)
names(dat) <- c("study", "nt", "nc", "d", "outcome", "v")
dat
# note: this is a subset of the studies in dat.kalaian1996; instead of using
# dummy variable 'x' (where x=0 for the verbal and x=1 for the math subtest),
# we use the 'outcome' variable that has more descriptive labels
```
```{r}
# construct variance-covariance matrices assuming rho = 0.7
V <- vcalc(v, cluster=study, type=outcome, rho=0.7, data=dat)
# var-cov matrix for studies 22 to 26
blsplit(V, cluster=dat$study, round, 4)[22:26]
```
```{r}
# multivariate model with (correlated) random effects for both outcomes
res <- rma.mv(d, V, mods = ~ outcome - 1,
random = ~ outcome | study, struct="UN", data=dat)
print(res, digits=3)
# note: by removing the intercept, the two coefficients directly provide the
# estimated average effect for the two outcomes
# note: both variance components are very close to 0; as in the book, we
# proceed with a model where both variances are constrained to be equal to 0
```
```{r}
# multivariate fixed-effects model
res <- rma.mv(d, V, mods = ~ outcome - 1, data=dat)
print(res, digits=3)
```
```{r}
# fit model with varying values of rho
rhos <- c(0, 0.5, 0.6, 0.7, 0.8)
res <- list()
for (i in 1:length(rhos)) {
V <- vcalc(v, cluster=study, type=outcome, rho=rhos[i], data=dat)
res[[i]] <- rma.mv(d, V, mods = ~ outcome - 1, data=dat)
}
# Table 13.2
tab <- data.frame(rho = rhos,
b1 = sapply(res, function(x) coef(x)[1]),
s1 = sapply(res, function(x) x$se[1]),
lcl1 = sapply(res, function(x) x$ci.lb[1]),
ucl1 = sapply(res, function(x) x$ci.ub[1]),
z1 = sapply(res, function(x) x$zval[1]),
b2 = sapply(res, function(x) coef(x)[2]),
s2 = sapply(res, function(x) x$se[2]),
lcl2 = sapply(res, function(x) x$ci.lb[2]),
ucl2 = sapply(res, function(x) x$ci.ub[2]),
z2 = sapply(res, function(x) x$zval[2]))
dfround(tab, digits=c(1,2,3,2,2,2,2,3,2,2,2))
# note: there are some printing errors in the table in the book;
# also the values given in the Z_2 column in the book are quite off
```
```{r}
# robust variance estimation
V <- vcalc(v, cluster=study, type=outcome, rho=0.7, data=dat)
res <- rma.mv(d, V, mods = ~ outcome - 1, data=dat)
robust(res, cluster=study)
# note: this is not the exact same approach that is described in the book; it
# uses a different weighting scheme and uses k-p for the degrees of freedom
# for the t-statistics; we can make use of the 'effective degrees of freedom'
# method described in the book with the 'clubSandwich' package
```
```{r, message=FALSE, warning=FALSE}
# install the 'clubSandwich' package (if it is not already installed) and load it
if (!require(clubSandwich))
install.packages("clubSandwich")
```
```{r}
robust(res, cluster=study, clubSandwich=TRUE)
# note: but these are still not the same results as given in the book because
# of the different weighting scheme; we can get the same results as given in
# the book with the 'robumeta' package
```
```{r, message=FALSE, warning=FALSE}
# install the 'robumeta' package (if it is not already installed) and load it
if (!require(robumeta)) {
install.packages("robumeta")
library(robumeta)
}
```
```{r}
robu(d ~ outcome - 1, data=dat, studynum=study, var.eff.size=v, rho=0.7, small=TRUE)
# note: the value of rho actually has no influence on these results
```
```{r}
# reproduce robumeta results using metafor and clubSandwich
vcalcfun <- function(v, tau2)
diag((tau2 + mean(v)) * length(v), nrow=length(v), ncol=length(v))
V <- lapply(split(dat$v, dat$study), vcalcfun, tau2=0)
res <- rma.mv(d, V, mods = ~ outcome - 1, data=dat)
robust(res, cluster=study, clubSandwich=TRUE)
```
```{r, include=FALSE}
# an example where tau^2 != 0
tmp <- dat
tmp$v <- tmp$v / 10
res <- robu(d ~ outcome - 1, data=tmp, studynum=study, var.eff.size=v, rho=0.7, small=TRUE)
res
V <- lapply(split(tmp$v, tmp$study), vcalcfun, tau2=c(res$mod_info$tau.sq))
res <- rma.mv(d, V, mods = ~ outcome - 1, data=tmp)
robust(res, cluster=study, clubSandwich=TRUE)
```
```{r}
# construct dataset with synthetic effects
agg <- aggregate(dat, by=list(dat$study), function(x) {
if (is.character(x))
paste(unique(x), collapse="/")
else
mean(x)
})
agg$Group.1 <- agg$x <- NULL
agg
```
```{r, include=FALSE, eval=FALSE}
# can also do this
dat <- escalc(yi=d, vi=v, data=dat, var.names=c("d","v"))
agg <- aggregate(dat, cluster=study, struct="CS", rho=1, weighted=FALSE, checkpd=FALSE,
select=c("study", "d", "v"))
agg
```
```{r}
# fit standard random-effects model to the dataset with synthetic effects
res <- rma(d, v, data=agg, method="DL")
print(res, digits=3)
```
***
## 14) Bayesian Meta-Analysis
For these analyses, we could do most of what is described in the chapter using the excellent [brms](https://cran.r-project.org/package=brms) package. However, to fully reproduce all of the analyses conducted, we either need to use [WinBUGS](https://www.mrc-bsu.cam.ac.uk/software/bugs/the-bugs-project-winbugs/) (as was done by the chapter authors) or we can use [JAGS](http://mcmc-jags.sourceforge.net/) (which has the advantage that it runs not just under Windows, but also macOS / Mac OS X and Linux). Here, we will make use of the latter, which we can interact with directly from R via the [rjags](https://cran.r-project.org/package=rjags) package. Note that JAGS needs to be installed separately (follow the link above for installation instructions).
```{r, message=FALSE, warning=FALSE}
# install the 'rjags' package (if it is not already installed) and load it
if (!require(rjags)) {
install.packages("rjags")
library(rjags)
}
```
```{r}
# Table 14.1: Respiratory Tract Infections Data
dat <- dat.damico2009[c(1:8,16,9:15),-8]
dat$conceal <- 1 - dat$conceal
rownames(dat) <- 1:nrow(dat)
dat <- escalc(measure="OR", ai=xt, n1i=nt, ci=xc, n2i=nc, data=dat, digits=2)
dat
# note: including 'conceal' variable (coded 0 = allocation concealment
# adequate, 1 = allocation concealment inadequate)
```
```{r}
# Bayesian equal-effects model
k <- length(dat$yi)
jags.data <- list(yi=dat$yi, vi=dat$vi, k=k)
fe.model <- "model {
for (i in 1:k) {
yi[i] ~ dnorm(theta, 1/vi[i])
}
theta ~ dnorm(0, 0.0001)
}"
inits <- list(theta=0, .RNG.name="base::Mersenne-Twister")
inits <- list(inits, inits, inits)
inits[[1]]$.RNG.seed <- 12341
inits[[2]]$.RNG.seed <- 12342
inits[[3]]$.RNG.seed <- 12343
# note: usually 'inits <- list(theta=0)' would have been sufficient; however,
# for full reproducibility, I am setting the random number generator and
# the seed for each chain (the same will be done below for other models)
model <- jags.model(textConnection(fe.model), inits=inits, data=jags.data, n.chains=3, quiet=TRUE)
update(model, n.iter=10000, progress.bar="none")
bma.ee <- coda.samples(model, variable.names=c("theta"), n.iter=100000, progress.bar="none")
dic.ee <- dic.samples(model, n.iter=100000, type="pD", progress.bar="none")
bma.ee <- summary(bma.ee)$quantiles[c(3,1,5)]
bma.ee <- rbind(theta=bma.ee, exp.theta = exp(bma.ee))
round(bma.ee, digits=2)
```
```{r}
# compare with results from a non-Bayesian analysis
res.ee <- rma(yi, vi, data=dat, method="EE", digits=2)
predict(res.ee, transf=exp)
```
```{r}
# Bayesian random-effects model with uniform(0,2) prior for tau
re.model <- "model {
for (i in 1:k) {
yi[i] ~ dnorm(theta[i], 1/vi[i])
theta[i] ~ dnorm(mu, 1/tau2)
}
mu ~ dnorm(0, 0.0001)
tau ~ dunif(0, 2)
tau2 <- tau^2
theta.2 <- theta[2]
theta.3 <- theta[3]
theta.new ~ dnorm(mu, 1/tau2)
}"
inits <- list(theta=rep(0,k), theta.new=0, mu=0, tau=1, .RNG.name="base::Mersenne-Twister")
inits <- list(inits, inits, inits)
inits[[1]]$.RNG.seed <- 12341
inits[[2]]$.RNG.seed <- 12342
inits[[3]]$.RNG.seed <- 12343
model <- jags.model(textConnection(re.model), inits=inits, data=jags.data, n.chains=3, quiet=TRUE)
update(model, n.iter=10000, progress.bar="none")
bma.re <- coda.samples(model, variable.names=c("mu","tau2","theta.2","theta.3","theta.new"),
n.iter=100000, progress.bar="none")
dic.re <- dic.samples(model, n.iter=100000, type="pD", progress.bar="none")
bma.re <- summary(bma.re)$quantiles[,c(3,1,5)]
bma.re <- rbind(mu=bma.re[1,], exp.mu=exp(bma.re[1,]), tau2=bma.re[2,],
bma.re[3:5,], exp.theta.new=exp(bma.re[5,]))
round(bma.re[1:3,], digits=2)
```
```{r}
# compare with results from a non-Bayesian analysis