假设我有三个变量,其相关矩阵如下:
y1 y2 y3
y1 1.0 0.2 0.3
y2 0.2 1.0 0.4
y3 0.3 0.4 1.0
我如何在此指定的相关参数下绘制一组n
随机bernoulli样本y1
,y2
,y3
(也许每个样本的指定概率他们)?如:
y1 y2 y3
1 1 1 0
2 0 1 1
3 0 0 0
4 0 0 1
5 1 0 0
6 0 1 0
....
n 1 0 0
答案 0 :(得分:1)
与this post一样,您可以使用rmvbin
功能:
# Correlation and marginal probabilities
cor.mat <- matrix(c(1, .2, .3, .2, 1, .4, .3, .4, 1), nrow=3)
marg.prob <- margprob <- c(.1, .2, .3)
# Generate random data
library(bindata)
set.seed(144)
res <- rmvbin(10000, margprob, bincorr=cor.mat)
# Check things are working properly
cor(res)
# [,1] [,2] [,3]
# [1,] 1.0000000 0.1891508 0.2965542
# [2,] 0.1891508 1.0000000 0.3927966
# [3,] 0.2965542 0.3927966 1.0000000
colMeans(res)
# [1] 0.1013 0.1980 0.3029