R 模拟事件发生次数的函数
我试图创建一个函数,它返回一个事件以特定概率发生的次数以及n发生变化的时间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
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”)
可以帮助你。我想你的意思是模拟…(不是刺激…)是的,没错