如何将树木重新调整为极端形状?

How to rescale trees to extreme shapes?

虽然@jeremycg 帮助我开发了以下代码,但我又发布了这个。它有效,但它的表现不是我想要的! 快速提醒:我有一组树,我需要根据一个称为 gamma 的因子进行测量,如果每棵树的值不在定义的范围内,它将被重新缩放直到它的值在定义的范围内......让我们试试这个例子:

library(ape)
library(phytools)

trees<- pbtree(b=1, n=100, nsim=50)

 fixmytrees <- function(tree, rescaleamt = NULL){
if(is.null(rescaleamt)){
rescaleamt <- sample(seq(from = 0.1, to = 0.9, by = 0.1), 1)
} 
if(is.na(gammaStat(tree))){return("bad tree")}
if(gammaStat(tree) < 6){
 return(tree)
 } else {
return(rescale(tree, model ="delta", rescaleamt))
}
}
z<-lapply(tree, fixmytrees) 

 #The script does rescale trees but they are not extreme enough. In this case if you try

 gammaStat(z[[]]) #You would probably see values lower than 6 and sometimes NA!!! 

谢谢!

首先,阅读库并创建递归辅助函数:

library(ape)
library(geiger)
fixmytrees <- function(tree, rescaleamt = NULL){
  if(is.null(rescaleamt)){
    rescaleamt <- sample(seq(from = 0.1, to = 0.9, by = 0.1))
  } 
  if(gammaStat(tree) >= 1){
    return(tree)
  } else {
    return(fixmytrees(rescale(tree, "delta", rescaleamt), rescaleamt/2))
  }
}

此函数将获取一棵树,如果它通过了伽马检查,returns它。如果不是,它将从 seq(from = 0.1, to = 0.9, by = 0.1) 中获取一个随机数,并按该数量重新缩放树,然后使用新树和 rescaleamt/2.

再次调用该函数

所以现在我们只需要在您的树列表上执行此操作:

lapply(trees, fixmytrees)

注意。这不是对您的数据做的特别明智的事情,所以请确保您知道为什么要这样做。