-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathborenstein2009.rmd
1382 lines (1018 loc) · 37.5 KB
/
borenstein2009.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 *Introduction to Meta-Analysis* by Borenstein et al. (2009)"
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
As the title promises, the book *Introduction to Meta-Analysis* by Borenstein et al. (2009) is a comprehensive introduction to standard meta-analytic methodology, with focus on the statistical methods. This document provides the R code to reproduce all examples and illustrations from the book using the `metafor` package. To read more about the 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. Where such discrepancies arise, they are noted (and the reasons for them if they are known).
3. I did not attempt to reproduce the figures exactly as they appear in the book. There are some fundamental differences in the aesthetics of the figures shown in the book and the functions in the `metafor` package for producing the corresponding figures. I made some adjustments to the defaults here and there to make the figures below look similar to the ones shown in the book and made sure to include all relevant elements.
4. 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
runperm <- TRUE
# runperm <- FALSE
options(width=94)
```
***
## 1) How a Meta-Analysis Works
```{r}
# analysis of the Canon et al. (2006) data using (log) risk ratios
dat <- dat.cannon2006[,1:6] # keep only variables we really need
dat <- escalc(measure="RR", ai=ep1t, n1i=nt, ci=ep1c, n2i=nc, data=dat, slab=trial)
dat
res <- rma(yi, vi, data=dat, method="DL")
res
predict(res, transf=exp, digits=2) # summary effect size (risk ratio)
dat$weights <- paste0(round(weights(res)), "%") # weights in % (rounded)
dat$pvals <- round(summary(dat)$pval, digits=3) # p-values of the individual trials
```
```{r, figure_01_1, fig.width=8, fig.height=4, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 1.1
par(mar=c(4,4,2,2))
forest(res, xlim=c(-1,2), atransf=exp, at=log(c(2/3, 1, 3/2)),
header=TRUE, top=2, mlab="Summary", efac=c(0,1,3),
ilab=data.frame(dat$weights, dat$pvals), ilab.xpos=c(0.8,1.2), ilab.pos=2)
text(0.8, -1, "100%", pos=2)
text(1.2, -1, formatC(res$pval, format="f", digits=5), pos=2)
text(0.8, 6, "Weight", pos=2, font=2)
text(1.2, 6, "P-Value", pos=2, font=2)
```
***
## 2) Why Perform a Meta-Analysis
```{r}
# analysis of the Lau et al. (1992) data using (log) risk ratios
dat <- dat.lau1992
dat <- escalc(measure="RR", ai=ai, n1i=n1i, ci=ci, n2i=n2i, data=dat, slab=trial)
dat
res <- rma(yi, vi, data=dat, method="DL")
res
predict(res, transf=exp, digits=2) # summary effect size (risk ratio)
formatC(res$pval, format="f", digits=7) # p-value of the summary estimate
```
```{r, figure_02_1, fig.width=8, fig.height=7.8, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 2.1
par(mar=c(4,4,2,2))
forest(res, xlim=c(-10,9), atransf=exp, at=log(c(0.01, 0.1, 1, 10, 100)),
header=TRUE, top=2, ilab=year, ilab.xpos=-6, digits=3L, cex=0.8,
efac=c(0,1), fonts=c("sans", "inconsolata"))
# note: using a monospaced font for the annotations on the right, so they are
# fully aligned; if the "Inconsolata" font is not available, can also use "mono"
text(-6, 35, "Year", font=2, cex=0.8)
```
***
## 4) Effect Sizes Based on 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
# note: if the pre- and post-test means and SDs are not available, but we know
# the mean and SD of the change scores, then set m2i=0, sd2i=0, and ri=0
dat <- escalc("MC", m1i=5, m2i=0, sd1i=10, sd2i=0, ni=50, ri=0)
summary(dat)
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, vtype="LS2")
summary(dat)
# 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
# same equation as given in the book is used; but the difference is usually
# negligible (the results above differ slightly from those given in the book
# due to intermittent rounding in the book)
```
```{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, vtype="LS2")
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)
# 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
# same equation as given in the book is used; but the difference is usually
# negligible (the results above differ slightly from those given in the book
# because the equation given in the book actually contains a mistake)
```
```{r}
# response ratio (log transformed ratio of means)
dat <- escalc("ROM", m1i=61.515, m2i=51.015, sd1i=19.475, sd2i=19.475, n1i=10, n2i=10)
summary(dat)
```
***
## 5) Effect Sizes Based on Binary Data
```{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}
# risk difference
dat <- escalc("RD", ai=5, n1i=100, ci=10, n2i=100)
summary(dat)
```
***
## 6) Effect Sizes Based on Correlations
```{r}
# r-to-z transformed correlation coefficient
dat <- escalc("ZCOR", ri=0.50, ni=100)
summary(dat)
```
***
## 14) Worked Examples (Part 1)
### Example for Continuous Data
```{r}
# Table 14.1: Dataset 1
dat <- read.table(header=TRUE, text = "
study mean1 sd1 n1 mean2 sd2 n2
Carroll 94 22 60 92 20 60
Grant 98 21 65 92 22 65
Peck 98 28 40 88 26 40
Donat 94 19 200 82 17 200
Stewart 98 21 50 88 22 45
Young 96 21 85 92 22 85")
dat <- escalc("SMD", m1i=mean1, sd1i=sd1, n1i=n1, m2i=mean2, sd2i=sd2, n2i=n2,
slab=study, data=dat, vtype="LS2")
dat
```
```{r}
# equal-effects model analysis
res <- rma(yi, vi, data=dat, method="EE", digits=2)
res
```
Note: The book uses the term 'fixed-effect 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, figure_14_1, fig.width=8, fig.height=5, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 14.1
par(mar=c(4,4,2,2))
dat$weights <- paste0(round(weights(res)), "%") # weights in % (rounded)
forest(res, xlim=c(-2,3), header=TRUE, top=2, mlab="Summary",
ilab=dat$weights, ilab.xpos=1.6, ilab.pos=2, efac=c(0,1,1.5))
text(1.6, -1, "100%", pos=2)
text(1.6, 8, "Weight", pos=2, font=2)
```
```{r}
# random-effects model analysis
res <- rma(yi, vi, data=dat, method="DL", digits=2)
res
```
```{r, figure_14_2, fig.width=8, fig.height=5, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 14.2
par(mar=c(4,4,2,2))
dat$weights <- paste0(round(weights(res)), "%") # weights in % (rounded)
forest(res, xlim=c(-2,3), header=TRUE, top=2, mlab="Summary",
ilab=dat$weights, ilab.xpos=1.6, ilab.pos=2, efac=c(0,1,1.5))
text(1.6, -1, "100%", pos=2)
text(1.6, 8, "Weight", pos=2, font=2)
```
### Example for Binary Data
```{r}
# Table 14.4: Dataset 2
dat <- read.table(header=TRUE, text = "
study events1 nonevents1 n1 events2 nonevents2 n2
Saint 12 53 65 16 49 65
Kelly 8 32 40 10 30 40
Pilbeam 14 66 80 19 61 80
Lane 25 375 400 80 320 400
Wright 8 32 40 11 29 40
Day 16 49 65 18 47 65")
dat <- escalc("OR", ai=events1, n1i=n1, ci=events2, n2i=n2, slab=study, data=dat)
dat
```
```{r}
# equal-effects model analysis
res <- rma(yi, vi, data=dat, method="EE", digits=2)
res
predict(res, transf=exp)
```
```{r, figure_14_3, fig.width=8, fig.height=5, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 14.3
par(mar=c(4,4,2,2))
dat$weights <- paste0(round(weights(res)), "%") # weights in % (rounded)
forest(res, xlim=c(-3.5,5), header=TRUE, top=2, mlab="Summary",
atransf=exp, at=log(c(0.125, 0.25, 0.5, 1, 2, 4)), digits=c(2L,3L),
ilab=dat$weights, ilab.xpos=2.5, ilab.pos=2, efac=c(0,1,1.5))
text(2.5, -1, "100%", pos=2)
text(2.5, 8, "Weight", pos=2, font=2)
```
```{r}
# random-effects model analysis
res <- rma(yi, vi, data=dat, method="DL", digits=2)
res
```
```{r, figure_14_4, fig.width=8, fig.height=5, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 14.4
par(mar=c(4,4,2,2))
dat$weights <- paste0(round(weights(res)), "%") # weights in % (rounded)
forest(res, xlim=c(-3.5,5), header=TRUE, top=2, mlab="Summary",
atransf=exp, at=log(c(0.125, 0.25, 0.5, 1, 2, 4)), digits=c(2L,3L),
ilab=dat$weights, ilab.xpos=2.5, ilab.pos=2, efac=c(0,1,1.5))
text(2.5, -1, "100%", pos=2)
text(2.5, 8, "Weight", pos=2, font=2)
```
### Example for Correlational Data
```{r}
# Table 14.7: Dataset 3
dat <- read.table(header=TRUE, text = "
study correl n
Fonda 0.50 40
Newman 0.60 90
Grant 0.40 25
Granger 0.20 400
Milland 0.70 60
Finch 0.45 50")
dat <- escalc("ZCOR", ri=correl, ni=n, slab=study, data=dat)
dat
```
```{r}
# equal-effects model analysis
res <- rma(yi, vi, data=dat, method="EE", digits=2)
res
predict(res, transf=transf.ztor)
```
```{r, figure_14_5, fig.width=8, fig.height=5, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 14.5
par(mar=c(4,4,2,2))
dat$weights <- paste0(round(weights(res)), "%") # weights in % (rounded)
forest(res, xlim=c(-2,4), header=TRUE, top=2, mlab="Summary",
atransf=transf.ztor, at=transf.rtoz(c(-0.5, 0, 0.5, 0.85)),
ilab=dat$weights, ilab.xpos=2.2, ilab.pos=2, efac=c(0,1,1.5))
text(2.2, -1, "100%", pos=2)
text(2.2, 8, "Weight", pos=2, font=2)
```
```{r}
# random-effects model analysis
res <- rma(yi, vi, data=dat, method="DL", digits=2)
res
predict(res, transf=transf.ztor)
```
```{r, figure_14_6, fig.width=8, fig.height=5, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 14.6
par(mar=c(4,4,2,2))
dat$weights <- paste0(round(weights(res)), "%") # weights in % (rounded)
forest(res, xlim=c(-2,4), header=TRUE, top=2, mlab="Summary",
atransf=transf.ztor, at=transf.rtoz(c(-0.5, 0, 0.5, 0.85)),
ilab=dat$weights, ilab.xpos=2.2, ilab.pos=2, efac=c(0,1,1.5))
text(2.2, -1, "100%", pos=2)
text(2.2, 8, "Weight", pos=2, font=2)
```
***
## 18) Worked Examples (Part 2)
### Notes
1. The prediction interval is computed in a slightly different way in the book compared to how this is done (by default) in the `metafor` package. For more details, see [here](https://www.metafor-project.org/doku.php/faq#for_random-effects_models_fitt). However, by using argument `pi.type="Riley"`, we can obtain the same results as those provided in the book.
2. The confidence intervals for $\tau^2$ and $I^2$ are also computed in a different way. By default, the `metafor` package uses the Q-profile method, which is an exact method (under the assumptions of the model), while the methods described in the book are based on large-sample approximations. However, we can still obtain the latter by using argument `type="HT"`.
### Example for Continuous Data
```{r}
# Table 14.1: Dataset 1
dat <- read.table(header=TRUE, text = "
study mean1 sd1 n1 mean2 sd2 n2
Carroll 94 22 60 92 20 60
Grant 98 21 65 92 22 65
Peck 98 28 40 88 26 40
Donat 94 19 200 82 17 200
Stewart 98 21 50 88 22 45
Young 96 21 85 92 22 85")
dat <- escalc("SMD", m1i=mean1, sd1i=sd1, n1i=n1, m2i=mean2, sd2i=sd2, n2i=n2,
slab=study, data=dat, vtype="LS2")
dat
```
```{r}
# random-effects model analysis
res <- rma(yi, vi, data=dat, method="DL")
res
predict(res, pi.type="Riley")
confint(res, type="HT")
```
```{r, figure_18_1, fig.width=8, fig.height=5, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 18.1
par(mar=c(4,4,2,2))
dat$weights <- paste0(round(weights(res)), "%") # weights in % (rounded)
forest(res, xlim=c(-2,3), header=TRUE, top=2, mlab="Summary",
ilab=dat$weights, ilab.xpos=1.6, ilab.pos=2, efac=c(0,1,1.5),
addpred=TRUE, pi.type="Riley")
text(1.6, -1, "100%", pos=2)
text(1.6, 8, "Weight", pos=2, font=2)
```
### Example for Binary Data
```{r}
# Table 14.4: Dataset 2
dat <- read.table(header=TRUE, text = "
study events1 nonevents1 n1 events2 nonevents2 n2
Saint 12 53 65 16 49 65
Kelly 8 32 40 10 30 40
Pilbeam 14 66 80 19 61 80
Lane 25 375 400 80 320 400
Wright 8 32 40 11 29 40
Day 16 49 65 18 47 65")
dat <- escalc("OR", ai=events1, n1i=n1, ci=events2, n2i=n2, slab=study, data=dat)
dat
```
```{r}
# random-effects model analysis
res <- rma(yi, vi, data=dat, method="DL")
res
predict(res, transf=exp, pi.type="Riley")
confint(res, type="HT")
```
```{r, figure_18_2, fig.width=8, fig.height=5, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 18.2
par(mar=c(4,4,2,2))
dat$weights <- paste0(round(weights(res)), "%") # weights in % (rounded)
forest(res, xlim=c(-3.5,5), header=TRUE, top=2, mlab="Summary",
atransf=exp, at=log(c(0.125, 0.25, 0.5, 1, 2, 4)), digits=c(2L,3L),
ilab=dat$weights, ilab.xpos=2.5, ilab.pos=2, efac=c(0,1,1.5),
addpred=TRUE, pi.type="Riley")
text(2.5, -1, "100%", pos=2)
text(2.5, 8, "Weight", pos=2, font=2)
```
### Example for Correlational Data
```{r}
# Table 14.7: Dataset 3
dat <- read.table(header=TRUE, text = "
study correl n
Fonda 0.50 40
Newman 0.60 90
Grant 0.40 25
Granger 0.20 400
Milland 0.70 60
Finch 0.45 50")
dat <- escalc("ZCOR", ri=correl, ni=n, slab=study, data=dat)
dat
```
```{r}
# random-effects model analysis
res <- rma(yi, vi, data=dat, method="DL")
res
predict(res, transf=transf.ztor, pi.type="Riley")
confint(res, type="HT")
```
```{r, figure_18_3, fig.width=8, fig.height=5, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 18.3
par(mar=c(4,4,2,2))
dat$weights <- paste0(round(weights(res)), "%") # weights in % (rounded)
forest(res, xlim=c(-2,4), header=TRUE, top=2, mlab="Summary",
atransf=transf.ztor, at=transf.rtoz(c(-0.5, 0, 0.5, 0.9)),
ilab=dat$weights, ilab.xpos=2.2, ilab.pos=2, efac=c(0,1,1.5),
addpred=TRUE, pi.type="Riley")
text(2.2, -1, "100%", pos=2)
text(2.2, 8, "Weight", pos=2, font=2)
```
***
## 19) Subgroup Analyses
```{r}
dat <- read.table(header=TRUE, text = "
study type g var
Thornhill A 0.110 0.010
Kendall A 0.224 0.030
Vandamm A 0.338 0.020
Leonard A 0.451 0.015
Professor A 0.480 0.010
Jeffries B 0.440 0.015
Fremont B 0.492 0.020
Doyle B 0.651 0.015
Stella B 0.710 0.025
Thorwald B 0.740 0.012")
dat
```
### EE models within subgroups
```{r}
# equal-effects model for subgroup A
resA <- rma(g, var, data=dat, method="EE", subset=type=="A")
resA
# equal-effects model for subgroup B
resB <- rma(g, var, data=dat, method="EE", subset=type=="B")
resB
```
```{r}
# equal-effects model for all 10 studies
res <- rma(g, var, data=dat, method="EE", slab=study)
res
```
```{r, figure_19_1, fig.width=8, fig.height=6, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 19.1
par(mar=c(4,4,2,2))
forest(res, xlim=c(-1,2), cex=1, refline=0.5,
header=c("Type / Study", "SMD [95% CI]"),
xlab="Standardized Mean Difference", mlab="Combined",
at=seq(-0.2, 1.2, by=0.2), efac=c(0,1,1.5),
rows=c(3:7, 12:16), ylim=c(-1.2,21))
text(-1, 17.5, "Type A", pos=4, font=2)
text(-1, 8.5, "Type B", pos=4, font=2)
addpoly(resA, row=10.5, mlab="Combined")
addpoly(resB, row= 1.5, mlab="Combined")
```
```{r}
# Table 19.2
sav <- list(resA, resB, res)
tab <- rbind(sapply(sav, function(x) coef(x)),
sapply(sav, function(x) vcov(x)),
sapply(sav, function(x) x$se),
sapply(sav, function(x) x$ci.lb),
sapply(sav, function(x) x$ci.ub),
sapply(sav, function(x) x$zval),
sapply(sav, function(x) x$pval),
sapply(sav, function(x) x$QE),
sapply(sav, function(x) x$k-1),
sapply(sav, function(x) x$QEp),
sapply(sav, function(x) x$I2))
colnames(tab) <- c("A", "B", "Combined")
rownames(tab) <- c("Y", "V", "SE", "LL", "UB", "Z", "pval", "Q", "df", "pval", "I2")
round(tab, digits=4)
```
```{r}
# compare effects of A and B
dif <- rma(g, var, mods = ~ type, method="FE", data=dat)
dif
# note: the estimate for 'typeB' is the estimated difference; the values
# for zval and pval are the test of the difference between A and B
```
```{r}
# Table 19.3
tab <- data.frame(Q = c(resA$QE, resB$QE, dif$QE, dif$QM, res$QE),
df = c(resA$k-1, resB$k-1, dif$k-dif$p, dif$m, res$k-1),
p = c(resA$QEp, resB$QEp, dif$QEp, dif$QMp, res$QEp))
rownames(tab) <- c("A", "B", "Within", "Between", "Total")
round(tab, digits=4)
```
### RE models with separate $\tau^2$ estimates
```{r}
# random-effect model within subgroup A
resA <- rma(g, var, data=dat, method="DL", subset=type=="A")
resA
# random-effect model within subgroup B
resB <- rma(g, var, data=dat, method="DL", subset=type=="B")
resB
# note: this allows for a different tau^2 value within the two subgroups
```
```{r}
# random-effect model for all 10 studies allowing for different tau^2 values within subgroups
# note: rma.mv() uses ML/REML estimation; here, we pass the tau^2 values as
# estimated by the DL method from the two subgroup models to the function
res <- rma.mv(g, var, random = ~ type | study, struct="DIAG",
data=dat, slab=study, tau2=c(resA$tau2, resB$tau2))
res
```
```{r, figure_19_5, fig.width=8, fig.height=6, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 19.5
par(mar=c(4,4,2,2))
forest(res, xlim=c(-1,2), cex=1, refline=0.5,
header=c("Type / Study", "SMD [95% CI]"),
xlab="Standardized Mean Difference", mlab="Combined",
at=seq(-0.2, 1.2, by=0.2), efac=c(0,1,1.5),
rows=c(3:7, 12:16), ylim=c(-1.2,21))
text(-1, 17.5, "Type A", pos=4, font=2)
text(-1, 8.5, "Type B", pos=4, font=2)
addpoly(resA, row=10.5, mlab="Combined")
addpoly(resB, row= 1.5, mlab="Combined")
```
```{r}
# Table 19.6
sav <- list(resA, resB, res)
tab <- rbind(sapply(sav, function(x) coef(x)),
sapply(sav, function(x) vcov(x)),
sapply(sav, function(x) x$se),
sapply(sav, function(x) x$ci.lb),
sapply(sav, function(x) x$ci.ub),
sapply(sav, function(x) x$zval),
sapply(sav, function(x) x$pval))
colnames(tab) <- c("A", "B", "Combined")
rownames(tab) <- c("Y", "V", "SE", "LL", "UB", "Z", "pval")
round(tab, digits=4)
# note: the Q-statistics in the table shown in the book are not traditional Q-test
# statistics, but incorporate the random-effects model weights (not shown here)
```
```{r}
# compare effects of A and B
dif <- rma.mv(g, var, mods = ~ type, random = ~ type | study, struct="DIAG",
data=dat, slab=study, tau2=c(resA$tau2, resB$tau2))
dif
# note: the estimate for 'typeB' is the estimated difference; the values
# for zval and pval are the test of the difference between A and B
# note: for the decomposition of the Q-statistics to work as shown in Table
# 19.7, one must compute these statistics incorporating the random-effects model
# weights; this gets confusing really quickly, because that's not how the
# Q-statistics for heterogeneity are typically computed, so I'll skip this part;
# however, note that the QM-test in the output above is the Q_Between test shown
# in the table and this is what we are interested in anyway
```
### RE models with pooled $\tau^2$ estimate
```{r}
# mixed-effect meta-regression model assuming a common tau^2 value for the 2 subgroups
dif <- rma(g, var, mods = ~ type - 1, data=dat, method="DL", slab=study)
dif
# note: fitted model without intercept so the coefficients are the estimated effects for A and B
```
```{r}
# random-effect model for all 10 studies setting tau^2 to the value from the model above
res <- rma(g, var, data=dat, tau2=dif$tau2, slab=study)
res
```
```{r, figure_19_8, fig.width=8, fig.height=6, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 19.8
par(mar=c(4,4,2,2))
forest(res, xlim=c(-1,2), cex=1, refline=0.5,
header=c("Type / Study", "SMD [95% CI]"),
xlab="Standardized Mean Difference", mlab="Combined",
at=seq(-0.2, 1.2, by=0.2), efac=c(0,1,1.5),
rows=c(3:7, 12:16), ylim=c(-1.2,21))
text(-1, 17.5, "Type A", pos=4, font=2)
text(-1, 8.5, "Type B", pos=4, font=2)
addpoly(coef(dif)[1], vcov(dif)[1,1], row=10.5, mlab="Combined")
addpoly(coef(dif)[2], vcov(dif)[2,2], row= 1.5, mlab="Combined")
```
```{r}
# Table 19.11
tab <- rbind(c(coef(dif)[1], coef(dif)[2], coef(res)),
c(vcov(dif)[1,1], vcov(dif)[2,2], vcov(res)),
c(dif$se[1], dif$se[2], res$se),
c(dif$ci.lb[1], dif$ci.lb[2], res$ci.lb),
c(dif$ci.ub[1], dif$ci.ub[2], res$ci.ub),
c(dif$zval[1], dif$zval[2], res$zval),
c(dif$pval[1], dif$pval[2], res$pval))
colnames(tab) <- c("A", "B", "Combined")
rownames(tab) <- c("Y", "V", "SE", "LL", "UB", "Z", "pval")
round(tab, digits=4)
# note: the Q-statistics in the table shown in the book are not traditional Q-test
# statistics, but incorporate the random-effects model weights (not shown here)
```
```{r}
# compare effects of A and B
dif <- rma(g, var, mods = ~ type, data=dat, method="DL", slab=study)
dif
# note: the estimate for 'typeB' is the estimated difference; the values
# for zval and pval are the test of the difference between A and B
```
***
## 20) Meta-Regression
```{r}
# compute log risk ratios for the BCG vaccine data
dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg,
slab=paste(author, year, sep=", "))
dat
```
```{r}
# equal-effects model
res <- rma(yi, vi, data=dat, method="EE")
res
```
```{r, figure_20_1, fig.width=8, fig.height=5.8, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 20.1
par(mar=c(4,4,2,2))
forest(res, xlim=c(-8,7), atransf=exp, at=log(c(0.1, 1, 10)), digits=3L, cex=0.8,
header=TRUE, top=2, mlab="Summary", efac=c(0,1,1.5), order="obs")
```
```{r}
# meta-regression model with absolute latitude
reg <- rma(yi, vi, mods = ~ ablat, data=dat, method="FE", digits=5)
reg
```
```{r}
# Table 20.3
tab <- data.frame(Q = c(reg$QM, reg$QE, res$QE),
df = c(reg$m, reg$k-reg$m, res$k-1),
pval = c(reg$QMp, reg$QEp, res$QEp))
rownames(tab) <- c("Model (Q_model)", "Residual (Q_resid)", "Total (Q)")
round(tab, digits=5)
```
```{r, figure_20_2, fig.width=8, fig.height=6.5, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 20.2
regplot(reg, xlim=c(0,70), ylim=c(-3,1), xlab="Latitude", ylab="ln(RR)", las=1,
pch=1, plim=c(NA,8), ci=FALSE)
title("Regression of log risk ratio on latitude (fixed-effect)")
```
```{r}
# random-effects model
res <- rma(yi, vi, data=dat, method="DL")
res
predict(res, transf=exp, digits=2)
```
```{r, figure_20_5, fig.width=8, fig.height=5.8, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 20.5
par(mar=c(4,4,2,2))
forest(res, xlim=c(-8,7), atransf=exp, at=log(c(0.1, 1, 10)), digits=3L, cex=0.8,
header=TRUE, top=2, mlab="Summary", efac=c(0,1,1.5), order="obs")
```
```{r}
# mixed-effects meta-regression model with absolute latitude
reg <- rma(yi, vi, mods = ~ ablat, data=dat, method="DL", digits=5)
reg
```
```{r, figure_20_6, fig.width=8, fig.height=6.5, dev.args=list(pointsize=pointsize), fig.align='center'}
# Figure 20.6
regplot(reg, xlim=c(0,70), ylim=c(-3,1), xlab="Latitude", ylab="ln(RR)", las=1,
pch=1, plim=c(NA,8), ci=FALSE)
title("Regression of log risk ratio on latitude (random-effect)")
```
***
## 21) Subgrouping / Meta-Regression
The following results are not actually shown in the chapter. They are provided here to show how these types of analyses can be conducted using the `metafor` package.
```{r}
dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg)
dat
```
```{r}
# random-effects model with Knapp-Hartung method
res <- rma(yi, vi, data=dat, method="DL", test="knha")
res
```
```{r}
# mixed-effects meta-regression model with Knapp-Hartung method
res <- rma(yi, vi, mods = ~ ablat, data=dat, method="DL", test="knha")
res
```
```{r, eval=runperm}
# random-effects model with permutation test
res <- rma(yi, vi, data=dat, method="DL")
permutest(res, exact=TRUE)
```
```{r, eval=runperm}
# mixed-effects meta-regression model with permutation test
# note: an exact permutation test would require 389188800 iterations, which will
# take forever; instead, we can use a large number of random permutations; for
# reproducibility, we set the 'seed' of the random number generator beforehand
res <- rma(yi, vi, mods = ~ ablat, data=dat, method="DL")
set.seed(1234)
permutest(res)
```
***
## 23) Subgroups within Studies
```{r}
# Table 23.1: Independent subgroups – five fictional studies
dat <- read.table(header=TRUE, text = "
study subgroup es var
'Study 1' 'Stage 1' 0.3 0.05
'Study 1' 'Stage 2' 0.1 0.05
'Study 2' 'Stage 1' 0.2 0.02
'Study 2' 'Stage 2' 0.1 0.02
'Study 3' 'Stage 1' 0.4 0.05
'Study 3' 'Stage 2' 0.2 0.05
'Study 4' 'Stage 1' 0.2 0.01
'Study 4' 'Stage 2' 0.1 0.01
'Study 5' 'Stage 1' 0.4 0.06
'Study 5' 'Stage 2' 0.3 0.06
")
dat
```
```{r}
# treat each subgroup as a separate study
res <- rma(es, var, data=dat, method="EE")
res
```
```{r}
# compute combined effect across subgroups within studies
dat <- escalc(yi=es, vi=var, data=dat, var.names=c("es","var"))
agg <- aggregate(dat, cluster=study, struct="ID", select=c("study","es","var"))
agg
```
```{r}
# use study as the unit of analysis
res <- rma(es, var, data=agg, method="EE")
res
```
***
## 24) Multiple Outcomes / Time-Points
```{r}
# Table 24.1: Multiple outcomes – five fictional studies
dat <- read.table(header=TRUE, text = "
study outcome es var cor
'Study 1' Reading 0.3 0.05 0.5
'Study 1' Math 0.1 0.05 0.5
'Study 2' Reading 0.2 0.02 0.6
'Study 2' Math 0.1 0.02 0.6
'Study 3' Reading 0.4 0.05 0.6
'Study 3' Math 0.2 0.05 0.6