简体   繁体   English

如何使用 R 创建简单的分支流程?

[英]How do I use R to create a simple branching process?

I'm trying to use R to make a simple branching process to calculate the population of foxes over 10 generations using我正在尝试使用 R 进行简单的分支过程来计算 10 代以上的狐狸数量

在此处输入图像描述

I'm using lambda = 1.5, and I'm aware that the number of foxes in the initial generation can be calculated using rpois(1, 1.5) .我正在使用 lambda = 1.5,并且我知道可以使用rpois(1, 1.5)计算初始生成中的狐狸数量。

However, I'm stuck on how to create a function that calculates the population of the next generations, and adds up the total population together.但是,我坚持如何创建一个 function 来计算下一代的人口,并将总人口加在一起。 If anyone could give some starting pointers that would be great.如果有人能给出一些起始点,那就太好了。

I believe you are looking for a function like the following:我相信您正在寻找如下 function :

pois_bprocess <- function(generations, lambda, trace = TRUE){
  z_i <- rpois(1, lambda)
  i <- 1
  
  if(trace) print(paste(i, ':', z_i))
  
  while(i < generations){
    # draw z_i samples from a poisson distribution with lambda and sum them
    z_i <- sum(rpois(z_i, lambda)) 
    i <- i + 1
    if(trace) print(paste(i, ':', z_i))
  }
  
  return(z_i)
}


# Example usage : pois_bprocess(10, 1.5)
# Example usage : pois_bprocess(10, 1.5, trace = F)

Note that as the assumed lambda for the Poisson distribution at 1.5 results in a fairly high the chance to draw a zero early on and get stuck.请注意,由于假设的 lambda 为 1.5 的泊松分布,导致早期绘制零并卡住的机会相当高。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM