-
Notifications
You must be signed in to change notification settings - Fork 1
/
abcmodels.intrinsic.R
635 lines (592 loc) · 24.2 KB
/
abcmodels.intrinsic.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
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
#' Intrinsic Character Evolution Models
#'
#' Functions describing various models of 'intrinsic' evolution (i.e. evolutionary processes intrinsic to the evolving
#' lineage, independent of other evolving lineages (competitors, predators, etc).
#'
#' @details
#' The following intrinsic models are:
#'
#' \code{nullIntrinsic} describes a model of no intrinsic character change.
#' It has no parameters, really.
#'
#' \code{brownianIntrinsic} describes a model of intrinsic character evolution via
#' Brownian motion. The input parameters for this model are:
#' \code{boundaryIntrinsic} with parameters \code{params = sigma}
#'
#' \code{boundaryIntrinsic} describes a model of intrinsic character evolution where character
#' change is restricted above a minimum and below a maximum threshold.
#' The input parameters for this model are:
#' \code{boundaryMinIntrinsic} with parameters \code{params = sigma, minimum, maximum}
#'
#' \code{boundaryMinIntrinsic} describes a model of intrinsic character evolution where character
#' change is restricted above a minimum threshold.
#' The input parameters for this model are:
#' \code{boundaryMinIntrinsic} with parameters \code{params = sigma, minimum}
#'
#' \code{autoregressiveIntrinsic} describes a model of intrinsic character evolution.
#' New character values are generated after one time step via a discrete-time OU process.
#' The input parameters for this model are:
#' \code{autoregressiveIntrinsic} with
#' \code{params = sigma (sigma), attractor (character mean), attraction (alpha)}
#'
#' \code{minBoundaryAutoregressiveIntrinsic} describes a model of intrinsic character evolution. New
#' character values are generated after one time step via a discrete-time OU
#' process with a minimum bound.
#' The input parameters for this model are:
#' \code{MinBoundaryAutoregressiveIntrinsic} with parameters \code{params = sigma (sigma), attractor
#' (character mean), attraction (alpha), minimum}
#'
#' \code{autoregressiveIntrinsicTimeSlices} describes a model of intrinsic character evolution. New
#' character values are generated after one time step via a discrete-time OU
#' process with differing means, sigma, and attraction over time.
#' In the various \emph{TimeSlices} models, time threshold units are in time before present
#' (i.e., 65 could be 65 MYA). The last time threshold should be 0.
#' The input parameters for this model are:
#' \code{autoregressiveIntrinsicTimeSlices} with parameters \code{params = sd-1 (sigma-1),
#' attractor-1 (character mean-1), attraction-1 (alpha-1), time threshold-1,
#' sd-2 (sigma-2), attractor-2 (character mean-2), attraction-2 (alpha-2), time
#' threshold-2}
#'
#' \code{autoregressiveIntrinsicTimeSlicesConstantMean} describes a model of intrinsic character evolution. New
#' character values are generated after one time step via a discrete-time OU
#' process with differing sigma and attraction over time
#' The input parameters for this model are:
#' \code{autoregressiveIntrinsicTimeSlicesConstantMean} with parameters \code{params = sd-1
#' (sigma-1), attraction-1 (alpha-1), time threshold-1, sd-2 (sigma-2),
#' attraction-2 (alpha-2), time threshold-2, attractor (character mean)}
#'
#' \code{autoregressiveIntrinsicTimeSlicesConstantSigma} describes a model of intrinsic character evolution. New
#' character values are generated after one time step via a discrete-time OU
#' process with differing means and attraction over time.
#' The input parameters for this model are:
#' \code{autoregressiveIntrinsicTimeSlicesConstantSigma} with parameters \code{params = sigma (sigma),
#' attractor-1 (character mean-1), attraction-1 (alpha-1), time threshold-1,
#' attractor-2 (character mean-2), attraction-2 (alpha-2), time threshold-2}
#'
#' @param params A vector containing input parameters for the given model (see \emph{Description} below on what parameters).
#' @param states Vector of current trait values for a taxon. May be multiple for some models, but generally expected to be
#' only a single value. Multivariate \code{TreEvo} is not yet supported.
#' @param timefrompresent The amount of time from the present - generally ignored except for time-dependent models.
#' @return
#' A vector of values representing character displacement of that lineage over a single time step.
#' @aliases abcmodels.intrinsic
#' @seealso Another intrinsic model with multiple optima is described at \code{\link{multiOptimaIntrinsic}}.
#' Extrinsic models are described at \code{\link{abcmodels.extrinsic}}.
#' @author Brian O'Meara and Barb Banbury
#' @examples
#
#' \donttest{
#
#' set.seed(1)
#' # Examples of simulations with various intrinsic models (and null extrinsic model)
#' tree <- rcoal(20)
#' # get realistic edge lengths
#' tree$edge.length <- tree$edge.length*20
#'
#' #Simple Brownian motion Intrinsic Model
#' char <- doSimulation(
#' phy = tree,
#' intrinsicFn = brownianIntrinsic,
#' extrinsicFn = nullExtrinsic,
#' startingValues = c(10), #root state
#' intrinsicValues = c(0.01),
#' extrinsicValues = c(0),
#' generation.time = 100000)
#'
#' # Simple model with BM, but a minimum bound at 0, max bound at 15
#' char <- doSimulation(
#' phy = tree,
#' intrinsicFn = boundaryIntrinsic,
#' extrinsicFn = nullExtrinsic,
#' startingValues = c(10), #root state
#' intrinsicValues = c(0.01, 0, 15),
#' extrinsicValues = c(0),
#' generation.time = 100000)
#'
#' # Autoregressive (Ornstein-Uhlenbeck) model
#' # with minimum bound at 0
#' char <- doSimulation(
#' phy = tree,
#' intrinsicFn = minBoundaryAutoregressiveIntrinsic,
#' extrinsicFn = nullExtrinsic,
#' startingValues = c(10), #root state
#' intrinsicValues = c(0.01, 3, 0.1, 0),
#' extrinsicValues = c(0),
#' generation.time = 100000)
#'
#' # Autoregressive (Ornstein-Uhlenbeck) model
#' # with max bound at 1
#' char <- doSimulation(
#' phy = tree,
#' intrinsicFn = maxBoundaryAutoregressiveIntrinsic,
#' extrinsicFn = nullExtrinsic,
#' startingValues = c(10), #root state
#' intrinsicValues = c(0.01, 3, 0.1, 1),
#' extrinsicValues = c(0),
#' generation.time = 100000)
#'
#' }
#intrinsic models
#note that these work for univariate, but need to be generalized for multivariate
#otherstates has one row per taxon, one column per state
#states is a vector for each taxon, with length = nchar
#' @name intrinsicModels
#' @rdname intrinsicModels
#' @export
nullIntrinsic <- function(params, states, timefrompresent) {
newdisplacement <- 0*states
return(newdisplacement)
}
#' @rdname intrinsicModels
#' @export
brownianIntrinsic <- function(params, states, timefrompresent) {
newdisplacement <- rnormFastZig(
nZig = length(states),
#mean = 0 because we ADD this to existing values
meanZig = 0,
sdZig = params
)
return(newdisplacement)
}
#' @rdname intrinsicModels
#' @export
boundaryIntrinsic <- function(params, states, timefrompresent) {
#params[1] is sigma, params[2] is min, params[3] is max. params[2] could be 0 or -Inf, for example
newdisplacement <- rnormFastZig(nZig = length(states),
meanZig = 0, sdZig = params[1])
for (i in 1:length(newdisplacement)) {
newstate <- newdisplacement[i]+states[i]
if (newstate<params[2]) { #newstate less than min
newdisplacement[i] <- params[2]-states[i] #so, rather than go below the minimum, this moves the new state to the minimum
}
if (newstate>params[3]) { #newstate greater than max
newdisplacement[i] <- params[3]-states[i] #so, rather than go above the maximum, this moves the new state to the maximum
}
}
return(newdisplacement)
}
#' @rdname intrinsicModels
#' @export
boundaryMinIntrinsic <- function(params, states, timefrompresent) {
#params[1] is sigma, params[2] is min boundary
newdisplacement <- rnormFastZig(
nZig = length(states),
meanZig = 0, sdZig = params[1]
)
for (i in 1:length(newdisplacement)) {
newstate <- newdisplacement[i]+states[i]
if (newstate<params[2]) { #newstate less than min
newdisplacement[i] <- params[2]-states[i] #so, rather than go below the minimum, this moves the new state to the minimum
}
}
return(newdisplacement)
}
#' @rdname intrinsicModels
#' @export
boundaryMaxIntrinsic <- function(params, states, timefrompresent) {
#params[1] is sigma, params[2] is max boundary
newdisplacement <- rnormFastZig(nZig = length(states),
meanZig = 0, sdZig = params[1]
)
for (i in 1:length(newdisplacement)) {
newstate <- newdisplacement[i]+states[i]
if (newstate>params[2]) { #newstate MORE than MAX
newdisplacement[i] <- params[2]-states[i] #so, rather than go ABOVE the MAXIMUM, this moves the new state to the maximum
}
}
return(newdisplacement)
}
#' @rdname intrinsicModels
#' @export
autoregressiveIntrinsic <- function(params, states, timefrompresent) {
#a discrete time OU, same sigma, mean, and attraction for all chars
# params[1] is sigma (sigma),
# params[2] is attractor (ie. character mean),
# params[3] is attraction (ie. alpha)
sigma <- params[1]
attractor <- params[2]
attraction <- params[3] #in this model, this should be between zero and one
#subtract current states because we want displacement
newdisplacement <- rnormFastZig(
nZig = length(states),
meanZig = (attractor-states)*attraction,
sdZig = sigma)
return(newdisplacement)
}
#' @rdname intrinsicModels
#' @export
maxBoundaryAutoregressiveIntrinsic <- function(params, states, timefrompresent) {
#a discrete time OU, same sigma, mean, and attraction for all chars
#params[1] is sigma (sigma), params[2] is attractor (ie. character mean),
#params[3] is attraction (ie. alpha), params[4] is max bound
sigma <- params[1]
attractor <- params[2]
attraction <- params[3] #in this model, this should be between zero and one
minBound <- params[4]
#subtract current states because we want displacement
newdisplacement <- rnormFastZig(
nZig = length(states),
meanZig = (attractor-states)*attraction,
sdZig = sigma)
#
#message(newdisplacement)
for (i in 1:length(newdisplacement)) {
newstate <- newdisplacement[i] + states[i]
#message(newstate)
#so, rather than go above the maximum, this moves the new state to the maximum
if (newstate > params[4]) { #newstate more than max
newdisplacement[i] <- params[4] - states[i]
}
}
return(newdisplacement)
}
#' @rdname intrinsicModels
#' @export
minBoundaryAutoregressiveIntrinsic <- function(params, states, timefrompresent) {
#a discrete time OU, same sigma, mean, and attraction for all chars
#params[1] is sigma (sigma), params[2] is attractor (ie. character mean),
#params[3] is attraction (ie. alpha), params[4] is min bound
sigma <- params[1]
attractor <- params[2]
attraction <- params[3] #in this model, this should be between zero and one
minBound <- params[4]
#
newdisplacement <- rnormFastZig(
nZig = length(states),
meanZig = (attractor-states)*attraction,
sdZig = sigma)
#message(newdisplacement)
for (i in 1:length(newdisplacement)) {
#subtract current states because we want displacement
newstate <- newdisplacement[i] + states[i]
#message(newstate)
#so, rather than go below the minimum, this moves the new state to the minimum
if (newstate <params[4]) { #newstate less than min
newdisplacement[i] <- params[4] - states[i]
}
}
return(newdisplacement)
}
#' @rdname intrinsicModels
#' @export
autoregressiveIntrinsicTimeSlices <- function(params, states, timefrompresent) {
#a discrete time OU, differing mean, sigma, and attraction with time
#params = [sd1, attractor1, attraction1, timethreshold1,
# sd2, attractor2, attraction2, timethreshold2, ...]
#time is time before present (i.e., 65 could be 65 MYA).
# The last time threshold should be 0,
# one before that is the end of the previous epoch, etc.
numRegimes <- length(params)/4
timeSliceVector = c(Inf, params[which(c(1:length(params))%%4 == 0)])
#message(timeSliceVector)
sigma <- params[1]
attractor <- params[2]
#in this model, attraction should be between zero and one
attraction <- params[3]
#message(paste("timefrompresent = ", timefrompresent))
for (regime in 1:numRegimes) {
#message(paste ("tryiing regime = ", regime))
if (timefrompresent<timeSliceVector[regime]) {
#message("timefrompresent>timeSliceVector[regime] == TRUE")
if (timefrompresent >= timeSliceVector[regime+1]) {
#message("timefrompresent <= timeSliceVector[regime+1] == TRUE")
#message(paste("choose regime ", regime, " so 4*(regime-1) = ", 4*(regime-1)))
sigma <- params[1+4*(regime-1)]
attractor <- params[2+4*(regime-1)]
attraction <- params[3+4*(regime-1)]
#message(paste("sigma = ", sigma, " attractor = ",
# attractor, " attraction = ", attraction))
}
}
}
#message(paste("sigma = ", sigma, " attractor = ",
# attractor, " attraction = ", attraction))
newdisplacement <- rnormFastZig(
nZig = length(states),
meanZig = (attractor-states)*attraction,
sdZig = sigma)
return(newdisplacement)
}
#' @rdname intrinsicModels
#' @export
autoregressiveIntrinsicTimeSlicesConstantMean <- function(params, states, timefrompresent) {
#a discrete time OU, constant mean, differing sigma, and differing attaction with time
#params = [sd1 (sigma1), attraction1 (alpha 1),
# timethreshold1, sd2 (sigma2), attraction2 (alpha 2),
# timethreshold2, ..., attractor (mean)]
#time is time before present (i.e., 65 could be 65 MYA).
# The last time threshold should be 0,
# one before that is the end of the previous epoch, etc.
numTimeSlices <- (length(params)-1)/3
sigma <- params[1]
attractor <- params[length(params)]
attraction <- params[2] #in this model, this should be between zero and one
previousThresholdTime <- Inf
for (slice in 0:(numTimeSlices-1)) {
thresholdTime <- params[3+3*slice]
if (thresholdTime >= timefrompresent) {
if (thresholdTime<previousThresholdTime) {
sigma <- params[1+3*slice]
attraction <- params[2+3*slice]
}
}
previousThresholdTime <- thresholdTime
}
newdisplacement <- rnormFastZig(
nZig = length(states),
meanZig = attraction*states + attractor,
sdZig = sigma
)
newdisplacement <- newdisplacement-states
return(newdisplacement)
}
#' @rdname intrinsicModels
#' @export
autoregressiveIntrinsicTimeSlicesConstantSigma <- function(
params,
states,
timefrompresent
){
#############################
##a discrete time OU, differing mean, constant sigma, and attaction with time
#params = [sigma, attractor1, attraction1,
# timethreshold1, attractor2, attraction2, timethreshold2, ...]
#time is time before present (i.e., 65 could be 65 MYA). The
# last time threshold should be 0,
# one before that is the end of the previous epoch, etc.
numRegimes <- (length(params)-1)/3
#message(numRegimes)
timeSliceVector <- c(Inf)
for (regime in 1:numRegimes) {
timeSliceVector <- append(timeSliceVector, params[4+3*(regime-1)])
}
#timeSliceVector = c(Inf, params[which(c(1:length(params))%%4 == 0)])
#message(timeSliceVector)
sigma <- params[1]
attractor <- params[2]
#in this model, attraction should be between zero and one
attraction <- params[3]
#message(paste("timefrompresent = ", timefrompresent))
for (regime in 1:numRegimes) {
#message(paste ("trying regime = ", regime))
if (timefrompresent<timeSliceVector[regime]) {
#message("timefrompresent>timeSliceVector[regime] == TRUE")
if (timefrompresent >= timeSliceVector[regime+1]) {
#message("timefrompresent >= timeSliceVector[regime+1] == TRUE")
#message(paste("chose regime ", regime))
#sigma <- params[1+4*(regime-1)]
attractor <- params[2+3*(regime-1)]
attraction <- params[3+3*(regime-1)]
#message(paste("sigma = ", sigma, " attractor = ",
# attractor, " attraction = ", attraction))
}
}
}
# message(paste("sigma = ", sigma, " attractor = ",
# attractor, " attraction = ", attraction))
newdisplacement <- rnormFastZig(
nZig = length(states),
meanZig = (attractor-states)*attraction,
sdZig = sigma)
return(newdisplacement)
}
varyingBoundariesFixedSigmaIntrinsic <- function(params, states, timefrompresent) {
#differing boundaries with time
#params = [sigma, min1, max1, timethreshold1, min2, max2, timethreshold2, ...]
#time is time before present (i.e., 65 could be 65 MYA). The last time (present)
# threshold should be 0, one before that is the end of the previous epoch, etc.
numRegimes <- (length(params)-1)/3
#message(numRegimes)
timeSliceVector <- c(Inf)
for (regime in 1:numRegimes) {
timeSliceVector <- append(timeSliceVector, params[4+3*(regime-1)])
}
#timeSliceVector = c(Inf, params[which(c(1:length(params))%%4 == 0)])
#message(timeSliceVector)
sigma <- params[1]
minBound <- params[2]
maxBound <- params[3]
for (regime in 1:numRegimes) {
#message(paste ("trying regime = ", regime))
if (timefrompresent<timeSliceVector[regime]) {
#message("timefrompresent>timeSliceVector[regime] == TRUE")
if (timefrompresent >= timeSliceVector[regime+1]) {
#message("timefrompresent >= timeSliceVector[regime+1] == TRUE")
#message(paste("chose regime ", regime))
#sigma <- params[1+4*(regime-1)]
minBound <- params[2+3*(regime-1)]
maxBound <- params[3+3*(regime-1)]
#message(paste("sigma = ", sigma, " attractor = ",
# attractor, " attraction = ", attraction))
}
}
}
#message(paste("sigma = ", sigma, " attractor = ",
#attractor, " attraction = ", attraction))
#
newdisplacement <- rnormFastZig(
nZig = length(states),
meanZig = 0,
sdZig = sigma)
for (i in 1:length(newdisplacement)) {
newstate <- newdisplacement[i]+states[i]
#
# is newstate less than min?
if (newstate<minBound) {
#so, rather than go below the minimum,
# this moves the new state to the minimum
newdisplacement[i] <- minBound-states[i]
}
# is newstate greater than max?
if (newstate>maxBound) {
# if so, rather than go above the maximum, this
# moves the new state to the maximum
newdisplacement[i] <- maxBound-states[i]
}
}
return(newdisplacement)
}
varyingBoundariesVaryingSigmaIntrinsic <- function(params, states, timefrompresent) {
#differing boundaries with time
#params = [sd1, min1, max1, timethreshold1,
# sd2, min2, max2, timethreshold2, ...]
#time is time before present (i.e., 65 could be 65 MYA).
# The last time (present) threshold should be 0,
# one before that is the end of the previous epoch, etc.
numRegimes <- (length(params))/3
#message(numRegimes)
timeSliceVector <- c(Inf)
for (regime in 1:numRegimes) {
timeSliceVector <- append(timeSliceVector, params[4+4*(regime-1)])
}
#timeSliceVector = c(Inf, params[which(c(1:length(params))%%4 == 0)])
#message(timeSliceVector)
sigma <- params[1]
minBound <- params[2]
maxBound <- params[3]
for (regime in 1:numRegimes) {
#message(paste ("trying regime = ", regime))
if (timefrompresent<timeSliceVector[regime]) {
#message("timefrompresent>timeSliceVector[regime] == TRUE")
if (timefrompresent >= timeSliceVector[regime+1]) {
#message("timefrompresent >= timeSliceVector[regime+1] == TRUE")
#message(paste("chose regime ", regime))
#sigma <- params[1+4*(regime-1)]
sigma <- params[1+4*(regime-1)]
minBound <- params[2+4*(regime-1)]
maxBound <- params[3+4*(regime-1)]
# message(paste("sigma = ", sigma, " attractor = ",
# attractor, " attraction = ", attraction))
}
}
}
#message(paste("sigma = ", sigma, " attractor = ",
#attractor, " attraction = ", attraction))
newdisplacement <- rnormFastZig(
nZig = length(states),
meanZig = 0,
sdZig = sigma)
for (i in 1:length(newdisplacement)) {
newstate <- newdisplacement[i]+states[i]
#is newstate less than min?
if (newstate<minBound) {
# so, rather than go below the minimum,
# this moves the new state to the minimum
newdisplacement[i] <- minBound-states[i]
}
#
# is newstate greater than max?
if (newstate>maxBound) {
#so, rather than go above the maximum
# this moves the new state to the maximum
newdisplacement[i] <- maxBound-states[i]
}
}
return(newdisplacement)
}
#this model assumes a pull (perhaps weak) to a
# certain genome size, but with occasional doublings
genomeDuplicationAttraction <- function(
params, states, timefrompresent
) {
#params = [sigma, attractor, attraction, doubling.prob]
sigma <- params[1]
attractor <- params[2]
#in this model, attraction should be between zero and one
attraction <- params[3]
doubling.prob <- params[4]
newdisplacement <- rnormFastZig(
nZig = length(states),
meanZig = (attractor-states)*attraction,
sdZig = sigma)
#subtract current states because we want displacement ?
for (i in 1:length(newdisplacement)){
newstate <- newdisplacement[i]+states[i]
# is newstate less than min
if (newstate<0){
#if so, rather than go below the minimum
# this moves the new state to the minimum
newdisplacement[i] <- 0-states[i]
}
}
if (runif(1, 0, 1)<doubling.prob) { #we double
newdisplacement <- states
}
return(newdisplacement)
}
#This is the same as the above model, but where the states are in log units
# The only difference is how doubling occurs
genomeDuplicationAttractionLogScale <- function(params, states, timefrompresent) {
#params = [sigma, attractor, attraction, doubling.prob]
sigma <- params[1]
attractor <- params[2]
#in this model, attraction should be between zero and one
attraction <- params[3]
doubling.prob <- params[4]
newdisplacement <- rnormFastZig(
nZig = length(states),
meanZig = (attractor-states)*attraction,
sdZig = sigma)
#subtract current states because we want displacement ?
if (runif(1, 0, 1)<doubling.prob) { #we double
newdisplacement <- log(2*exp(states))-states
}
return(newdisplacement)
}
# Genome duplication, but with no attraction.
# However, each duplication may shortly result in less than a full doubling.
# Basically, the increased size is based on a beta distribution.
# If you want pure doubling only, shape param 1 = Inf and param 2 = 1
genomeDuplicationPartialDoublingLogScale <- function(params, states, timefrompresent){
#params = [sigma, shape1, doubling.prob]
sigma <- params[1]
#the larger beta.shape1 is,
# the more the duplication is exactly a doubling.
# To see what this looks like,
# plot(density(1+rbeta(10000, beta.shape1, 1)))
beta.shape1 <- params[2]
duplication.prob <- params[3]
newdisplacement <- rnormFastZig(
nZig = length(states),
meanZig = 0,
sdZig = sigma)
if (runif(1, 0, 1)<duplication.prob) { #we duplicate
newdisplacement <- log((1+rbeta(1, beta.shape1, 1))*exp(states))-states
}
return(newdisplacement)
}
##Get Genome duplication priors
GetGenomeDuplicationPriors <- function(numSteps, phy, data) {
#returns a matrix with 3 priors for genome duplication
# (genomeDuplicationPartialDoublingLogScale)
timeStep <- 1/numSteps #out of doRun_rej code
#new TreEvo function
sigma <- getBMRatePrior(phy=phy, traits=data, timeStep=timeStep)
beta.shape1 <- 1
#for(i in 1:10) {
# lines(density(1+rbeta(10000, 10^runif(1, 0, 2), 1)), xlim = c(1, 2))
# }
# seems to produce nice distributions, but how to justify using 3?
#exponential, but which rate?
duplication.prob <- 2
}