library(ape)
library(phytools)
library(pspline)
data(Cetacea)
tot_time<-max(node.age(Cetacea)$ages)
data(InfTemp)
dof<-smooth.spline(InfTemp[,1], InfTemp[,2])$df
plot(Cetacea)
tot_time<-max(node.age(Cetacea)$ages)
# slice the Cetaceae tree 5 Myr ago:
time_stop=5
sliced_tree <- Cetacea
sliced_sub_trees <- treeSlice(sliced_tree,slice = tot_time-time_stop, trivial=TRUE)
for (i in 1:length(sliced_sub_trees)){
if (Ntip(sliced_sub_trees[[i]])>1){
sliced_tree <- drop.tip(sliced_tree,
tip=sliced_sub_trees[[i]]$tip.label[2:Ntip(sliced_sub_trees[[i]])])
}
}
for (i in which(node.depth.edgelength(sliced_tree)>(tot_time-time_stop))){
temp = sliced_tree$edge.length[which(sliced_tree$edge[,2]==i)]-time_stop
sliced_tree$edge.length[which(sliced_tree$edge[,2]==i)] <- temp
}
Ntip(sliced_tree) # 52 lineages present 5 Myr have survived until today
# Now we can fit environment-dependent birth-death models excluding the 5 last Myr
# Fits a model with lambda varying as an exponential function of temperature
# and mu fixed to 0 (no extinction). Here t stands for time and x for temperature.
f.lamb <-function(t,x,y){y[1] * exp(y[2] * x)}
f.mu<-function(t,x,y){0}
lamb_par<-c(0.10, 0.01)
mu_par<-c()
#result_env <- fit_env_in_past(sliced_tree, InfTemp, tot_time, time_stop, f.lamb,
# f.mu, lamb_par,mu_par,
# desc=Ntip(Cetacea), tot_desc=89,
# fix.mu=TRUE,df=dof,dt=1e-3)
Run the code above in your browser using DataLab