## ----comment=">"-------------------------------------------------------------- # Load the package and dependencies (ape, phytools, corpcor, subplex, spam) library(mvMORPH) # Use a specified random number seed for reproducibility set.seed(14) ## ----------------------------------------------------------------------------- tree<-pbtree(n=100) ## ----comment=">"-------------------------------------------------------------- # Simulate two selective regimes state<-as.vector(c(rep("Forest",60),rep("Savannah",40))); names(state)<-tree$tip.label # Make the tree with mapped states using SIMMAP tree<-make.simmap(tree, state, model="ER", nsim=1) ## ----comment=">"-------------------------------------------------------------- # Plot the phylogeny with the mapped discrete trait col<-c("blue","orange"); names(col)<-c("Forest","Savannah") plotSimmap(tree,col, fsize=0.6, node.numbers=FALSE, lwd=3, pts=FALSE) ## ----comment=">"-------------------------------------------------------------- # 2 Random traits evolving along the phylogeny as a two-optimum OU set.seed(101) alpha<-matrix(c(1.1,-0.9,-0.9,1),2) sigma<-matrix(c(0.35,0.06,0.06,0.35),2) theta<-c(5.5,5.1,1.2,1.4) data<-mvSIM(tree, param=list(sigma=sigma, alpha=alpha, ntraits=2, theta=theta, names_traits=c("limb.length","limb.width")), model="OUM", nsim=1) ## ----comment=">"-------------------------------------------------------------- # Fitting the Ornstein Uhlenbeck on the whole tree trait1_OU1<- mvOU(tree, data[,1], model="OU1", diagnostic=FALSE, echo=FALSE) trait2_OU1<- mvOU(tree, data[,2], model="OU1", diagnostic=FALSE, echo=FALSE) # Fitting the Ornstein Uhlenbeck with multiple optimums trait1_OUM<- mvOU(tree, data[,1], model="OUM", diagnostic=FALSE, echo=FALSE) trait2_OUM<- mvOU(tree, data[,2], model="OUM", diagnostic=FALSE, echo=FALSE) # Compare the AIC values between models fit AIC(trait1_OUM); AIC(trait1_OU1) AIC(trait2_OUM); AIC(trait2_OU1) # Now compare with the multivariate fit OUM<- mvOU(tree, data, model="OUM") OU1<- mvOU(tree, data, model="OU1") AIC(OUM); AIC(OU1) ## ----eval=FALSE--------------------------------------------------------------- # # # Simulate 1000 traits under the two optima (OUM) and the unique optimum OU (OU1) process # library(parallel) # nsim=1000 # # # Dataset simulated with the OUM maximum likelihood estimates # data1<-simulate(OUM, nsim=nsim, tree=tree) # # Dataset simulated with the MLE of the OU1 model # data2<-simulate(OU1, nsim=nsim, tree=tree) # # # Fit of the models using the parallel package (we will use 2 cores), can take a while... # # library(parallel) # nb_cores=2L # oum_data1<- mclapply(1:nsim, function(x){ # mvOU(tree, data1[[x]], model="OUM", method="sparse", diagnostic=F, echo=F) # }, mc.cores = getOption("mc.cores", nb_cores)) # # ou1_data1<- mclapply(1:nsim, function(x){ # mvOU(tree, data1[[x]], model="OU1", method="sparse", diagnostic=F, echo=F) # }, mc.cores = getOption("mc.cores", nb_cores)) # # # # Now same simulations on the second dataset # oum_data2<- mclapply(1:nsim, function(x){ # mvOU(tree, data2[[x]], model="OUM", method="sparse", diagnostic=F, echo=F) # }, mc.cores = getOption("mc.cores", nb_cores)) # # ou1_data2<- mclapply(1:nsim, function(x){ # mvOU(tree, data2[[x]], model="OU1", method="sparse", diagnostic=F, echo=F) # }, mc.cores = getOption("mc.cores", nb_cores)) # # # Retrieve the results from the simulations # OUM_simul<-sapply(1:nsim, function(x){ # c(oum_data1[[x]]$AICc,ou1_data1[[x]]$AICc) # }) # # OU1_simul<-sapply(1:nsim, function(x){ # c(oum_data2[[x]]$AICc,ou1_data2[[x]]$AICc) # }) # # # Now compute the type I error and power (type II) # sum(OU1_simul[1,]