【问题标题】:Generate binomial samples determining the probability生成确定概率的二项式样本
【发布时间】:2020-06-22 13:07:12
【问题描述】:

我想随机生成 n 个二进制值(0 或 1)的样本:

使用 runif 很容易:

samples <- round(runif(n,min=0, max=5))

但是有 0 或 1 的概率是 50%。但是,如果我想说明例如 30% 有 1 和 70% 有 0 的概率呢?

非常感谢你

【问题讨论】:

  • 那么你只需从二项分布中采样,即使用rbinom。在概率相等的情况下,您也应该这样做。
  • 如果你只有两个值,那么你可以使用samples <- as.numeric(runif(n)<.3)。对于更复杂的情况,请尝试 sample 函数:samples <- sample(0:1, n, replace=T, prob=c(.3, .7))
  • rbinom(n=10, size=1, prob=.3)?
  • 这能回答你的问题吗? sample() function in R

标签: r random statistics


【解决方案1】:

如果要从二项分布中抽样,请使用rbinom

set.seed(42) #for reproducibility
#the sample:
res <- rbinom(n = 1000, size = 1, prob = 0.3)

#check probabilities
mean(res)
#[1] 0.293

【讨论】:

    猜你喜欢
    • 2020-01-10
    • 2018-06-20
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多