-
Notifications
You must be signed in to change notification settings - Fork 1
/
misfits.r
executable file
·1581 lines (642 loc) · 30.5 KB
/
misfits.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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###The functions for the coalescent models were written by Hélène Morlon and publshed in the PLoSB (2010). I have modified them to some degree, but the original descriptions as she wrote them appear below:
library(ape)
library(picante)
library(laser)
library(geiger)
library(qpcR)
#This code computes the likelihood of a given phylogeny under the constant diversity/exponentially varying turnover rate model (Model 2 from the PloSB 2010 paper), with turnover rate at present tau0, exponential variation in turnover rate gamma, and diversity N0
getLikelihood.coalMoranEXP<-function(Vtimes,Ntips,tau0,gamma,N0)
{
Ttimes <- diff(Vtimes)
Vtimes<-Vtimes[2:length(Vtimes)]
nbint<-length(Ttimes)
samp<-seq((Ntips-2),(Ntips-nbint-1),by=-1)
indLikelihood<-samp*(samp+1)/2*2*tau0/N0*exp(gamma*Vtimes)*exp(-samp*(samp+1)/2*2*tau0/N0*1/gamma*exp(gamma*Vtimes)*(1-exp(-gamma*Ttimes)))
res<-sum(log(indLikelihood))
return(list("res"=res,"all"=indLikelihood))
}
#This code computes the likelihood of a given phylogeny under the constant diversity/constant turnover rate model (Model 1 from the PloSB 2010 paper), with turnover rate tau0 and diversity N0
getLikelihood.coalMoranCST<-function(Vtimes,Ntips,tau0,N0)
{
Ttimes <- diff(Vtimes)
nbint<-length(Ttimes)
samp<-seq((Ntips-2),(Ntips-nbint-1),by=-1)
indLikelihood<-samp*(samp+1)/2*2*tau0/N0*exp(-samp*(samp+1)/2*2*tau0/N0*Ttimes)
res<-sum(log(indLikelihood))
return(list("res"=res,"all"=indLikelihood))
}
#This code computes the likelihood of a given phylogeny under various flavors of the birth-death model (Models 2 to 6 from the PloSB 2010 paper), with parameters lamb0,alpha,mu0 and beta, and diversity N0
getLikelihood.coalBD <- function(Vtimes,Ntips,lamb0,alpha,mu0,beta,N0,pos=TRUE)
# The extinction rate is forced to be less than the speciation rate over the history of the clade
{
Ttimes <- diff(Vtimes)
Vtimes <- Vtimes[2:length(Vtimes)]
nbint<-length(Ttimes)
samp<-seq((Ntips-2),(Ntips-nbint-1),by=-1)
times<-c(0,sort(Vtimes))
if (min(abs(lamb0)*exp(alpha*times)-abs(mu0)*exp(beta*times))<=0)
{
indLikelihood<-0*vector(length=length(samp))
res<-sum(log(indLikelihood))
}
else if ((alpha==0) & (beta==0))
{ if (pos==FALSE)
{r<-lamb0-mu0
indLikelihood<-samp*(samp+1)/2*2*lamb0/N0*1/(exp(-r*Vtimes))*exp(-samp*(samp+1)/2*2*lamb0/N0/r*exp(r*Vtimes)*(1-exp(-r*Ttimes)))}
else
{r<-abs(abs(lamb0)-abs(mu0))
indLikelihood<-samp*(samp+1)/2*2*abs(lamb0)/N0*1/(exp(-r*Vtimes))*exp(-samp*(samp+1)/2*2*abs(lamb0)/N0/r*exp(r*Vtimes)*(1-exp(-r*Ttimes)))}
res<-sum(log(indLikelihood))
}
else
{
if ((beta==0) & !(alpha==0))
{ if (pos==FALSE)
{demfun<-function(x){2*lamb0*exp(alpha*x)/(N0*exp(lamb0/alpha*(1-exp(alpha*x))+mu0*x))}}
else {demfun<-function(x){2*abs(lamb0)*exp(alpha*x)/(N0*exp(abs(lamb0)/alpha*(1-exp(alpha*x))+abs(mu0)*x))}}
}
else if ((alpha==0) & !(beta==0))
{ if (pos==FALSE)
{demfun<-function(x){2*lamb0/(N0*exp(-lamb0*x-mu0/beta*(1-exp(beta*x))))}}
else
{demfun<-function(x){2*abs(lamb0)/(N0*exp(-abs(lamb0)*x-abs(mu0)/beta*(1-exp(beta*x))))}}
}
else { if (pos==FALSE)
{demfun<-function(x){2*lamb0*exp(alpha*x)/(N0*exp(lamb0/alpha*(1-exp(alpha*x))-mu0/beta*(1-exp(beta*x))))}}
else {demfun<-function(x){2*abs(lamb0)*exp(alpha*x)/(N0*exp(abs(lamb0)/alpha*(1-exp(alpha*x))-abs(mu0)/beta*(1-exp(beta*x))))}}
}
if (FALSE %in% is.finite(demfun(Vtimes)))
{
indLikelihood<-0*vector(length=length(samp))
res<-sum(log(indLikelihood))}
else
{
integrals<-c()
demfunval<-c()
for (i in 1:length(Vtimes))
{
demfunvali<-demfun(Vtimes[i])
integrali<-integrate(demfun,(Vtimes[i]-Ttimes[i]),Vtimes[i],stop.on.error=FALSE)$value
demfunval<-c(demfunval,demfunvali)
integrals<-c(integrals,integrali)}
indLikelihood<-samp*(samp+1)/2*demfunval*exp(-samp*(samp+1)/2*integrals)
res<-sum(log(indLikelihood))
}
}
return(list("res"=res,"all"=indLikelihood))}
#This code fits the constant diversity/exponentially varying turnover rate model (Model 2 from the PloSB 2010 paper) to a given phylogeny, by maximum likelihood, using the Nelder-Mead algorithm
#Outputs are the log-likelihood, the second order Akaike's Information Criterion, and the maximum likelihood estimates of the turnover rate at present (tau0) and the exponential variation in turnover rate (gamma). See notations in the PloSB 2010 paper.
#The code uses the ape package
#The code uses the getLikelihood.coalMoranEXP code. use source("getLikelihood.coalMoranEXP.r")
fitcoalMoranEXP<-function (phylo, tau0=10^-2, gamma=1, meth = "Nelder-Mead", N0=0, Vtimes=FALSE)
#Assuming we know N0 (the total number of species at present), we estimate tau0 and gamma. If N0=0 (default), N0 is set to the #number of tips in the phylogeny (i.e. the phylogeny is assumed to be 100% complete). Otherwise, enter the number of species at #present.
#the input in tau0 and gamma are initial parameter values. Try several inital values to make sure you are not stuck in a local optimum.
#The code can take as input either a phylogeny (default, Vtimes=FALSE), or branching times (if Vtimes=TRUE)
{
if (Vtimes==TRUE) {
Vtimes<-sort(phylo)
Ntips<-length(phylo)+1
if (N0==0) {N0<-Ntips}
}
else{
Vtimes <- sort(branching.times(phylo))
Ntips<-Ntip(phylo)
if (N0==0) {N0<-Ntips}
}
init<-c(tau0,gamma)
nbpar<-length(init)
nbobs<-length(Vtimes)-1
optimLH.MoranEXP <- function(init) {
tau0 <- init[1]
gamma <- init[2]
LH <- getLikelihood.coalMoranEXP(Vtimes,Ntips,tau0,gamma,N0)$res
return(-LH)
}
temp<-suppressWarnings(optim(init, optimLH.MoranEXP, method = meth))
res <- list(model = "MoranEXP", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), tau0 = temp$par[1], gamma=temp$par[2])
return(res)
}
#This code fits the constant diversity/constant turnover rate model (Model 1 from the PloSB 2010 paper) to a given phylogeny, by maximum likelihood, using the Nelder-Mead algorithm
#Outputs are the log-likelihood, the second order Akaike's Information Criterion, and the maximum likelihood estimate of the turnover rate (tau0). See notations in the PloSB 2010 paper.
#The code uses the ape package
#The code uses the getLikelihood.coalMoranCST code. use source("getLikelihood.coalMoranCST.r")
fitcoalMoranCST<-function (phylo, tau0=1, meth = "Nelder-Mead", N0=0, Vtimes=FALSE)
#Assuming we know N0 (the total number of species at present), we estimate tau0. If N0=0 (default), N0 is set to the number of tips in the phylogeny (i.e. the phylogeny is assumed to be 100% complete). Otherwise, enter the number of species at present.
#the input in tau0 is an initial value for the turnover rate. Try several inital values to make sure you are not stuck in a local optimum.
#The code can take as input either a phylogeny (default, Vtimes=FALSE), or branching times (if Vtimes=TRUE)
{
if (Vtimes==TRUE) {
Vtimes<-sort(phylo)
Ntips<-length(phylo)+1
if (N0==0) {N0<-Ntips}
}
else{
Vtimes <- sort(branching.times(phylo))
Ntips<-Ntip(phylo)
if (N0==0) {N0<-Ntips}
}
init<-c(tau0)
nbpar<-length(init)
nbobs<-length(Vtimes)-1
optimLH.MoranCST <- function(init) {
tau0 <- init[1]
LH <- getLikelihood.coalMoranCST(Vtimes,Ntips,tau0,N0)$res
return(-LH)
}
temp<-suppressWarnings(optim(init, optimLH.MoranCST, method = meth))
res <- list(model = "MoranCST", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), tau0 = temp$par[1])
return(res)
}
#This code fits various flavors of the birth-death model (Models 2 to 6 from the PloSB 2010 paper) to a given phylogeny, by maximum #likelihood, using the Nelder-Mead algorithm
#Outputs are the log-likelihood, the second order Akaike's Information Criterion, and the maximum likelihood estimates of the parameters of diversification. Depending on the model, these parameters include a combination of the speciation rate at present #(lamb0), the exponential variation in speciation rate (alpha), the extinction rate at present (mu0), the exponential variation in extinction rate (beta) and the extinction fraction (extinction rate/speciation rate, eps). See notations in the PloSB 2010 paper.
#The code uses the ape package
#The code uses the getLikelihood.coalBD code. use source("getLikelihood.coalBD.r")
fitcoalBD<-function (phylo,lamb0=0.1,alpha=1,mu0=0.01,beta=0,meth = "Nelder-Mead",N0=0,cst.lamb=FALSE,cst.mu=FALSE,fix.eps=FALSE,mu.0=FALSE,pos=TRUE,Vtimes=FALSE)
#The default settings allow to fit the most general model where the rates of speciation and extinction vary over time without a fixed extinction fraction (Model 4d from the PloSB 2010 paper). cst.lamb=TRUE forces the speciation rate to be constant over time (used to #fit Models 3, 5 and 4b). cst.mu=TRUE forces the extinction rate to be constant over time (used to fit Models 3, and 4a). fix.eps forces the extinction fraction to be constant over time (used to fit Model 4c). mu.0=TRUE forces the extinction rate to 0 (used to fit Models 5 #and 6).
#pos=TRUE (the default) forces the rates of speciation and extinction to be positive. pos=FALSE removes this forcing.
{
if (Vtimes==TRUE) {
Vtimes<-sort(phylo)
Ntips<-length(phylo)+1
if (N0==0) {N0<-Ntips}
}
else{
Vtimes <- sort(branching.times(phylo))
Ntips<-Ntip(phylo)
if (N0==0) {N0<-Ntips}
}
nbobs<-length(Vtimes)-1
#pure birth constant rates (Model 5)
if (mu.0==TRUE & cst.lamb==TRUE)
{init<-c(lamb0)}
#birth-death constant rates (Model 3)
else if (cst.mu==TRUE & cst.lamb==TRUE)
{init <- c(lamb0,mu0)}
#pure birth varying speciation rate (Model 6)
else if (mu.0==TRUE & cst.lamb==FALSE)
{init<-c(lamb0,alpha)}
#birth-death varying speciation rate (Model 4a)
else if (cst.mu==TRUE & cst.lamb==FALSE)
{init <- c(lamb0,alpha,mu0)}
#birth-death varying extinction rate (Model 4b)
else if (cst.mu==FALSE & cst.lamb==TRUE)
{init <- c(lamb0,mu0,beta)}
#birth-death varying speciation rate and constant extinction fraction (Model 4c)
else if (fix.eps==TRUE)
{init <- c(lamb0,alpha,mu0/lamb0)}
#birth-death varying speciation and extinction rates (Model 4d)
else
{init = c(lamb0,alpha,mu0,beta)}
nbpar<-length(init)
############################################################
if (mu.0==TRUE & cst.lamb==TRUE)
{optimLH.coalBD <- function(init) {
lamb0 <- init[1]
LH <- getLikelihood.coalBD(Vtimes,Ntips,lamb0,alpha=0,mu0=0,beta=0,N0,pos=pos)$res
return(-LH)}
}
else if (cst.mu==TRUE & cst.lamb==TRUE)
{optimLH.coalBD <- function(init) {
lamb0 <- init[1]
mu0 <- init[2]
LH <- getLikelihood.coalBD(Vtimes,Ntips,lamb0,alpha=0,mu0,beta=0,N0,pos=pos)$res
return(-LH)}
}
else if (mu.0==TRUE & cst.lamb==FALSE)
{optimLH.coalBD <- function(init) {
lamb0 <- init[1]
alpha <- init[2]
LH <- getLikelihood.coalBD(Vtimes,Ntips,lamb0,alpha,mu0=0,beta=0,N0,pos=pos)$res
return(-LH)}
}
else if (cst.mu==TRUE & cst.lamb==FALSE)
{optimLH.coalBD <- function(init) {
lamb0 <- init[1]
alpha <- init[2]
mu0 <- init[3]
LH <- getLikelihood.coalBD(Vtimes,Ntips,lamb0,alpha,mu0,beta=0,N0,pos=pos)$res
return(-LH)}
}
else if (cst.mu==FALSE & cst.lamb==TRUE)
{optimLH.coalBD <- function(init) {
lamb0 <- init[1]
mu0 <- init[2]
beta<- init[3]
LH <- getLikelihood.coalBD(Vtimes,Ntips,lamb0,alpha=0,mu0,beta,N0,pos=pos)$res
return(-LH)}
}
else if (fix.eps==TRUE)
{optimLH.coalBD <- function(init) {
lamb0 <- init[1]
alpha <- init[2]
eps <- init[3]
LH <- getLikelihood.coalBD(Vtimes,Ntips,lamb0,alpha=0,mu0,beta,N0,pos=pos)$res
return(-LH)}
}
else
{optimLH.coalBD <- function(init) {
lamb0 <- init[1]
alpha <- init[2]
mu0 <- init[3]
beta <- init[4]
LH <- getLikelihood.coalBD(Vtimes,Ntips,lamb0,alpha,mu0,beta,N0,pos=pos)$res
return(-LH)}
}
#######################################################################################
temp <- optim(init, optimLH.coalBD, method = meth,control=list(ndeps=10^(-4)))
if (mu.0==TRUE & cst.lamb==TRUE)
{
if (pos==FALSE)
{res <- list(model = "Pure birth constant speciation", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = temp$par[1])}
else
{res <- list(model = "Pure birth constant speciation", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = abs(temp$par[1]))}}
else if (cst.mu==TRUE & cst.lamb==TRUE)
{
if (pos==FALSE)
{res <- list(model = "Birth-death constant rates", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = temp$par[1],mu0 = temp$par[2])}
else
{res <- list(model = "Birth-death constant rates", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = abs(temp$par[1]),mu0 = abs(temp$par[2]))}}
else if (mu.0==TRUE & cst.lamb==FALSE)
{
if (pos==FALSE)
{res <- list(model = "Pure birth varying speciation", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = temp$par[1], alpha = temp$par[2])}
else
{res <- list(model = "Pure birth varying speciation", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = abs(temp$par[1]), alpha = temp$par[2])}}
else if (cst.mu==TRUE & cst.lamb==FALSE)
{
if (pos==FALSE)
{res <- list(model = "Birth-death varying speciation constant extinction", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = temp$par[1], alpha = temp$par[2], mu0 = temp$par[3])}
else
{res <- list(model = "Birth-death varying speciation constant extinction", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = abs(temp$par[1]), alpha = temp$par[2], mu0 = abs(temp$par[3]))}}
else if (cst.mu==FALSE & cst.lamb==TRUE)
{
if (pos==FALSE)
{res <- list(model = "Birth-death constant speciation varying extinction", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = temp$par[1], mu0 = temp$par[2], beta = temp$par[3])}
else
{res <- list(model = "Birth-death constant speciation varying extinction", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = abs(temp$par[1]), mu0 = abs(temp$par[2]), beta = temp$par[3])}}
else if (fix.eps==TRUE)
{
if (pos==FALSE)
{res <- list(model = "Birth-death constant extinction fraction", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = temp$par[1], alpha = temp$par[2], eps = temp$par[3])}
else
{res <- list(model = "Birth-death constant extinction fraction", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = abs(temp$par[1]), alpha = temp$par[2], eps = abs(temp$par[3]))}}
else
{
if (pos==FALSE)
{res <- list(model = "Birth-death varying speciation and extinction", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = temp$par[1], alpha = temp$par[2], mu0 = temp$par[3],beta = temp$par[4])}
else
{res <- list(model = "Birth-death varying speciation and extinction", LH = -temp$value, aicc = 2 *
temp$value + 2*nbpar + 2*nbpar*(nbpar+1)/(nbobs-nbpar-1), lamb0 = abs(temp$par[1]), alpha = temp$par[2], mu0 = abs(temp$par[3]),beta = temp$par[4])}}
return(res)
}
#Monotonic decay of speciation rate, sensu Rabosky and Lovette 2008
fit.linear <- function(phy)
{
N <- length(phy$tip.label)
x <- c(0, branching.times(phy))
T <- max(x)
t <- T - x
lambda.t <- function(lambda, mT) lambda * (1 - t[3:N] / mT)
chi.t <- function(lambda, mT, time) exp(-((lambda * (time - T) * (-2 * mT + time + T)) / (2 * mT)))
linear.lik <- function(lambda, mT) lfactorial(N - 1) + sum(log(lambda.t(lambda, mT))) + sum(log(chi.t(lambda, mT, t[3:N]))) + 2 * log(chi.t(lambda, mT, t[2]))
lnL <- function(p)
{
lambda <- exp(p[1])
mT <- exp(p[2]) + T
-linear.lik(lambda, mT)
}
res <- NULL
temp <- optim(c(log(log(N) / T), log(T)), lnL)
res$LH <- -temp$value
res$lambda0 <- exp(temp$par[1])
res$mT <- exp(temp$par[2]) + T
res$AIC <- -2 * res$LH + 4
res
}
#Yule model with incomplete sampling
fit.yule.f <- function(phy, sampling.f = 1)
{
N <- length(phy$tip.label)
x <- c(0, branching.times(phy))
tau <- x[2]
t <- tau - x
ptt.f <- function(lambda, time, tau) sampling.f / (sampling.f - (sampling.f - 1) * exp(lambda * (time - tau)))
beta <- function(lambda, time) 1 - ptt.f(lambda, 0, time) * exp(-lambda * time)
yule.f.lik <- function(lambda) lfactorial(N - 1) + (N - 2) * log(lambda) + sum(log(ptt.f(lambda, t[3:N], tau))) + 2 * log(1 - beta(lambda, tau)) + sum(log(1 - beta(lambda, x[3:N])))
lnL <- function(p)
{
lambda <- exp(p[1])
-yule.f.lik(lambda)
}
res <- NULL
temp <- suppressWarnings(optim(log(0.5), lnL))
res$LH <- -temp$value
res$r <- exp(temp$par[1])
res$AIC <- -2 * res$LH + 2
res
}
inv.logit <- function(x) return(1 / (exp(-x) + 1));
logit <- function(p) return(log(p) - log(1-p));
#Birth-death model with incomplete sampling
fit.bd.f <- function(phy, sampling.f = 1)
{
N <- length(phy$tip.label)
x <- c(0, branching.times(phy))
tau <- x[2]
t <- tau - x
ptt.f <- function(r, eps, time, tau) ((eps - 1) * sampling.f * exp(r * (tau - time))) / ((-sampling.f * exp(r * (tau - time))) + eps + sampling.f - 1)
beta <- function(r, eps, time) 1 - ptt.f(r, eps, 0, time) * exp(-r * time)
bd.f.lik <- function(r, eps) lfactorial(N - 1) - (N - 2) * log(1 - eps) + (N - 2) * log(r) + sum(log(ptt.f(r, eps, t[3:N], tau))) + 2 * log(1 - beta(r, eps, tau)) + sum(log(1 - beta(r, eps, x[3:N])))
lnL <- function(p)
{
r <- exp(p[1])
eps <- inv.logit(p[2])
-bd.f.lik(r, eps)
}
res <- NULL
temp <- optim(c(log(0.1), logit(0.5)), lnL)
res$LH <- -temp$value
res$r <- exp(temp$par[1])
res$eps <- inv.logit(temp$par[2])
res$AIC <- -2 * res$LH + 4
res
}
#Model of Strathmann and Slatkin 1983, with hyperbolic decay of high initial speciation rate and constant extinction
fit.ss83 <- function(phy)
{
N <- length(phy$tip.label)
x <- c(0, branching.times(phy))
tau <- x[2]
t <- tau - x
lambda.t <- function(lambda0, mu, eps) lambda0 / (1 + eps * t[3:N])
rho.t <- function(k, mu, eps, tau, t) mu * (((k * log((eps * t + 1) / (eps * tau + 1))) / eps) - t + tau)
ptt.t <- function(k, mu, eps, t, tau)
{
foo <- function(x) exp(mu * (((k * log((eps * t + 1) / (eps * x + 1))) / eps) - t + x))
integ <- integrate(foo, t, tau)
res <- (1 / (1 + mu * integ$value))
res
}
chi.t <- function(k, mu, eps, t, tau) ptt.t(k, mu, eps, t, tau) * exp(rho.t(k, mu, eps, tau, t))
ss83.lik <- function(lambda0, mu, eps, k)
{
p1 <- 0
p2 <- 0
for(i in 3:N) p1 <- p1 + log(ptt.t(k, mu, eps, t[i], tau))
for(i in 3:N) p2 <- p2 + log(chi.t(k, mu, eps, t[i], tau))
-(lfactorial(N - 1) + sum(log(lambda.t(lambda0, mu, eps))) + p1 + log(chi.t(k, mu, eps, 0, tau) ^ 2) + p2)
}
lnL.ss83 <- function(p)
{
lambda0 <- exp(p[1])
mu <- exp(p[2])
k <- lambda0 / mu
eps <- mu / (lambda0 + mu)
ss83.lik(lambda0, mu, eps, k)
}
temp <- optim(c(log(0.5), log(0.1)), lnL.ss83)
res <- NULL
res$LH <- -temp$value
res$lambda0 <- exp(temp$par[1]) + exp(temp$par[2])
res$mu <- exp(temp$par[2])
res$eps <- res$mu / res$lambda0
res$k <- exp(temp$par[1]) / exp(temp$par[2])
res$AIC <- 2 * -res$LH + 4
res
}
bullet<-function(tree, f, N0, alpha,lamb0)
{
tree<-tree
if(missing(f)) {f <- 1
} else {
f <- f }
if(missing(N0)) {N0 <- 0
} else {
N0 <- N0 }
if(missing(alpha)) {alpha <- 0.001
} else {
alpha<-alpha}
sampling.f<-f