2

I am trying to simulate variables knowing their marginal distribution and their correlation matrix. I know we can use packages like copula but I am not familiar on how to go about it. Can someone help

#mean(w1)=0.6, sd(w1)=0.38; w1 is normally distributed
#mean(w2)=0.31; w2 is binary
#mean(w3)=0.226; w3 is binary

cor
           w1         w2         w3
w1  1.0000000 -0.3555066 -0.1986376
w2 -0.3555066  1.0000000  0.1030849
w3 -0.1986376  0.1030849  1.0000000
4

1 回答 1

1

从这里的答案中得出:https ://stackoverflow.com/a/10540234/6455166

library(copula)
set.seed(123)
myCop <- normalCopula(param = c(-0.46, -0.27, 0.18), 
                      dim = 3, dispstr = "un")
out <- rCopula(1e5, myCop)
out[, 1] <- qnorm(out[, 1], mean = 0.6, sd = 0.38)
out[, 2] <- qbinom(out[, 2], size = 1, prob = 0.31)
out[, 3] <- qbinom(out[, 3], size = 1, prob = 0.226)

cor(out)
#            [,1]       [,2]       [,3]
# [1,]  1.0000000 -0.3548863 -0.1943631
# [2,] -0.3548863  1.0000000  0.1037638
# [3,] -0.1943631  0.1037638  1.0000000
colMeans(out)
# [1] 0.5992595 0.3118300 0.2256000
sd(out[, 1])
# [1] 0.3806173

解释。 我们绘制相关的制服,然后将制服的每个向量转换为我们想要的分布。paramin 参数的值normalCopula是通过反复试验得出的:从您想要的相关性(即c(-0.3555, -0.1986, 0.103))开始,然后调整它们直到cor(out)产生您的目标相关性。

于 2017-12-07T00:11:15.220 回答