-
Notifications
You must be signed in to change notification settings - Fork 0
/
Supplementary_Code_Documentation_B_Model_Selection_and_Diagnostics.Rmd
2582 lines (2132 loc) · 97 KB
/
Supplementary_Code_Documentation_B_Model_Selection_and_Diagnostics.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: "Coral Heritability Meta-analysis"
subtitle: "Supplementary Code Documentation B: Model Selection and Diagnostics"
author: "Kevin R Bairos-Novak"
date: "Document last run on `r format(Sys.time(), '%Y-%m-%d')`"
output:
html_document:
df_print: kable
toc: yes
highlight: tango
theme: cerulean
number_sections: no
fig_width: 10
fig_height: 8
fig_caption: yes
code_folding: hide
self_contained: TRUE
keep_md: yes
editor_options:
chunk_output_type: console
knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding, output_file = paste0(xfun::sans_ext(inputFile),"_",format(Sys.time(), '%Y-%m-%d'), ".html")) })
---
```{r, include=F}
knitr::opts_chunk$set(echo = TRUE, comment="",
message=F, warning=F)
patt <- "Supplementary_Code_Documentation_B_Model_Selection_and_Diagnostics"
x <- paste0(patt,"_",format(Sys.time(), '%Y-%m-%d'), ".html")
y <- list.files(pattern = glob2rx(paste0(patt,"*.html")))
y <- y[!(y == x)]
unlink(y, recursive=T)
setwd("~/Documents/PhD Thesis/Heritability meta-analysis")
myAIC <- function(..., aic=F, aicc=T, waic=F,
delta_aic=F, delta_aicc=T, pvals=F,
getmodel=F, param.list = NULL){
require(tidyverse)
if(getmodel==T){
mod.name <- as.character(list(...))
mod.out <- list(...)
mod.out <- lapply(mod.out, function(x) get(x))
} else {
mod.name <- eval(substitute(alist(...))) %>% as.character
mod.out <- list(...)
}
# add additional optional arguments later...
dfs <- mod.out %>% map(anova) %>% map_dbl("m")
AICs <- mod.out %>% map_dbl(AIC)
AICcs <- mod.out %>% map_dbl(AIC, correct=T)
p <- mod.out %>% map(anova) %>% map_dbl("QMp")
out <- data.frame(model=mod.name, df=dfs, AIC=AICs, AICc=AICcs, p=p) %>%
mutate(ΔAIC = AIC - min(AIC),
ΔAICc = AICc - min(AICc),
p = format(p, scientific=F, digits=2),
expAICc = exp(-0.5*ΔAICc),
wAICc = expAICc/max(expAICc),
expAICc = NULL) %>%
relocate(ΔAIC, .after="AIC") %>%
relocate(ΔAICc, .after="AICc") %>%
rename("Overall p-val"=p) %>%
arrange(AICc)
if(delta_aic==T) {out <- out %>% arrange(AIC)}
if(!is.null(param.list)) {
possible.terms <- paste("x ~",paste(param.list, collapse="*")) %>%
map(as.formula) %>% map(terms.formula) %>%
map(~attr(.x, "term.labels")) %>% unlist
chars <- mod.out %>% map("formula.yi") %>% as.character()
terms <- chars %>% map(as.formula) %>% map(terms.formula) %>%
map(~attr(.x, "term.labels"))
x <- matrix(nrow=0, ncol=length(possible.terms))
for (i in 1:length(mod.name)){
x <- rbind(x, possible.terms %in% terms[[i]] %>% as.numeric)
}
i.x <- colSums(x)>0
x <- as.data.frame(x[,i.x]) * out$wAICc
colnames(x) <- possible.terms[i.x]
rel.importance <- x %>% summarise_all(sum) %>%
pivot_longer(everything()) %>%
mutate(norm_val = value/max(value)) %>%
arrange(-norm_val)
}
if(aic == F) {out <- out %>% select(-AIC)}
if(aicc== F) {out <- out %>% select(-AICc)}
if(delta_aic==F) {out <- out %>% select(-ΔAIC)}
if(delta_aicc==F) {out <- out %>% select(-ΔAICc)}
if(waic==F) {out <- out %>% select(-wAICc)}
if(pvals==F) {out <- out %>% select(-`Overall p-val`)}
if(!is.null(param.list)) {
out <- list(AICtable=out, rel.importance=rel.importance)
}
out
}
```
```{=html}
<style>
.container { width: 1000px; }
h2 { color: #f8f8f8; background-color: #437FAA; }
h3 { color: #f8f8f8; background-color: #437FAA; text-align: center; }
<!-- Get highlight from Terminal: pandoc --print-highlight-style tango -->
</style>
```
# Start
```{r, class.source = 'fold-show'}
# rm(list=ls())
setwd("~/Documents/PhD Thesis/Heritability meta-analysis")
library(readxl)
library(tidyverse); theme_set(theme_light())
library(metafor)
library(patchwork)
source('Functions/useful_functions.R')
source('Functions/mlm.variance.distribution.R')
# Load processed data:
load("Data/heritability_estimates_processed.RData")
# data.full <- data.full %>% filter(!(study == "Zhang et al.")) # duplicate estimate!
data <- data.full %>%
filter(trait != "gamete contribution") %>%
mutate(trait = fct_drop(trait))
dat_s <- data %>%
filter(!(trait %in% c("photochemistry", "immune response"))) %>%
mutate(trait = factor(trait))
dat_sh <- data %>%
filter(!(trait %in% c("photochemistry", "nutrient content", "immune response"))) %>%
mutate(trait = factor(trait))
dat_shg <- data %>%
filter(trait %in% c("symbiont community", "survival", "nutrient content", "growth", "bleaching")) %>%
filter(!(growth.form %in% c("columnar", "encrusting"))) %>%
mutate(trait = factor(trait), growth.form = factor(growth.form))
dat_tm <- data %>%
filter(!is.na(temp.diff)) %>%
filter(!(trait %in% c("symbiont community", "morphology", "gene expression"))) %>%
mutate(trait = factor(trait),
temp.manip = temp.manip.plus.others,
temp.s = sqrt(temp.diff))
dat_tm_nz <- dat_tm %>% filter(temp.diff >0) %>%
mutate(trait = factor(trait),
stage = factor(stage),
h2 = factor(h2),
growth.form = factor(growth.form))
# Specify final main model
final.model.A <- rma(yi=val.log ~ trait*stage + h2,
vi=sv.log, data=dat_s, method="REML", test="knha")
```
------------------------------------------------------------------------
# Summary of results
When defining the base model architecture that includes all data (save
for one estimate on the heritability of gamete compatibility), the
optimal and most parsimonious explanatory variable was the trait type.
In order of increasing relative heritability (not accounting for
differences in heritability type):
- gene expression
- photochemistry
- growth
- nutrient content
- bleaching
- morphology
- symbiont community
- immune response
- survival
In sub-analyses using smaller data subsets, life stage, heritability type, and growth form
came out important, whereas temperature manipulation
(binary yes/no), temperature differential (°C above
normal/ambient/control temperature), and symbiont type (symbiotic or
asymbiotic) were not.
An overall model of trait type, and a sub-analysis model of trait type x life stage + heritability type was preferred through model selection. No other models were found.
# Overview of sample sizes
Narrow-sense heritability is reported for 10 studies, broad-sense for 10
studies, including one study reporting broad-sense and narrow-sense
estimates (Carlon et al. 2011).
```{r}
table.plot("h2", "study", data.full) # only Carlon et al. study with multiple h2 values
```
```{r, eval=F}
summarise.coverage("trait", "study", data.full)
```
Most studies (12/19) report on a single trait type and not multiple
trait types. Three studies report on two trait types (Meyer et al. 2009;
Manzello et al. 2019; Kenkel et al. 2015), two studies report on three
trait types (Csázár et al. 2010; Quigley et al. 2020), and Zhang et al.
(2019) and Wright et al. (2019) report on four and six trait types,
respectively.
Both immune response and gamete contribution trait types have only a
single study contributing to their estimates. In the case of the latter,
gamete contribution has only one estimate, limiting any inferences using
models.
```{r, eval=F}
table.plot("trait", "species", data.full)
data.full %>% group_by(species) %>% tally %>% arrange(-n)
summarise.coverage("species", "study", data.full)$Y
```
Most heritability estimates are from Acropora millepora (48 estimates; 5
separate studies), A. spathulata (12; 1), Porites astreoides (9; 2),
Fabia fragum (6; 1), Orbicella faveolata (4; 3), and A. cervicornis (3;
2).
Most diversity of species/studies are covered by trait types such as
growth, survival, bleaching, and symbiont community.
# Model Selection Process
## Complete Dataset
Following the process of mixed model selection outlined by Zuur et al.
(Mixed Effects Modelling Book, 2009, pp. 121-122) and Diggle et al.
(2002), we:
1. Begin with a 'beyond optimal' model with as many fixed effects as
possible, given the available data
2. With the beyond optimal fixed effects, test for the optimal random
effects structure by comparing AIC among models fit using REML
3. Using the optimal random effects structure, compare fixed effects by
comparing AIC among models fit using ML
4. Refit the final mixed effects model using REML
5. Check final model diagnostics: funnel plots, fail safe number, Cook's distances, residual plots, and simulated data
### Step 1: Begin with a *beyond optimal* model
We have 4 main factors that cover the complete dataset:
- trait type (9 levels, including survival, growth, bleaching, etc.)
- heritability type (broad- or narrow-sense)
- coral life stage (larval, juvenile, adult)
- coral growth form (3 main types: corymbose, branching, massive; also
encrusting and columnar)
Thus, our beyond optimal model is one that includes the main effects of
all 4 factors (additive model). As seen later on, we cannot examine
interactions, given factor combinations being missing For example,
heritability estimates for narrow-sense heritability exist only for 6/9
trait types, so no interaction between trait type x heritability type
can be properly examined without removing the 3 trait types with only
broad-sense heritability estimates.
```{r, eval=F}
summarise.coverage("trait", "h2", data)$X # 9 broad, 6 narrow
```
### Step 2: Select random effects structure
Next, we fit different random effect structures, including nested
(3-level) models that include either study ID or species ID as an upper
level, with estimate ID nested within (sampling variance acts as the
3rd, most basal level in a meta-model). We also fit similar models with
one or both of study/species ID and estimate ID fixed at zero, as well
as a 'null' random effects model, where all random effects are fit at
zero (essentially a fixed effects model).
```{r}
m.study.nested <- rma.mv(yi=val.log ~ trait + h2 + growth.form + stage,
V=sv.log, random = ~1|study/est.id,
data=data, method="REML", tdist=T,
sigma2 = c(NA, NA))
m.spp.nested <- rma.mv(yi=val.log ~ trait + h2 + growth.form + stage,
V=sv.log, random = ~1|species/est.id,
data=data, method="REML", tdist=T,
sigma2 = c(NA, NA))
m.only.est <- rma.mv(yi=val.log ~ trait + h2 + growth.form + stage,
V=sv.log, random = ~1|study/est.id,
data=data, method="REML", tdist=T,
sigma2 = c(0, NA)) # same as random = ~1|est.id
m.only.study <- rma.mv(yi=val.log ~ trait + h2 + growth.form + stage,
V=sv.log, random = ~1|study/est.id,
data=data, method="REML", tdist=T,
sigma2 = c(NA, 0)) # same as random = ~1|study
m.only.spp <- rma.mv(yi=val.log ~ trait + h2 + growth.form + stage,
V=sv.log, random = ~1|species/est.id,
data=data, method="REML", tdist=T,
sigma2 = c(NA, 0)) # same as random = ~1|species
m.no.reff <- rma.mv(yi=val.log ~ trait + h2 + growth.form + stage,
V=sv.log, random = ~1|study/est.id,
data=data, method="REML", tdist=T,
sigma2 = c(0, 0)) # no random effects
TableS1 <- myAIC(m.study.nested, m.spp.nested, m.only.est, m.only.study, m.only.spp, m.no.reff) %>%
mutate(model = fct_recode(model,
"Estimate ID nested in study ID" = "m.study.nested",
"Estimate ID nested in species" = "m.spp.nested",
"Study ID only" = "m.only.study",
"Species only" = "m.only.spp",
"Estimate ID only" = "m.only.est",
"No random effect" = "m.no.reff"))
write.csv(TableS1, file="Suppl Tables//TableS1.csv", row.names=F)
TableS1
```
Here, we see that the model with estimate ID nested within study ID is
preferred (`random=~1|study/est.id, sigma2 = c(NA,NA)`), since it has
the lowest relative AICc value.
Thus, we continue on with this random effects structure when deciding
our fixed effects structure.
### Step 3: Select fixed effects structure
Next, we select the optimal by comparing model AICc values of all
possible fixed-effect models for factors that exist for the complete
dataset:
- trait type (*t*; 9 levels: survival, growth, gene expression, etc.)
- heritability type (*h*; 2 levels: $h^2$ or $H^2$)
- coral growth form (*g*; 4 levels: branching, massive/columnar,
corymbose, or encrusting)
- coral life stage (*s*; 3 levels: larval, juvenile, adult)
We also have a factor of symbiont type (symbiotic or asymbiotic),
however, there are only 4 estimates with asymbiotic individuals, and
these all involve coral larvae from broadcast-spawners. Thus, there are
too few estimates and too little coverage to compare at this stage.
Similarly, temperature was not manipulated for a few of the studies, and
thus is not assessed here either.
We fit all combinations of additive factors possible, excluding any
interactions at this point, as the data are not complete for a number of
factor combinations. We display only the top 10 models by AICc (given it
is different in all models to AIC, our N sample size is too small for
the k model parameters being estimated!).
Again, because the data are not complete for interactions at this stage,
we examine only additive models with the various factors.
```{r}
dat <- data %>% mutate(t = trait, h = h2, g = growth.form, s = stage)
mod_names <- ls(pattern = "m.fixed.")
do.call("rm", as.list(mod_names))
# One-ways with less and less other terms:
m.fixed.1way.t.h.g.s <- rma.mv(yi=val.log ~ t + h + g + s,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.t.h.g <- rma.mv(yi=val.log ~ t + h + g,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.t.h.s <- rma.mv(yi=val.log ~ t + h + s,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.t.g.s <- rma.mv(yi=val.log ~ t + g + s,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.h.g.s <- rma.mv(yi=val.log ~ h + g + s,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.t.h <- rma.mv(yi=val.log ~ t + h,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.t.g <- rma.mv(yi=val.log ~ t + g,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.t.s <- rma.mv(yi=val.log ~ t + s,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.h.g <- rma.mv(yi=val.log ~ h + g,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.h.s <- rma.mv(yi=val.log ~ h + s,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.g.s <- rma.mv(yi=val.log ~ g + s,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.t <- rma.mv(yi=val.log ~ t,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.h <- rma.mv(yi=val.log ~ h,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.g <- rma.mv(yi=val.log ~ g,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.s <- rma.mv(yi=val.log ~ s,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
m.fixed.1way.null <- rma.mv(yi=val.log ~ 1,
random = ~1|study/est.id, V=sv.log,
data=dat, method="ML", tdist=T)
mod_names <- ls(pattern = "m.fixed")
param_list=c("t", "h", "g", "s")
table1 <- do.call(myAIC, as.list(c(mod_names, getmodel=T,
param.list = list(param_list))))
# table1$rel.importance
AICt <- table1$AICtable %>% filter(model == "m.fixed.1way.t") %>% pull(4) %>% round(2)
AICth <- table1$AICtable %>% filter(model == "m.fixed.1way.t.h") %>% pull(4) %>% round(2)
AICts <- table1$AICtable %>% filter(model == "m.fixed.1way.t.s") %>% pull(4) %>% round(2)
AICths <- table1$AICtable %>% filter(model == "m.fixed.1way.t.h.s") %>% pull(4) %>% round(2)
# head(table1$AICtable)
TableS2 <- head(table1$AICtable) %>%
mutate(model = fct_recode(model,
"trait" = "m.fixed.1way.t",
"trait + heritability type" = "m.fixed.1way.t.h",
"trait + life stage" = "m.fixed.1way.t.s",
"trait + heritability type + life stage" = "m.fixed.1way.t.h.s",
"trait + growth form + life stage" = "m.fixed.1way.t.g.s",
"trait + growth form" = "m.fixed.1way.t.g"))
write.csv(TableS2, file="Suppl Tables/TableS2.csv", row.names=F)
TableS2
```
The model with the lowest AICc is one with only trait type as a fixed
effect, followed closely (within ΔAICc \< 2) by a model of trait + life stage (ΔAICc =
`r AICts`), and trait +
heritability type (ΔAICc = `r AICth`), and finally, trait + heritability + stage (ΔAICc =
`r AICths`). and finally a few models including life stage,
heritability, and trait type. However, clearly, there are data
limitations present that limit the number of model parameters possible.
Thus, we accept trait type by itself as the core model for the complete
dataset.
```{r}
rbind(
data.frame(
Model = rep(paste0("~ trait (ΔAICc=", AICt,")")),
Parameter = names(coef(m.fixed.1way.t)),
Estimate = coef(m.fixed.1way.t),
SE = m.fixed.1way.t$se),
data.frame(
Model = rep(paste0("~ trait + life stage (ΔAICc=", AICts,")")),
Parameter = names(coef(m.fixed.1way.t.s)),
Estimate = coef(m.fixed.1way.t.s),
SE = m.fixed.1way.t.s$se),
data.frame(
Model = rep(paste0("~ trait + h2 (ΔAICc=", AICth,")")),
Parameter = names(coef(m.fixed.1way.t.h)),
Estimate = coef(m.fixed.1way.t.h),
SE = m.fixed.1way.t.h$se),
data.frame(
Model = rep(paste0("~ trait + h2 + life stage (ΔAICc=", AICths,")")),
Parameter = names(coef(m.fixed.1way.t.h.s)),
Estimate = coef(m.fixed.1way.t.h.s),
SE = m.fixed.1way.t.h.s$se)) %>%
mutate(Parameter = factor(Parameter),
Parameter = fct_relevel(Parameter, "intrcpt")) %>%
ggplot(aes(x=Estimate, y=Parameter, color=Model)) +
geom_vline(xintercept=0, linetype="dashed") +
geom_pointrange(aes(xmin = Estimate - SE, xmax = Estimate + SE), position = position_dodge(width=0.3)) +
labs(x="Parameter estimate (log[X+0.2] scale) ± SE") +
theme(legend.position="top", legend.direction="vertical")
```
The plot shows relatively similar coefficient estimates and uncertainty
in the top 4 models. However, we go with the best model of only trait
type as a predictor.
### Step 4: Fit final model with REML
Due to the small effect size of life stage in the trait + life stage model, and the nearly identical AICc value (difference of 0.15), we fit our final model using trait type only as:
$$ logit(h^2 + 0.2) \sim N(\mu, \sigma^2) \\
\mu = trait_k \, + \, \epsilon_{i} $$
where $\sigma^2$ are the estimated amount of heterogeneity in the
studies/estimates, calculated using an inverse variance weighting method
via package `metafor` that accounts for uncertainty in each estimate,
and $\epsilon$ is the residual error for each estimate.
```{r}
# Using subset data
final.model <- rma.mv(yi=val.log ~ trait,
V=sv.log, random = ~1|study/est.id,
data=data, method="REML", tdist=T)
final.model
save(final.model, file="Models/final.model.Rdata")
x <- mlm.variance.distribution(final.model, suppress.plot=F)
```
------------------------------------------------------------------------
### Step 5: Check final model diagnostics
Lastly, we check the final model diagnostics, such as examining the
funnel plot for asymmetry indicating publishing bias, in addition to simulating data
based on the final model to examine if it loosely resembles the data
observed.
#### Funnel plot
```{r}
x <- resid(final.model)
hist(x) # looks approx. normal!
## Model checks:
par(mfrow=c(2,1))
funnel(final.model)
funnel(final.model, yaxis="vinv")
par(mfrow=c(1,1))
ranktest(final.model) # p = 0.72
```
Visually, the funnel plot appears to be slightly asymmetric in favour of studies reporting lower heritabilities, but a rank correlation test for funnel plot asymmetry was not statistically significant (Kendall's $\tau = = -0.025$, $P = 0.72$), indicating no apparent publication bias.
#### Fail-safe number
```{r}
fsn(yi=val.log, vi=sv.log, data=data, type="Rosenberg") # N=1287 to get no significance (but assumes fixed effects)
# Number of studies x5 + 10:
length(unique(data$study))*5 + 10
```
#### Cook's distance
```{r}
cdist <- cooks.distance(final.model, reestimate=FALSE)
cdist.i <- which(cdist >2)
plot(cdist)
points(cdist.i, cdist[cdist.i], col="red", pch=16)
data[cdist.i,] %>% select(study, species, val, trait) %>%
cbind(., cooks = round(cdist[cdist.i], 2))
update(final.model, .~., data[-cdist.i,])
```
The main influential point is one for immune response, which drives the
average estimate for immune response much higher than other values for
immune response. Note that all the values for the immune response trait
come from the same study (Wright et al. 2019), and so the values are
already to be interpreted cautiously.
#### Residuals and simulated data
```{r}
plot(fitted(final.model), rstandard(final.model)$z)
abline(h=0, lty=2)
plot(residuals(final.model, type="pearson"))
abline(h=0, lty=2)
# Simulate new data given the parameter values of the data and same variance structure, plot on real data to see if it is similar
xsim <- simulate(final.model, 100) %>%
mutate(true.val = data$val.log,
weighting = 1/data$sv.log) %>%
pivot_longer(cols=-c(true.val, weighting)) %>%
select(name, true.val, sim.val = value, weighting)
xsim %>%
ggplot(aes(x=true.val, y=sim.val)) +
geom_point(aes(col=weighting), alpha=0.05) +
geom_abline(slope=1, intercept=0, linetype="dashed") +
scale_colour_viridis_c() +
scale_x_continuous(
breaks=log( c(0,0.10,0.25,0.5,0.75,1)+0.2),
labels = c(0,0.10,0.25,0.5,0.75,1)) +
scale_y_continuous(
breaks=log( c(0,0.10,0.25,0.5,0.75,1)+0.2),
labels = c(0,0.10,0.25,0.5,0.75,1))
# More accuracy in the middle, definitely!
# reruns model, based around the same variance/covariance structures but with simulated data.
xsim %>%
ggplot(aes(y=weighting)) +
geom_point(aes(x=sim.val), col="red", size=2, alpha=0.05) +
geom_point(data= filter(xsim, name == "sim_1"),
aes(x=true.val),
col="blue", size=2, alpha=0.9) +
scale_x_continuous(
breaks=log( c(0,0.10,0.25,0.5,0.75,1)+0.2),
labels = c(0,0.10,0.25,0.5,0.75,1)) +
labs(x="Heritability estimate", y=expression("Weighting (1/SE"^2*")"))
# Seems that the model is in general simulating the true data ok!
# Main problem is that values are simulating beyond the bounds of possible heritability.
```
------------------------------------------------------------------------
### Preamble on further models
Note that the data was previously not complete enough to examine
interactions between explanatory variables properly. For example, some factors such as heritability type and life stage have only a single level represented within some trait types, and some traits involve entirely one level of another factor, e.g., all estimates for nutrient content, immune response, and photochemistry come from single studies, and thus all have only one of the two heritability types within, thus precluding direct comparisons within these trait types.
Thus, to better compare interactions, we conduct separate analyses on subsetted data.
## Which predictor variable first?
To guide our analysis further, we determine which additional factors can be examined in combination with trait type, and compare the sample sizes of each comparison to determine the order of the sub-analyses.
```{r}
p1 <- data %>% table.plot("stage", "trait", .) # singular for photochemistry, immune response
p_stage <- table.plot("stage", "trait", dat_s)
p2 <- data %>% table.plot("h2", "trait", .) # singular for photochemistry, nutrient content, immune response
p_h2 <- table.plot("h2", "trait", dat_sh)
p3 <- data %>% table.plot("growth.form", "trait", .) # singular for traits: photochemistry, immune response, morphology, immune response, gene expression; singular for columnar and encrusting growth forms
p_growthform <- table.plot("growth.form", "trait", dat_shg)
p4 <- data %>% filter(!is.na(temp.diff)) %>%
table.plot("temp.manip.plus.others", "trait", .) # singular for traits: photochemistry, immune response, morphology, immune response, gene expression; singular for columnar and encrusting growth forms
p_tempmanip <- table.plot("temp.manip.plus.others", "trait", dat_tm)
require(patchwork)
(p1 | p2) / (p3 | p4)
```
We see there there are a number of rows/columns with less than 2 levels (i.e. 'singular' terms), which make it impossible to estimate interaction terms for these factor combinations, due to the singularity. Thus, we subset these rows/columns and plot the remaining data to examine the sample sizes and factor levels remaining.
```{r}
require(patchwork)
(p_stage | p_h2) / (p_growthform | p_tempmanip)
```
Both life stage and heritability type use similar levels, and thus can be combined for the smaller sample size of heritability type. Life stage thus must be examined first, then heritability type and life stage can be examined together for the smaller data subset. Growth form also uses an even more narrow range of studies, and thus we can examine it after looking at the latter two.
Finally, temperature manipulation results in a few different trait types being removed (e.g., morphology, symbiont community), and the addition of photochemistry, and the data within each trait type are also dissimilar due to some studies being observational and thust being removed. Thus, this analysis must be done on its own, and factors re-added on a case-by-case basis.
## Sub-analysis A: Model selection on dataset allowing a trait type x **life stage** interaction
### Step 0: View 'complete' dataset
```{r}
dat_s %>% table.plot("stage", "trait", .)
# exclude photochemistry, immune response
```
Using the smaller data subset, we can now examine possible interactions between trait type and life stage, and include additive terms for other predictors.
### Step 1: Define beyond optimal model
The beyond-optimal model in this case is a full analysis of trait type x life stage:
$$ logit(h^2) \sim N(\mu, \tau^2) \\
\mu = trait \times stage + h^2type + growth\,form + \epsilon_{i} $$
### Step 2: Select optimal random effects
```{r}
m.study.nested <- rma.mv(yi=val.log ~ trait * stage + h2 + growth.form,
V=sv.log, random = ~1|study/est.id,
data=dat_s, method="REML", tdist=T,
sigma2 = c(NA, NA))
m.spp.nested <- rma.mv(yi=val.log ~ trait * stage + h2 + growth.form,
V=sv.log, random = ~1|species/est.id,
data=dat_s, method="REML", tdist=T,
sigma2 = c(NA, NA))
m.only.est <- rma.mv(yi=val.log ~ trait * stage + h2 + growth.form,
V=sv.log, random = ~1|study/est.id,
data=dat_s, method="REML", tdist=T,
sigma2 = c(0, NA)) # same as random = ~1|est.id
m.only.study <- rma.mv(yi=val.log ~ trait * stage + h2 + growth.form,
V=sv.log, random = ~1|study/est.id,
data=dat_s, method="REML", tdist=T,
sigma2 = c(NA, 0)) # same as random = ~1|study
m.only.spp <- rma.mv(yi=val.log ~ trait * stage + h2 + growth.form,
V=sv.log, random = ~1|species/est.id,
data=dat_s, method="REML", tdist=T,
sigma2 = c(NA, 0)) # same as random = ~1|species
m.no.reff <- rma.mv(yi=val.log ~ trait * stage + h2 + growth.form,
V=sv.log, random = ~1|study/est.id,
data=dat_s, method="REML", tdist=T,
sigma2 = c(0, 0)) # no random effects
TableS3 <- myAIC(m.study.nested, m.spp.nested, m.only.est, m.only.study, m.only.spp, m.no.reff) %>%
mutate(model = fct_recode(model,
"Estimate ID nested in study ID" = "m.study.nested",
"Estimate ID nested in species" = "m.spp.nested",
"Study ID only" = "m.only.study",
"Species only" = "m.only.spp",
"Estimate ID only" = "m.only.est",
"No random effect" = "m.no.reff"))
write.csv(TableS3, file="Suppl Tables/TableS3.csv", row.names=F)
TableS3
rm(m.study.nested, m.spp.nested, m.only.est, m.only.study, m.only.spp, m.no.reff)
```
A random effects structure of estimate ID-only is preferred (i.e. to treat estimate IDs as sample sizes of same units). Next preferred is ΔAICc = 1.34.
### Step 3: Select optimal fixed effects
```{r}
mod_names <- ls(pattern = "m.fixed.")
do.call("rm", as.list(mod_names))
m.fixed.txs.h.g <- rma.mv(yi=val.log ~ trait*stage + h2 + growth.form,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.t.s.h.g <- rma.mv(yi=val.log ~ trait + stage + h2 + growth.form,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.txs.h <- rma.mv(yi=val.log ~ trait*stage + h2,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.txs.g <- rma.mv(yi=val.log ~ trait*stage + growth.form,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.txs <- rma.mv(yi=val.log ~ trait*stage,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.t.s.g <- rma.mv(yi=val.log ~ trait + stage + growth.form,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.t.s.h <- rma.mv(yi=val.log ~ trait + stage + h2,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.t.h.g <- rma.mv(yi=val.log ~ trait + h2 + growth.form,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.s.h.g <- rma.mv(yi=val.log ~ stage + h2 + growth.form,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.t.s <- rma.mv(yi=val.log ~ trait + stage,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.t.h <- rma.mv(yi=val.log ~ trait + h2,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.t.g <- rma.mv(yi=val.log ~ trait + growth.form,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.s.h <- rma.mv(yi=val.log ~ stage + h2,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.s.g <- rma.mv(yi=val.log ~ stage + growth.form,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.h.g <- rma.mv(yi=val.log ~ h2 + growth.form,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.t <- rma.mv(yi=val.log ~ trait,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.s <- rma.mv(yi=val.log ~ stage,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.h <- rma.mv(yi=val.log ~ h2,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.g <- rma.mv(yi=val.log ~ growth.form,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
m.fixed.null <- rma.mv(yi=val.log ~ 1,
random = ~1|study/est.id, V=sv.log,
data=dat_s, method="ML", tdist=T, sigma2 = c(0, NA))
mod_names <- ls(pattern = "m.fixed.")
TableS4 <- head(do.call("myAIC", as.list(c(mod_names, getmodel=T)))) %>%
mutate(model = fct_recode(model,
"trait x life stage + heritability type" = "m.fixed.txs.h",
"trait x life stage" = "m.fixed.txs",
"trait x life stage + growth form" = "m.fixed.txs.g",
"trait x life stage + heritability type + growth form" = "m.fixed.txs.h.g",
"trait + life stage + growth form" = "m.fixed.t.s.g",
"trait + heritability type + growth form" = "m.fixed.t.h.g"))
write.csv(TableS4, file="Suppl Tables/TableS4.csv", row.names=F)
TableS4
# trait x life stage preferred!
# Double-check with rma fits (knha method is slightly different from rma.mv with t-distributed CIs):
# m.fixed.txs.h <- rma(yi=val.log ~ trait*stage + h2,
# vi=sv.log, data=dat_s, method="ML", test="knha")
# m.fixed.txs.g <- rma(yi=val.log ~ trait*stage + growth.form,
# vi=sv.log, data=dat_s, method="ML", test="knha")
# m.fixed.txs <- rma(yi=val.log ~ trait*stage,
# vi=sv.log, data=dat_s, method="ML", test="knha")
# myAIC(m.fixed.txs.h, m.fixed.txs.g, m.fixed.txs)
# model of trait x life stage + heritability type is still preferred!
```
The trait x life stage + heritability fixed effect structure is optimal. The next closest model drops the effect of heritability type, and is over 2 ΔAICc away (ΔAICc = 2.6).
### Step 4: Fit final model of life stage/heritability type
```{r}
final.model.A <- rma(yi=val.log ~ trait*stage + h2, vi=sv.log,
data=dat_s, method="REML", test="knha")
final.model.A
save(final.model.A, file="Models/final.model.A.Rdata")
final.model.A$I
final.model.A$k - final.model.A$p # df for parameter tests
```
Estimate ID-only, so all residual heterogeneity is:
$I^2$ = 46.10%, N = 74
### Step 5: Check final model diagnostics
#### Funnel plot
```{r}
x <- resid(final.model.A)
hist(x) # looks approx. normal, except for one strong outlier at -1.5
par(mfrow=c(2,1))
funnel(final.model.A)
update(final.model.A, .~., data=dat_s[x!=0,]) %>%
funnel(., yaxis="vinv")
par(mfrow=c(1,1))
```
#### Fail-safe number
```{r}
fsn(yi=val.log, vi=sv.log, data=dat_s, type="Rosenberg") # N=513 to get no significance (but assumes fixed effects)
# Number of studies x5 + 10:
length(unique(dat_s$study))*5 + 10
```
#### Cook's distance
```{r}
cdist <- cooks.distance(final.model.A, reestimate=FALSE)
plot(cdist)
cdist.i <- which(cdist >2)
points(cdist.i, cdist[cdist.i], col="red", pch=16)
data[cdist.i,] %>% select(study, species, val, trait, stage, h2) %>%
cbind(., cooks = round(cdist[cdist.i], 2))
update(final.model.A, .~., data=dat_s[-cdist.i,]) # effect of juv:growth n.s.
update(final.model.A, .~., data=dat_s[-cdist.i[1],]) # immune : temp interaction remains
update(final.model.A, .~., data=dat_s[-cdist.i[2],]) # no interaction after removal!
```
#### Residuals and simulated data
```{r}
plot(fitted(final.model.A), rstandard(final.model.A)$z)
abline(h=0, lty=2)
plot(residuals(final.model.A, type="pearson"))
abline(h=0, lty=2)
# Not amazing, not terrible
# Simulate new data given the parameter values of the data and same variance structure, plot on real data to see if it is similar
xsim <- simulate(final.model.A, 100) %>%
mutate(true.val = dat_s$val.log,
weighting = 1/dat_s$sv.log) %>%
pivot_longer(cols=-c(true.val, weighting)) %>%
select(name, true.val, sim.val = value, weighting)
xsim %>%
ggplot(aes(x=true.val, y=sim.val)) +
geom_point(aes(col=weighting), alpha=0.05) +
geom_abline(slope=1, intercept=0, linetype="dashed") +
scale_colour_viridis_c() +
scale_x_continuous(
breaks=log( c(0,0.10,0.25,0.5,0.75,1)+0.2),
labels = c(0,0.10,0.25,0.5,0.75,1)) +
scale_y_continuous(
breaks=log( c(0,0.10,0.25,0.5,0.75,1)+0.2),
labels = c(0,0.10,0.25,0.5,0.75,1))
# reruns model, based around the same variance/covariance structures but with simulated data.
xsim %>%
ggplot(aes(y=weighting)) +
geom_point(aes(x=sim.val), col="red", size=2, alpha=0.05) +
geom_point(data= filter(xsim, name == "sim_1"),
aes(x=true.val),
col="blue", size=2, alpha=0.9) +
scale_x_continuous(
breaks=log( c(0,0.10,0.25,0.5,0.75,1)+0.2),
labels = c(0,0.10,0.25,0.5,0.75,1)) +
labs(x="Heritability estimate", y=expression("Weighting (1/SE"^2*")"))
# Seems that the model is in general simulating the true data ok!
# Main problem is that values are simulating beyond the bounds of possible heritability.
```
------------------------------------------------------------------------
## Sub-analysis B: Model selection on dataset allowing a trait type x **heritability type** interaction
Main result: trait type x heritability type interaction supported. Most
of the interaction is likely driven by the gene expression trait level,
where one estimate of narrow-sense h2 is higher than all broad-sense
estimates of H2.
### Step 0: View 'complete' dataset
```{r}
dat_sh %>% table.plot("h2", "trait", .) # subset allows examination of h2 x trait type
dat_sh %>% table.plot("stage", "trait", .) # can also look at life stage interaction too!
dat_sh %>% table.plot("stage", "h2", .)
dat_sh %>% table.plot("stage", "growth.form", .) # some bad levels for growth form!
```
The data subset allows for the examination of h2 x trait type interaction, as well as the trait x life stage interaction seen previously!
### Step 1: Define beyond optimal model
The beyond-optimal model in this case is a full analysis of trait type x heritability type + trait type x life stage, plus additive effects:
$$ logit(h^2) \sim N(\mu, \tau^2) \\
\mu = trait \times stage + trait \times h^2type + growth\,form + \epsilon_{i} $$
### Step 2: Select optimal random effects
```{r}
m.study.nested <- rma.mv(yi=val.log ~ trait * h2 + trait * stage + growth.form,
V=sv.log, random = ~1|study/est.id,
data=dat_sh, method="REML", tdist=T,
sigma2 = c(NA, NA))
m.spp.nested <- rma.mv(yi=val.log ~ trait * h2 + trait * stage + growth.form,
V=sv.log, random = ~1|species/est.id,
data=dat_sh, method="REML", tdist=T,
sigma2 = c(NA, NA))
m.only.est <- rma.mv(yi=val.log ~ trait * h2 + trait * stage + growth.form,
V=sv.log, random = ~1|study/est.id,
data=dat_sh, method="REML", tdist=T,
sigma2 = c(0, NA)) # same as random = ~1|est.id
m.only.study <- rma.mv(yi=val.log ~ trait * h2 + trait * stage + growth.form,
V=sv.log, random = ~1|study/est.id,
data=dat_sh, method="REML", tdist=T,
sigma2 = c(NA, 0)) # same as random = ~1|study
m.only.spp <- rma.mv(yi=val.log ~ trait * h2 + trait * stage + growth.form,
V=sv.log, random = ~1|species/est.id,
data=dat_sh, method="REML", tdist=T,
sigma2 = c(NA, 0)) # same as random = ~1|species
m.no.reff <- rma.mv(yi=val.log ~ trait * h2 + trait * stage + growth.form,
V=sv.log, random = ~1|study/est.id,
data=dat_sh, method="REML", tdist=T,
sigma2 = c(0, 0)) # no random effects
TableS5 <- myAIC(m.study.nested, m.spp.nested, m.only.est, m.only.study, m.only.spp, m.no.reff) %>%
mutate(model = fct_recode(model,
"Estimate ID nested in study ID" = "m.study.nested",
"Estimate ID nested in species" = "m.spp.nested",
"Study ID only" = "m.only.study",
"Species only" = "m.only.spp",
"Estimate ID only" = "m.only.est",
"No random effect" = "m.no.reff"))
write.csv(TableS5, file="Suppl Tables/TableS5.csv", row.names=F)
TableS5
rm(m.study.nested, m.spp.nested, m.only.est, m.only.study, m.only.spp, m.no.reff)
```
The optimal model is using study ID-only, however the second-most preferred model uses estimate ID only, and ΔAICc = 0.27, so close call either way.