I am trying to find a way to generate correlated random numbers from several binomial distributions.
I know how to do it with normal distributions (using MASS::mvrnorm
), but I did not find a function applicable to binomial responses.
To generate correlated normally distributed random samples, one can first generate uncorrelated samples, and then multiply them by a matrix C such that CCT=R, where R is the desired covariance matrix. C can be created, for example, by using the Cholesky decomposition of R, or from the eigenvalues and eigenvectors of R.
r = binornd( n , p ) generates random numbers from the binomial distribution specified by the number of trials n and the probability of success for each trial p . n and p can be vectors, matrices, or multidimensional arrays of the same size. Alternatively, one or more arguments can be scalars.
2 The correlation of X and Y is the number defined by ρXY = Cov(X, Y ) σXσY . The value ρXY is also called the correlation coefficient. Theorem 4.5. 3 For any random variables X and Y , Cov(X, Y ) = EXY − µXµY .
In statistics, correlation or dependence is any statistical relationship, whether causal or not, between two random variables or bivariate data.
You can generate correlated uniforms using the copula
package, then use the qbinom
function to convert those to binomial variables. Here is one quick example:
library(copula)
tmp <- normalCopula( 0.75, dim=2 )
x <- rcopula(tmp, 1000)
x2 <- cbind( qbinom(x[,1], 10, 0.5), qbinom(x[,2], 15, 0.7) )
Now x2
is a matrix with the 2 columns representing 2 binomial variables that are correlated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With