假設我想要2個具有指定phi係數的二進制數據向量,我怎麼能用R來模擬它?如何用R模擬相關的二進制數據?
例如,如何可以創建兩個向量像x
並與COR高效的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
假設我想要2個具有指定phi係數的二進制數據向量,我怎麼能用R來模擬它?如何用R模擬相關的二進制數據?
例如,如何可以創建兩個向量像x
並與COR高效的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包是與此,更復雜的生成二進制數據好的指定矢量長度的y
相關結構。 (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
複製? http://stackoverflow.com/a/10540234/2105757 – ndoogan
@ndoogan - 嗯,這是要求二進制數據,而不是二項式,所以它有點不同。 –
@ JoshO'Brien例如,單個硬幣翻轉的二項式模型和隨機二元模型之間有什麼區別? – ndoogan