gpt4 book ai didi

r - 如何用R模拟相关的二进制数据?

转载 作者:行者123 更新时间:2023-12-04 22:13:30 27 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Generate correlated random numbers from binomial distributions

(3 个回答)


8年前关闭。




假设我想要 2 个具有指定 phi 系数的二进制数据向量,我如何用 R 模拟它?

例如,如何创建两个向量,如 xy指定向量长度的相关系数为 0.79

> x = c(1,  1,  0,  0,  1,  0,  1,  1,  1)
> y = c(1, 1, 0, 0, 0, 0, 1, 1, 1)
> cor(x,y)
[1] 0.7905694

最佳答案

bindata 包非常适合生成具有这种和更复杂相关结构的二进制数据。 ( Here's a link to a working paper (warning, pdf) 阐述了包作者采用的方法的理论基础。)

在您的情况下,假设 x 和 y 的独立概率均为 0.5:

library(bindata)

## Construct a binary correlation matrix
rho <- 0.7905694
m <- matrix(c(1,rho,rho,1), ncol=2)

## Simulate 10000 x-y pairs, and check that they have the specified
## correlation structure
x <- rmvbin(1e5, margprob = c(0.5, 0.5), bincorr = m)
cor(x)
# [,1] [,2]
# [1,] 1.0000000 0.7889613
# [2,] 0.7889613 1.0000000

关于r - 如何用R模拟相关的二进制数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16089178/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com