-1

尽管@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!!! 

谢谢你!

4

1 回答 1

1

首先,读取库并创建一个递归辅助函数:

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))
  }
}

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

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

lapply(trees, fixmytrees)

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

于 2015-12-04T03:58:40.183 回答