-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphylo_sde2.R
211 lines (184 loc) · 8.42 KB
/
phylo_sde2.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
#'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, fossils = NULL, traits,
...) {
sde_edges <- function(fossils, tr, node, X0, t0, traits) {
daughters <- tr$edge[which(tr$edge[, 1] == node), 2]
if (any(daughters %in% fossils)) {
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]] <<- traits[fossils[which(fossils %in% daughters)]]
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 <- 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 <- 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)))))
n_steps <- tr$edge.length[edge] * N
tE <- t0 + tr$edge.length[edge]
X0 <- traits[fossils[which(fossils %in% daughters)]]
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, tr, root, lst[[edge]][n_steps +
1], tE, traits)
}
}
else {
for (d_ind in 1:2) {
edge <- which((tr$edge[, 1] == node) & (tr$edge[,
2] == daughters[d_ind]))
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 <- 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 <- 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)))))
n_steps <- tr$edge.length[edge] * N
tE <- t0 + tr$edge.length[edge]
if (n_steps == 0) {
lst[[edge]] <<- NA
tE <- t0
} 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]
}
if (daughters[d_ind] > n_tips) {
sde_edges(fossils, tr, daughters[d_ind], lst[[edge]][n_steps +
1], tE, traits)
}
}
}
}
lst <- list()
n_tips <- length(tr$tip.label)
rt_node <- n_tips + 1
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(fossils, tr, rt_node, X0 = rt_value, t0 = 0, traits)
return(lst)
}
## phylo_sde <- function(tr, rt_value, N, theta, model, method, 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(tr, node, X0, t0) {
## # preceeding nodes
## daughters <- tr$edge[which(tr$edge[, 1] == node), 2]
## 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]
## if (tr$edge.length[edge] == 0) {
## lst[[edge]] <<- NULL
## } 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]
## if (daughters[d_ind] > n_tips) {
## sde_edges(tr, daughters[d_ind], lst[[edge]][n_steps + 1], tE)
## }
## }
## }
## }
## sde_edges(tr, rt_node, X0 = rt_value, t0 = 0)
## return(lst)
## }