r语言 - 具有异质边际概率的相关二元变量



我想创建 50 个相关的二元变量,其中不同的变量具有不同的边际概率:

首先,我创建相关矩阵:

cor.mat=matrix(.9,nrow=50,ncol=50)
cor.mat[,9:11]=.1 
cor.mat[9:11,]=.1  
diag(cor.mat)=1 

然后我使用"rmvbin"生成数据:

library(bindata)
marg=rep(c(0.4,0.6),c(25,25))
a<-rmvbin(100, margprob=marg, bincorr=cor.mat)

但是,我收到以下错误:

Error in commonprob2sigma(commonprob, simulvals) : 
  Matrix commonprob not admissible.

我也尝试过用commonprob代替margprob:

common=rep(c(0.4,0.6),c(25,25))
a<-rmvbin(100, commonprob=common, bincorr=cor.mat)

但我得到:

Error in if (n != dim(commonprob)[2]) { : argument is of length zero
> 

我做错了什么?

将".9"更改为".4"或更低

cor.mat=matrix(0.4, nrow=50, ncol=50)
cor.mat[,9:11]=.1 
cor.mat[9:11,]=.1  
diag(cor.mat)=1 

这现在应该运行:

library(bindata)
marg=rep(c(0.4,0.6),c(25,25))
a<-rmvbin(100, margprob=marg, bincorr=cor.mat)

对于第二个错误:

1. commonprob需要是一个矩阵。

2. 只能指定commonprobbincorrsigma的一个参数。默认值为不相关的组件。

m <- cbind(c(1/2,1/5,1/6),c(1/5,1/2,1/6),c(1/6,1/6,1/2))
m
check.commonprob(m)
rmvbin(10,commonprob=m)
## or
## same as the example above, but faster if the same probabilities are
## used repeatedly (commonprob2sigma rather slow)
sigma <- commonprob2sigma(m)
rmvbin(10,margprob=diag(m),sigma=sigma)

来源:RMVBin 帮助文件中的示例

最新更新