在 R 中模拟 n 次复制的随机二项式序列
Simulate a random binomial sequence over n replications in R
我喜欢从带有 theta 参数的二项分布中绘制 nrep
次,为每个 theta 创建一个 k
长度序列,并将它们构建在矩阵维度 nrep x k
中。如何在 R 中创建结果矩阵?
下面的代码从不同的 theta 中提取 (nrep * k
),即序列不是来自相同 theta 的 k 长度。 [我的目标是绘制 nrep 乘以二项式概率 theta 长度 k。]
### simulate some binary sequence data in matrix ted (1000 x 20)
nrep <- 1000
s <- 7; k <- 20
theta <- rbeta(nrep, shape1=s+1, shape2=k-s+1)
ted <- 0
ted <- matrix(rbinom(k * nrep, 1, theta), ncol = k, nrow = nrep)
hist(ted)
rbinom
是在参数prob
上向量化的,所以可以用rep(theta, k)
来实现
ted <- matrix(rbinom(k * nrep, 1, rep(theta, k)), ncol = k, nrow = nrep)
我喜欢从带有 theta 参数的二项分布中绘制 nrep
次,为每个 theta 创建一个 k
长度序列,并将它们构建在矩阵维度 nrep x k
中。如何在 R 中创建结果矩阵?
下面的代码从不同的 theta 中提取 (nrep * k
),即序列不是来自相同 theta 的 k 长度。 [我的目标是绘制 nrep 乘以二项式概率 theta 长度 k。]
### simulate some binary sequence data in matrix ted (1000 x 20)
nrep <- 1000
s <- 7; k <- 20
theta <- rbeta(nrep, shape1=s+1, shape2=k-s+1)
ted <- 0
ted <- matrix(rbinom(k * nrep, 1, theta), ncol = k, nrow = nrep)
hist(ted)
rbinom
是在参数prob
上向量化的,所以可以用rep(theta, k)
来实现
ted <- matrix(rbinom(k * nrep, 1, rep(theta, k)), ncol = k, nrow = nrep)