# How to get the right correlation matrix for correlated binary data [closed]

I am trying to generate correlated binary data in high dimensional in R using bindata lib given marginal probabilites and correlation matrix, but it give me always error

  Error in commonprob2sigma(commonprob, simulvals) :


Here is sample of code for only 10 variables:

marginal_probs =  c(9.795272e-01 , 9.331778e-01 , 6.764349e-01 , 9.884067e-02 ,9.522222e-05 , 3.499417e-03 , 2.380556e-05 , 9.826457e-01 , 9.628633e-01 ,8.874949e-01)

correlation_mat =

[,1]         [,2]         [,3]         [,4]          [,5]          [,6]          [,7]          [,8]          [,9]       [,10]
[1,]  1.0000000000  0.540258943  0.209031764  0.047879233 -6.750092e-02  0.0085672057  7.053822e-04  0.7840635867  0.6694665745  0.40604770
[2,]  0.5402589429  1.000000000  0.386910326  0.088622750 -3.646798e-02 -0.0454879132  1.305637e-03  0.4929722619  0.6613106007  0.61159373
[3,]  0.2090317635  0.386910326  1.000000000  0.229052428 -1.410984e-02 -0.0434598161 -7.054666e-03  0.1909793458  0.2831488805  0.49337866
[4,]  0.0478792330  0.088622750  0.229052428  1.000000000 -3.231892e-03 -0.0101705338 -1.615888e-03  0.0434012259  0.0646190283  0.11766286
[5,] -0.0675009217 -0.036467977 -0.014109837 -0.003231892  1.000000e+00 -0.0005782943 -4.761395e-05 -0.0734320072 -0.0496901947 -0.02740859
[6,]  0.0085672057 -0.045487913 -0.043459816 -0.010170534 -5.782943e-04  1.0000000000  8.233515e-02  0.0078752345  0.0095061395 -0.03886223
[7,]  0.0007053822  0.001305637 -0.007054666 -0.001615888 -4.761395e-05  0.0823351499  1.000000e+00  0.0006484086  0.0009582161  0.00173719
[8,]  0.7840635867  0.492972262  0.190979346  0.043401226 -7.343201e-02  0.0078752345  6.484086e-04  1.0000000000  0.6766830516  0.37325133
[9,]  0.6694665745  0.661310601  0.283148881  0.064619028 -4.969019e-02  0.0095061395  9.582161e-04  0.6766830516  1.0000000000  0.55158959
[10,]  0.4060477004  0.611593731  0.493378657  0.117662862 -2.740859e-02 -0.0388622278  1.737190e-03  0.3732513255  0.5515895878  1.00000000


This line:

     r <- rmvbin(10,margprob = marginal_probs, bincorr = correlation_mat)


Produces the following error:

Not all probabilities are between 0 and 1.
Error in Element ( 1 , 5 ): Admissible values are in [ 0 , 9.5222224867284e-05 ].
Error in Element ( 1 , 7 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 3 , 7 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 4 , 7 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 5 , 7 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 6 , 7 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 7 , 3 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 7 , 4 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 7 , 5 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 7 , 6 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 7 , 8 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 7 , 9 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 7 , 10 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 8 , 7 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 9 , 7 ): Admissible values are in [ 0 , 2.3805556216821e-05 ].
Error in Element ( 9 , 10 ): Admissible values are in [ 0.850358273621063 , 0.887494941319304 ].
Error in commonprob2sigma(commonprob, simulvals) :


I tried to use better inputs to be within range of each element with the following:

> corr_mat[1,5]=runif(1,min= 0,max = 9.5222224867284e-05)
> corr_mat[1,5]
[1] 8.459955e-05
> corr_mat[5,1]=8.459955e-05


and did the same for each element but for element (9,10) it give me the same error whatever value I use??

> r <- rmvbin(10,margprob = marginal_probs, bincorr = correlation_mat)
Error in Element ( 9 , 10 ): Admissible values are in [ 0.850358273621063 , 0.887494941319304 ].
Error in commonprob2sigma(commonprob, simulvals) :


Even after applying this:

> corr_mat[9,10]=0.887494941319304
> corr_mat[10,9]=0.887494941319304
> r <- rmvbin(10,margprob = probs[1:10], bincorr = corr_mat[1:10,1:10])


It still shows:

Error in Element ( 9 , 10 ): Admissible values are in [ 0.850358273621063 , 0.887494941319304 ].
Error in commonprob2sigma(commonprob, simulvals) :


And:

> corr_mat[9,10]=runif(1,min=0.850358273621063,max=0.887494941319304)
> corr_mat[9,10]
[1] 0.8520473
> corr_mat[10,9]=0.8520473
> r <- rmvbin(10,margprob = marginal_probs, bincorr = correlation_mat)


Shows:

Error in Element ( 9 , 10 ): Admissible values are in [ 0.850358273621063 , 0.887494941319304 ].
Error in commonprob2sigma(commonprob, simulvals) :


What's wrong in my code? How should I fix my correlation matrix? Thanks in advance

New contributor
Eman Ismail is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

## closed as off-topic by mkt, kjetil b halvorsen, whuber♦Feb 12 at 14:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – mkt, kjetil b halvorsen, whuber
If this question can be reworded to fit the rules in the help center, please edit the question.

• Study help("check.commonprob"). Then look at bincorr2commonprob(marginal_probs, correlation_mat). One obvious (but not the only) problem is that negative probabilities are calculated. The function obviously can't handle negative correlations. – Roland Feb 12 at 11:36
• I stopped reading at the point I looked at the upper $2\times 2$ quadrant of correlation_mat: because it obviously is not symmetric, it's not a correlation matrix. GIGO. Evidently some problem occurred in your computation of this matrix, but since you haven't explained how it was computed, there's no answerable question. – whuber Feb 12 at 14:27