-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_full_analysis_script.R
More file actions
281 lines (228 loc) · 9.42 KB
/
08_full_analysis_script.R
File metadata and controls
281 lines (228 loc) · 9.42 KB
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
#full script for modeling and predicting LMA and phenology from fossil data
#required packages----
require(BIEN)
require(tidyr)
require(dplyr)
require(stringr)
require(phangorn)
require(ape)
require(phytools)
require(phylolm)
require(MPSEM)
library(picante)
library(readr)
library(ggplot2)
require(mosaic)
require(lattice)
require(nlme)
library(geiger)
library(sp)
require(foreach)
require(phylobase)
require(tibble)
require(miscTools)
require(lme4)
require(RColorBrewer)
#Initial phylogenetic approach----
#This approach inserts a species of your choice randomly into a phylogeny based on taxonomy and its most recent fossil
#this is a custom function
#not exactly relevent for the lm and glm models but might come in handly
#extract.clade.label-----
#depedency within drop.clade.label------
#https://github.com/MarioJose/r-functions
extract.clade.label <- function(tree, node){
if(!is.vector(node, mode = "character") | length(node) > 1)
stop("'node' parameter must be a character vector of length 1")
if(sum(node %in% tree$node.label) == 0)
stop("tree has not node labels defined in 'node' parameter")
if(!is.rooted(tree))
stop("tree must be rooted")
# Solve bug reading newick in phytools 0.6
if(tree$Nnode > length(tree$node.label))
tree$node.label <- c(tree$node.label, rep('', tree$Nnode - length(tree$node.label)))
# Reorder tree
#tree <- reorder(tree)
# number of tips
not <- length(tree$tip.label)
# node number
nn <- which(tree$node.label %in% node) + not
edge <- tree$edge
# nodes descendants
nd <- tmp <- edge[edge[ ,1] %in% nn , 2]
while(length(tmp) > 0){
tmp <- edge[edge[ ,1] %in% tmp , 2]
if(length(tmp) > 0) nd <- c(nd, tmp)
}
# tips desdendants
td <- nd[nd <= not]
# only nodes descendats
nd <- nd[nd > not]
# New tips positions
newt <- matrix(NA, nrow = not, ncol = 2,
dimnames = list(1:not, c('tips', 'new')))
newt[ ,1] <- 1:not
newt <- newt[newt[ ,1] %in% td, ]
newt[ ,2] <- rank(newt[ ,1])
# New nodes positions
newn <- matrix(NA, nrow = tree$Nnode, ncol = 2,
dimnames = list(1:tree$Nnode, c('nodes', 'new')))
newn [ ,1] <- 1:tree$Nnode + not
newn <- newn[newn[ ,1] %in% c(nd, nn), ]
if(!is.matrix(newn))
newn <- matrix(newn, nrow = 1, dimnames = list(1, names(newn)))
newn[ ,2] <- rank(newn[ ,1]) + length(td)
# Reference table to new nodes positions
refedge <- rbind(newt, newn)
newedge <- edge[edge[ ,1] %in% c(nd, nn), ]
newedge[ ,1] <- refedge[match(newedge[ ,1], refedge[ ,1]) ,2]
newedge[ ,2] <- refedge[match(newedge[ ,2], refedge[ ,1]) ,2]
# Update tree
tree$edge <- newedge
tree$tip.label <- tree$tip.label[newt[ ,1]]
tree$node.label <- tree$node.label[newn[ ,1] - not]
tree$Nnode <- length(tree$node.label)
return(tree)
}
#drop.clade.label function, dependency for intfossil---------
#https://github.com/MarioJose/r-functions
drop.clade.label <- function(tree, node){
# Remove all tips from 'node', keeping 'node' label as a tip
if(!is.vector(node, mode = "character") | length(node) > 1)
stop("'node' parameter must be a character vector of length 1")
if(sum(node %in% tree$node.label) == 0)
stop("tree has not node labels defined in 'node' parameter")
if(!is.rooted(tree))
stop("tree must be rooted")
# Solve bug reading newick in phytools 0.6
if(tree$Nnode > length(tree$node.label))
tree$node.label <- c(tree$node.label, rep('', tree$Nnode - length(tree$node.label)))
# Reorder tree
#tree <- reorder(tree)
# number of tips
not <- length(tree$tip.label)
# node number
nn <- which(tree$node.label %in% node) + not
# 'node' tree extracted
extr <- extract.clade.label(tree, node)
# number of tips of extracted 'node' tree
extrnot <- length(extr$tip.label)
# new number of tips
nnot <- not - extrnot
edge <- tree$edge
# nodes descendants
nd <- tmp <- edge[edge[ ,1] %in% nn , 2]
while(length(tmp) > 0){
tmp <- edge[edge[ ,1] %in% tmp , 2]
if(length(tmp) > 0) nd <- c(nd, tmp)
}
# tips desdendants
td <- nd[nd <= not]
# only nodes descendats
nd <- nd[nd > not]
# New tips positions
newt <- matrix(NA, nrow = not, ncol = 2,
dimnames = list(1:not, c('tips', 'new')))
newt[ ,1] <- 1:not
newt <- newt[!(newt[ ,1] %in% td), ]
newt[ ,2] <- rank(newt[ ,1])
# New nodes positions
newn <- matrix(NA, nrow = tree$Nnode, ncol = 2,
dimnames = list(1:tree$Nnode, c('nodes', 'new')))
newn [ ,1] <- 1:tree$Nnode + not
newn <- newn[!(newn[ ,1] %in% c(nd, nn)), ]
# 'nnot' plus 1L to new tip 'node'
newn [ ,2] <- rank(newn[ ,1]) + nnot + 1L
# Reference table to new nodes positions
refedge <- rbind(newt, newn, matrix(c(nn, (nnot + 1)), nrow = 1))
newedge <- edge[!(edge[ ,1] %in% c(nd, nn)), ]
newedge[ ,1] <- refedge[match(newedge[ ,1], refedge[ ,1]) ,2]
newedge[ ,2] <- refedge[match(newedge[ ,2], refedge[ ,1]) ,2]
# Update tree
tree$edge <- newedge
tree$tip.label <- c(tree$tip.label[newt[ ,1]], node)
tree$node.label <- tree$node.label[newn[ ,1] - not]
tree$Nnode <- length(tree$node.label)
return(tree)
}
#extract.node.label and drop.node.lable fromp------
#https://github.com/MarioJose/r-functions/blob/master/drop.clade.label/extract.clade.label.r
#intfossil function-----------------------------------------
#created through addfossil function https://github.com/michellelawing/ppgm
#dependencies: ape, phytools, drop.clade.labels
intfossil <- function(tree, mintime=0,maxtime=NA, name="fossil", edge=NA, genus="genus", fossil_tax)
{
require(ape)
lookup <- match(genus, fossil_tax$scrubbed_genus)
taxonomy <- fossil_tax[na.omit(lookup), ]
order <- taxonomy$order
cladetree <- extract.clade.label(tree, order)
cladetree$edge.length<- extract.clade(tree, order)$edge.length
if(is.na(maxtime)){maxtime=max(dist.nodes(cladetree))/2}
treeage<-max(dist.nodes(cladetree))/2
M<-dist.nodes(cladetree)
maxedge<-(as.numeric(treeage - M[cladetree$edge[,1],cladetree$edge[1,1]]))
minedge<-(as.numeric(treeage - M[cladetree$edge[,2],cladetree$edge[1,1]]))
if(is.na(edge)){edgesample<-sample(which(maxedge>mintime & minedge<maxtime),1)}
cull_tree <- drop.clade.label(tree, order)
cull_tree$edge.length <- drop.tip(tree, cladetree$tip.label, collapse.singles = FALSE)$edge.length
tree_4 <- as(tree, "phylo4")
reqedge <- getEdge(tree_4, order)
reqlength <- edgeLength(tree_4)[reqedge]
length <- tree$edge.length
reqloc <- as.numeric(which(grepl(reqlength, length)))
nedge <- as.matrix(cull_tree$edge.length)
fixedge <- insertRow(nedge, reqloc, reqlength)
cull_tree$edge.length <- fixedge
dedge<-cladetree$edge[edgesample,2]
place<-runif(1,max(c(minedge[edgesample],mintime)),min(c(maxtime,maxedge[edgesample])))
#if(is_not_going_to_work)
#then(dont)
fossil<-list(edge=matrix(c(2,1),1,2), tip.label=name, edge.length=runif(1,min=0.0000000001,max=(place-max(c(minedge[edgesample],mintime)))), Nnode=1)
class(fossil)<-"phylo"
fossiltree<-bind.tree(cladetree,fossil,where=dedge,position=place-minedge[edgesample])
tree_full <- bind.tree(cull_tree, fossiltree, where = which(cull_tree$tip.label == order))
tree_full <- force.ultrametric(tree_full, method = "extend")
return(tree_full)
}
#mixed-effects modelling-----
#this section is where we predict lma from petiole^2/la and family
#data creation
royer_tax_full <- readRDS("~/Documents/BEIN data R/data/processed/07_lm4_royer")
royer_fossil_dropped <- all_fossil_royer_pred %>%
filter(!grepl('Cercidiphyllaceae', scrubbed_family)) %>%
filter(!grepl('Smilacaceae', scrubbed_family)) %>%
filter(!grepl('Staphyleaceae', scrubbed_family))
royer_tax_count <- royer_tax_full_na_omit%>%
group_by(scrubbed_family) %>%
mutate(count=n()) %>%
royer_tax_count
#creation of royer_tax_top_5/ royer_tax_over_10---------------
#These are imporatant as these determine the top 5 extant families with the most samples, and the top 10 families respecitively------
royer_tax_top5 <- subset(royer_tax_count, as.numeric(count)>23)
royer_tax_over10 <- subset(royer_tax_count, as.numeric(count)>9)
#creation of the royer_lmfam_top5 and royer_lm_top5, these are the linear models for comparing log lma and contains predictions for log(lma)----
#for 5 fossil famlies Ericaceae, Fabaceae, Fagaceae, Myrtaceae, and Proteaceae
royer_lm_top5 <- lm(log_lma~log_pet_leafarea, data = royer_tax_top5)
pred_lmfam_top5 <- as.data.frame(predict(royer_lmfam_top5, interval="prediction"))
royer_lmfam_top5_bound <- cbind(royer_tax_top5, pred_lmfam_top5)
#The following two sections plot log_lma vs log(petiole^2/leafarea) for the top 5 families---------
ggplot(royer_lmfam_top5_bound, aes(log_pet_leafarea, log_lma))+
aes(color=royer_lmfam_top5_bound$scrubbed_family)+
geom_point() +
geom_smooth(method=lm, se=TRUE)+
labs(x="log(petiole^2/leafarea)")+
labs(y="log(lma)")+
labs(color = "Family")
ggplot(royer_lmfam_top5_bound, aes(log_pet_leafarea, log_lma))+
geom_point() +
geom_smooth(method=lm, se=TRUE)+
labs(x="log(petiole^2/leafarea)")+
labs(y="log(lma)")+
labs(color = "Family")
##This is the logisitic regression predicting phenlogoy off of log_lma-----
ggplot(glm_pred_data, aes(log_lma, phenology))+
geom_point() +
stat_smooth(method="glm", method.args=list(family="binomial"), se=FALSE)
ggplot(glm_pred_data, aes(log_lma, phenology, group=(scrubbed_family), color=scrubbed_family)) +
geom_point() +
stat_smooth(method="glm", method.args=list(family="binomial"), se=FALSE)