-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphylo_sde.R
311 lines (248 loc) · 13.2 KB
/
phylo_sde.R
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
#'phylo_sde
#'
#'Simulate a Cox-Ingersoll-Ross Diffusion Process in the Tree of Life
#'
#'@param tr An object of class \code{phylo} from the ape package. In this version the CIR
#'process parameters alpha, mu, and sigma for each branch are within vectors in the same order
#'as the edge (branch) labelling.
#'@param rt_value Value at the root of \code{tr}.
#'@param N Data imputation frequency.
#'@param theta Matrix of parameter values for each edge of the tree.
#'@param model A list containing drift, diffusion, and the partial differentiation of diffusion as quoted
#'expressions using method quote. For the Euler scheme
#'the drift coefficient as \code{drift}, the diffusion coefficient as
#'\code{diffusion}, and the partial differentiation of
#'\code{diffusion} by \code{x} as \code{dx_diffusion} is required.
#'See the Examples.
#'@param method Specified as either "euler" or "milstein."
#'@param fossils A numeric vector containing the tip values for every fossil added to the tree.
#'@param ... Not used.
#'
#'@return A list of time series projects for each simulated path equal to the length of
#'the number of branches in the \code{tr} object.
#'
#'@export
phylo_sde <- function(tr, rt_value, N, theta, model, method, traits, fossils=NULL, ...) {
#stores list of points for each simulated edge
lst <- list()
n_tips <- length(tr$tip.label)
rt_node <- n_tips + 1
#only relevant if specified
dotslist <- list(...)
if ("pred.corr" %in% names(dotslist)) {
pred.corr <- dotslist$pred.corr
} else {
pred.corr <- FALSE
}
if (method == "milstein") {
pred.corr <- TRUE
}
if (pred.corr) {
if (!exists("dx_diffusion", model)) {
model$dx_diffusion <- D(model$diffusion, "x")
}
} else {
model$dx_diffusion <- quote(NULL)
}
sde_edges <- function(fossils, tr, node, X0, t0, traits) {
#node is the root node
# preceeding nodes
daughters <- tr$edge[which(tr$edge[, 1] == node), 2]
## if (length(daughters) == 0) return()
if (any(daughters %in% fossils)) {
# do not use fossil edge (length = 0), use the sister node edge
edge <- which((tr$edge[,1] == node) & !(tr$edge[, 2] %in% fossils))
f_edge <- which((tr$edge[,1] == node) & (tr$edge[, 2] %in% fossils))
root <- tr$edge[edge, 2]
lst[[f_edge]] <<- 0
drift <- as.expression(force(eval(substitute(substitute(e,
list(alpha = theta[edge, "alpha"],
mu = theta[edge, "mu"],
sigma = theta[edge, "sigma"])),
list(e = model$drift)))))
#diffusion = expression (1)
# model$diffusion = sigma
diffusion <- as.expression(force(eval(substitute(substitute(e,
list(alpha = theta[edge, "alpha"],
mu = theta[edge, "mu"],
sigma = theta[edge, "sigma"])),
list(e = model$diffusion)))))
#diffusion_x = 0
#model$dx_diffusion
diffusion_x <- as.expression(force(eval(substitute(substitute(e,
list(alpha = theta[edge, "alpha"],
mu = theta[edge, "mu"],
sigma = theta[edge, "sigma"])),
list(e = model$dx_diffusion)))))
#number of steps is the length of the edge times the given frequency N (100)
n_steps <- tr$edge.length[edge] * N
#time end is time start plus the length of the given edge
tE <- t0 + tr$edge.length[edge]
X0 <- traits[fossils[which(fossils %in% daughters)]]
#runs sde.sim
## print(t0)
lst[[edge]] <<- sde::sde.sim(X0 = X0, t0 = t0, T = tE, N = n_steps,
method = method,
drift = drift,
sigma = diffusion,
sigma.x = diffusion_x,
pred.corr = pred.corr
)
tE <- tsp(lst[[edge]])[2]
if (root > n_tips) {
sde_edges(fossils=fossils, tr=tr, node=root, X0=lst[[edge]][n_steps + 1], t0=tE, traits=traits)
}
}else{
for (d_ind in 1:2) {
#identify the branch for simulation
edge <- which((tr$edge[, 1] == node) & (tr$edge[, 2] == daughters[d_ind]))
#drift = expression (0.1 *(0-x))
# model$drift = alpha * (mu - x)
#pulls specific edge parameters from vector theta
drift <- as.expression(force(eval(substitute(substitute(e,
list(alpha = theta[edge, "alpha"],
mu = theta[edge, "mu"],
sigma = theta[edge, "sigma"])),
list(e = model$drift)))))
#diffusion = expression (1)
# model$diffusion = sigma
diffusion <- as.expression(force(eval(substitute(substitute(e,
list(alpha = theta[edge, "alpha"],
mu = theta[edge, "mu"],
sigma = theta[edge, "sigma"])),
list(e = model$diffusion)))))
#diffusion_x = 0
#model$dx_diffusion
diffusion_x <- as.expression(force(eval(substitute(substitute(e,
list(alpha = theta[edge, "alpha"],
mu = theta[edge, "mu"],
sigma = theta[edge, "sigma"])),
list(e = model$dx_diffusion)))))
#number of steps is the length of the edge times the given frequency N (100)
n_steps <- tr$edge.length[edge] * N
#time end is time start plus the length of the given edge
tE <- t0 + tr$edge.length[edge]
#runs sde.sim
if (t0 == tE) {
lst[[edge]] <- 0
} else {
lst[[edge]] <<- sde::sde.sim(X0 = X0, t0 = t0, T = tE, N = n_steps,
method = method,
drift = drift,
sigma = diffusion,
sigma.x = diffusion_x,
pred.corr = pred.corr
)
}
tE <- tsp(lst[[edge]])[2]
#recursive call until a tip is met
if (daughters[d_ind] > n_tips && lengths(lst[[edge]]) > 0) {
sde_edges(fossils=fossils, tr=tr, node=daughters[d_ind], X0=lst[[edge]][n_steps + 1],t0= tE, traits=traits)
} else {
sde_edges(fossils=fossils, tr=tr, node=daughters[d_ind], X0=X0, t0=tE, traits=traits)
}
}
}
}
sde_edges(fossils=fossils, tr=tr, node=rt_node, X0 = rt_value, t0 = 0, traits=traits)
# Remove tip values (we have observed tip values)
# fossils in list are given value of 0, NULL causes problems if the fossil is right justified
return(lst)
}
############################################################################
sde_edges <- function(fossils, tr, node, X0, t0, traits) {
#node is the root node
# preceeding nodes
daughters <- tr$edge[which(tr$edge[, 1] == node), 2]
## if (length(daughters) == 0) return()
if (any(daughters %in% fossils)) {
# do not use fossil edge (length = 0), use the sister node edge
edge <- which((tr$edge[,1] == node) & !(tr$edge[, 2] %in% fossils))
f_edge <- which((tr$edge[,1] == node) & (tr$edge[, 2] %in% fossils))
root <- tr$edge[edge, 2]
lst[[f_edge]] <<- 0
drift <- as.expression(force(eval(substitute(substitute(e,
list(alpha = theta[edge, "alpha"],
mu = theta[edge, "mu"],
sigma = theta[edge, "sigma"])),
list(e = model$drift)))))
#diffusion = expression (1)
# model$diffusion = sigma
diffusion <- as.expression(force(eval(substitute(substitute(e,
list(alpha = theta[edge, "alpha"],
mu = theta[edge, "mu"],
sigma = theta[edge, "sigma"])),
list(e = model$diffusion)))))
#diffusion_x = 0
#model$dx_diffusion
diffusion_x <- as.expression(force(eval(substitute(substitute(e,
list(alpha = theta[edge, "alpha"],
mu = theta[edge, "mu"],
sigma = theta[edge, "sigma"])),
list(e = model$dx_diffusion)))))
#number of steps is the length of the edge times the given frequency N (100)
n_steps <- tr$edge.length[edge] * N
#time end is time start plus the length of the given edge
tE <- t0 + tr$edge.length[edge]
X0 <- traits[fossils[which(fossils %in% daughters)]]
#runs sde.sim
lst[[edge]] <<- sde::sde.sim(X0 = X0, t0 = t0, T = tE, N = n_steps,
drift = drift,
sigma = diffusion,
sigma.x = diffusion_x, method="euler", pred.corr=F
)
tE <- tsp(lst[[edge]])[2]
if (root > n_tips) {
sde_edges(fossils=fossils, tr=tr, node=root, X0=lst[[edge]][n_steps + 1], t0=tE, traits=traits)
}
}else{
for (d_ind in 1:2) {
#identify the branch for simulation
edge <- which((tr$edge[, 1] == node) & (tr$edge[, 2] == daughters[d_ind]))
#drift = expression (0.1 *(0-x))
# model$drift = alpha * (mu - x)
#pulls specific edge parameters from vector theta
drift <- as.expression(force(eval(substitute(substitute(e,
list(alpha = theta[edge, "alpha"],
mu = theta[edge, "mu"],
sigma = theta[edge, "sigma"])),
list(e = model$drift)))))
#diffusion = expression (1)
# model$diffusion = sigma
diffusion <- as.expression(force(eval(substitute(substitute(e,
list(alpha = theta[edge, "alpha"],
mu = theta[edge, "mu"],
sigma = theta[edge, "sigma"])),
list(e = model$diffusion)))))
#diffusion_x = 0
#model$dx_diffusion
diffusion_x <- as.expression(force(eval(substitute(substitute(e,
list(alpha = theta[edge, "alpha"],
mu = theta[edge, "mu"],
sigma = theta[edge, "sigma"])),
list(e = model$dx_diffusion)))))
#number of steps is the length of the edge times the given frequency N (100)
n_steps <- tr$edge.length[edge] * N
#time end is time start plus the length of the given edge
tE <- t0 + tr$edge.length[edge]
#runs sde.sim
if (t0 == tE) {
lst[[edge]] <<- 0
} else {
lst[[edge]] <<- sde::sde.sim(X0 = X0, t0 = t0, T = tE, N = n_steps,
drift = drift, method="euler",
sigma = diffusion,
sigma.x = diffusion_x, pred.corr=F
)
}
tE <- tsp(lst[[edge]])[2]
#recursive call until a tip is met
if (daughters[d_ind] > n_tips) {
sde_edges(fossils=fossils, tr=tr, node=daughters[d_ind], X0=lst[[edge]][n_steps + 1],t0= tE, traits=traits)
} else {
sde_edges(fossils=fossils, tr=tr, node=daughters[d_ind], X0=X0, t0=tE, traits=traits)
}
}
}
}
sde_edges(fossils=fossils, tr=tr, node=6, X0 = rt_value, t0 = 0, traits=traits)