【问题标题】:About generating matrix of random numbers following Bernoulli distribution with a probability matrix in R关于使用 R 中的概率矩阵生成遵循伯努利分布的随机数矩阵
【发布时间】:2019-12-31 03:26:17
【问题描述】:

假设概率矩阵是A(nxm),那么我想生成一个随机数矩阵(B),每个随机数遵循伯努利分布,在A 中具有相应的概率。这意味着B[i,j] ~ Bernoulli(A[i,j)。有没有人知道如何在 R 中方便地做到这一点?我什至试过rbinom(n*m,1,B),但没有用。

【问题讨论】:

    标签: r


    【解决方案1】:

    你可以使用

    B <- matrix(rbinom(n*m, 1, A), n, m)
    

    或者(接近你已经在 OP 中尝试过的示例)

    B <- A
    B[] <- rbinom(n*m, 1, B)
    

    一些示例数据(借自@chinsoon)

    set.seed(1000L)
    n = 3
    m = 3
    A = matrix(runif(9), n, m)
    

    【讨论】:

      【解决方案2】:

      不确定这是不是你要找的东西

      apply(A, 1L:2L, function(p) rbinom(1, 1, p))
      

      输出:

           [,1] [,2] [,3]
      [1,]    0    1    1
      [2,]    1    0    1
      [3,]    0    0    0
      

      数据:

      set.seed(1000L)
      A <- matrix(runif(9), 3, 3)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-26
        • 2014-08-16
        • 2019-05-10
        • 1970-01-01
        • 2016-06-30
        • 1970-01-01
        • 1970-01-01
        • 2019-02-01
        相关资源
        最近更新 更多