-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chrotomyini_ancestralstate_06032022.Rmd
313 lines (268 loc) · 9.88 KB
/
Chrotomyini_ancestralstate_06032022.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
---
title: "Chrotomyini ancestral state reconstruction 06032022"
author: "S.M. Smith"
date: "6/3/2022"
output:
html_document:
keep_md: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
pacman::p_load(install = F, "ape", "bayesplot","BiocManager", "brms", "broom", "dagitty", "devtools", "flextable", "ggdark", "ggmcmc", "ggrepel", "ggtree", "gtools", "lattice","loo", "patchwork", "phytools","rcartocolor", "Rcpp", "remotes", "rstan", "StanHeaders", "statebins", "tidybayes", "viridis", "viridisLite", "pacman")
```
Load up Chrotomyini trabecular bone architecture (TBA) data and standardize variables:
```{r}
d <- read.csv(file = "G:\\My Drive\\Philippine rodents\\chrotomyini\\05062022 Philippine Murids segmentation parameters and morphological data - TBA data total BoneJ (full).csv", header = T)
d <- d[d$tribe=="chroto",c(1:2, 4:23)]
d <-
d %>%
mutate(bvtv = as.numeric(bvtv))
d <-
d %>%
mutate(mass_s = rethinking::standardize(log10(mass_g)),
elev_s = rethinking::standardize(elev),
bvtv_s = rethinking::standardize(bvtv),
tbth_s = rethinking::standardize(tbth),
tbsp_s = rethinking::standardize(tbsp),
conn_s = rethinking::standardize(conn),
cond_s = rethinking::standardize(m_connd),
cond_s2 = rethinking::standardize(connd),
da_s = rethinking::standardize(da))
# remove C. gonzalesi and R. isarogensis, singletons:
d <-
d %>%
filter(taxon!="Chrotomys_gonzalesi") %>%
filter(taxon!="Rhynchomys_isarogensis")
# Make categorical vars into factors
d <-
d %>%
mutate(loco = factor(loco),
hab_simp = factor(hab_simp),
genus = factor(genus))
# Specify colors for plots:
cols = c("#86acca","#ab685b", "#3370a3", "#1f314c","#5a9fa8")
```
Load in phylogeny:
REMEMBER: A <- ape::vcv.phylo(phylo), add corr = T if your tree is NOT SCALED TO 1.
```{r}
ch.tre <- read.nexus(file = "G:\\My Drive\\Philippine rodents\\Chrotomys\\analysis\\SMS_PRUNED_and_COLLAPSED_03292022_OTUsrenamed_Rowsey_PhBgMCC_LzChrotomyini.nex")
ch <- ape::vcv.phylo(ch.tre, corr = T)
d <-
d %>%
mutate(phylo = taxon)
```
Ancestral state reconstruction as demonstrated here:
http://www.phytools.org/eqg2015/asr.html
Calculate species means:
```{r}
# Species means
dmean <- d %>%
group_by(taxon) %>%
summarize(mass_s = mean(mass_s),
bvtv_s = mean(bvtv_s),
tbth_s = mean(tbth_s),
tbsp_s = mean(tbsp_s),
cond_s = mean(cond_s))
```
```{r}
mass_ace <- dmean$mass_s
names(mass_ace) <- dmean$taxon
fit<-fastAnc(ch.tre,mass_ace,vars=TRUE,CI=TRUE)
obj<-contMap(ch.tre,mass_ace,plot=FALSE)
plot(obj,legend=0.7*max(nodeHeights(ch.tre)),
fsize=c(0.7,0.9))
td <- data.frame(node = nodeid(ch.tre, names(mass_ace)),
trait = mass_ace)
nd <- data.frame(node = names(fit$ace), trait = fit$ace)
d <- rbind(td, nd)
d$node <- as.numeric(d$node)
tree <- full_join(ch.tre, d, by = 'node')
p1mass <- ggtree(tree,ladderize = FALSE, size=2.8) +
geom_tree(aes(color=trait), continuous = 'colour', size=3) +
scale_color_gradientn(colours=c("#ecd156", "#00bbcd", "#fc2187")) +
geom_tiplab(size = 3, hjust=-0.3) +
theme(legend.position = "none") +
xlim(0, 12)+
theme(legend.position = c(0.05, .85))+
labs(color = "mass")
```
########################################
Use ggtree because then you can also use patchwork to look at stuff side by side.
Bone Volume fraction:
```{r}
# Without size as predictor - just mean of scaled vars by species.
bvtv_ace <- dmean$bvtv_s
names(bvtv_ace) <- dmean$taxon
# Fit and make tree
fit<-fastAnc(ch.tre,bvtv_ace,vars=TRUE,CI=TRUE)
td <- data.frame(node = nodeid(ch.tre, names(bvtv_ace)),
trait = bvtv_ace)
nd <- data.frame(node = names(fit$ace), trait = fit$ace)
d <- rbind(td, nd)
d$node <- as.numeric(d$node)
tree <- full_join(ch.tre, d, by = 'node')
p1bvtv <- ggtree(tree, aes(color=trait),
ladderize = FALSE, continuous = 'colour', size=2) +
scale_color_gradientn(colours=c("red", 'orange', 'green', 'cyan', 'blue')) +
geom_tiplab(size = 3, hjust=-0.3) +
theme(legend.position = "none") +
xlim(0, 12)
# With size as predictor
ch.75.4 <-
brm(file = "G:\\My Drive\\Philippine rodents\\chrotomyini\\fits\\ch.75.4")
print(ch.75.4)
bvtv_mu.0 <- ch.75.4 %>%
fixef() %>%
as.data.frame() %>%
select(matches("Estimate")) %>%
slice(1:11,)
bvtv_mu <- as.numeric(bvtv_mu.0$Estimate)
names(bvtv_mu) <- (gsub("taxon", "", rownames(bvtv_mu.0)))
# Fit and make tree
fit<-fastAnc(ch.tre,bvtv_mu,vars=TRUE,CI=TRUE)
td <- data.frame(node = nodeid(ch.tre, names(bvtv_mu)),trait = bvtv_mu)
nd <- data.frame(node = names(fit$ace), trait = fit$ace)
d <- rbind(td, nd)
d$node <- as.numeric(d$node)
tree <- full_join(ch.tre, d, by = 'node')
p2bvtv <- ggtree(tree, aes(color=trait),
ladderize = FALSE, continuous = 'colour', size=2) +
scale_color_gradientn(colours=c("red", 'orange', 'green', 'cyan', 'blue')) +
geom_tiplab(size = 3, hjust=-0.3) +
theme(legend.position = "none") +
xlim(0, 12)
p1bvtv|p2bvtv
```
Trabecular thickness:
```{r}
# Without size as predictor - just mean of scaled vars by species.
tbth_ace <- dmean$tbth_s
names(tbth_ace) <- dmean$taxon
# Fit and make tree
fit<-fastAnc(ch.tre,tbth_ace,vars=TRUE,CI=TRUE)
td <- data.frame(node = nodeid(ch.tre, names(tbth_ace)),
trait = tbth_ace)
nd <- data.frame(node = names(fit$ace), trait = fit$ace)
d <- rbind(td, nd)
d$node <- as.numeric(d$node)
tree <- full_join(ch.tre, d, by = 'node')
p1tbth <- ggtree(tree, aes(color=trait),
ladderize = FALSE, continuous = 'colour', size=2) +
scale_color_gradientn(colours=c("red", 'orange', 'green', 'cyan', 'blue')) +
geom_tiplab(size = 3, hjust=-0.3) +
theme(legend.position = "none") +
xlim(0, 12)
# With size as predictor
ch.76.4 <-
brm(file = "G:\\My Drive\\Philippine rodents\\chrotomyini\\fits\\ch.76.4")
tbth_mu.0 <- ch.76.4 %>%
fixef() %>%
as.data.frame() %>%
select(matches("Estimate")) %>%
slice(1:11,)
tbth_mu <- as.numeric(tbth_mu.0$Estimate)
names(tbth_mu) <- (gsub("taxon", "", rownames(tbth_mu.0)))
# Fit and make tree
fit<-fastAnc(ch.tre,tbth_mu,vars=TRUE,CI=TRUE)
td <- data.frame(node = nodeid(ch.tre, names(tbth_mu)),trait = tbth_mu)
nd <- data.frame(node = names(fit$ace), trait = fit$ace)
d <- rbind(td, nd)
d$node <- as.numeric(d$node)
tree <- full_join(ch.tre, d, by = 'node')
p2tbth <- ggtree(tree, aes(color=trait),
ladderize = FALSE, continuous = 'colour', size=2) +
scale_color_gradientn(colours=c("red", 'orange', 'green', 'cyan', 'blue')) +
geom_tiplab(size = 3, hjust=-0.3) +
theme(legend.position = "none") +
xlim(0, 12)
p1tbth|p2tbth
```
Trabecular spacing:
```{r}
# Without size as predictor - just mean of scaled vars by species.
tbsp_ace <- dmean$tbsp_s
names(tbsp_ace) <- dmean$taxon
# Fit and make tree
fit<-fastAnc(ch.tre,tbsp_ace,vars=TRUE,CI=TRUE)
td <- data.frame(node = nodeid(ch.tre, names(tbsp_ace)),
trait = tbsp_ace)
nd <- data.frame(node = names(fit$ace), trait = fit$ace)
d <- rbind(td, nd)
d$node <- as.numeric(d$node)
tree <- full_join(ch.tre, d, by = 'node')
p1tbsp <- ggtree(tree, aes(color=trait),
ladderize = FALSE, continuous = 'colour', size=2) +
scale_color_gradientn(colours=c("red", 'orange', 'green', 'cyan', 'blue')) +
geom_tiplab(size = 3, hjust=-0.3) +
theme(legend.position = "none") +
xlim(0, 12)
# With size as predictor
ch.77.4 <-
brm(file = "G:\\My Drive\\Philippine rodents\\chrotomyini\\fits\\ch.77.4")
tbsp_mu.0 <- ch.77.4 %>%
fixef() %>%
as.data.frame() %>%
select(matches("Estimate")) %>%
slice(1:11,)
tbsp_mu <- as.numeric(tbsp_mu.0$Estimate)
names(tbsp_mu) <- (gsub("taxon", "", rownames(tbsp_mu.0)))
# Fit and make tree
fit<-fastAnc(ch.tre,tbsp_mu,vars=TRUE,CI=TRUE)
td <- data.frame(node = nodeid(ch.tre, names(tbsp_mu)),trait = tbsp_mu)
nd <- data.frame(node = names(fit$ace), trait = fit$ace)
d <- rbind(td, nd)
d$node <- as.numeric(d$node)
tree <- full_join(ch.tre, d, by = 'node')
p2tbsp <- ggtree(tree, aes(color=trait),
ladderize = FALSE, continuous = 'colour', size=2) +
scale_color_gradientn(colours=c("red", 'orange', 'green', 'cyan', 'blue')) +
geom_tiplab(size = 3, hjust=-0.3) +
theme(legend.position = "none") +
xlim(0, 12)
p1tbsp|p2tbsp
```
Connectivity density:
```{r}
# Without size as predictor - just mean of scaled vars by species.
cond_ace <- dmean$cond_s
names(cond_ace) <- dmean$taxon
# Fit and make tree
fit<-fastAnc(ch.tre,cond_ace,vars=TRUE,CI=TRUE)
td <- data.frame(node = nodeid(ch.tre, names(cond_ace)),
trait = cond_ace)
nd <- data.frame(node = names(fit$ace), trait = fit$ace)
d <- rbind(td, nd)
d$node <- as.numeric(d$node)
tree <- full_join(ch.tre, d, by = 'node')
p1cond <- ggtree(tree, aes(color=trait),
ladderize = FALSE, continuous = 'colour', size=2) +
scale_color_gradientn(colours=c("red", 'orange', 'green', 'cyan', 'blue')) +
geom_tiplab(size = 3, hjust=-0.3) +
theme(legend.position = "none") +
xlim(0, 12)
# With size as predictor
ch.78.4 <-
brm(file = "G:\\My Drive\\Philippine rodents\\chrotomyini\\fits\\ch.78.4")
cond_mu.0 <- ch.78.4 %>%
fixef() %>%
as.data.frame() %>%
select(matches("Estimate")) %>%
slice(1:11,)
cond_mu <- as.numeric(cond_mu.0$Estimate)
names(cond_mu) <- (gsub("taxon", "", rownames(cond_mu.0)))
# Fit and make tree
fit<-fastAnc(ch.tre,cond_mu,vars=TRUE,CI=TRUE)
td <- data.frame(node = nodeid(ch.tre, names(cond_mu)),trait = cond_mu)
nd <- data.frame(node = names(fit$ace), trait = fit$ace)
d <- rbind(td, nd)
d$node <- as.numeric(d$node)
tree <- full_join(ch.tre, d, by = 'node')
p2cond <- ggtree(tree, aes(color=trait),
ladderize = FALSE, continuous = 'colour', size=2) +
scale_color_gradientn(colours=c("red", 'orange', 'green', 'cyan', 'blue')) +
geom_tiplab(size = 3, hjust=-0.3) +
theme(legend.position = "none") +
xlim(0, 12)
p1cond|p2cond
```