Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 模拟事件发生次数的函数_R - Fatal编程技术网

R 模拟事件发生次数的函数

R 模拟事件发生次数的函数,r,R,我试图创建一个函数,它返回一个事件以特定概率发生的次数以及n发生变化的时间 X~Bin(n,p), p=0.1, n= varies 它会是这样的 happened <- function(n){ ...} 发生了这正是rbinom函数所做的。此函数接受如下所示的参数,并根据二项分布生成随机数目的绘图 rbinom(n = 50, #number of trials size = 1, # of size, eg 50 trials with either 1 or 0

我试图创建一个函数,它返回一个事件以特定概率发生的次数以及n发生变化的时间

X~Bin(n,p), p=0.1,
n= varies
它会是这样的

happened <- function(n){ ...}

发生了这正是
rbinom
函数所做的。此函数接受如下所示的参数,并根据二项分布生成随机数目的绘图

rbinom(n = 50, #number of trials 
       size = 1, # of size, eg 50 trials with either 1 or 0 outputs
       prob = .01) #probability of success
这将产生:

> rbinom(n = 20, size = 1, prob = .01)
 [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
现在,如果您想编写一个函数,可以根据您的需求执行以下操作:

successes <- function(n, size = 1, prob = 0.01){
  trials <- rbinom(n = n, size = size, prob = prob)

  sum(trials)
}

您所质疑的成功是一个不明确的问题。我怀疑
help(“qbinom”)
可以帮助你。我想你的意思是模拟…(不是刺激…)是的,没错