Search code examples
rcategorical-datacontingency

Generate r x c contingency tables with bi-variate normal distribution in R


I want to generate contingency tables from bi-variate normal distribution using R. One way to generate tables using multi nominal distribution with rmultinom and other will be r2dtable, but i want to generate the cross classified data using bivariate normal with different correlated structure.

I have tried generating samples from bi-variate normal distribution with mean 0 and sigma as diag(2). After this I don't know to construct contingency tables.

library(MASS)

x<-mvrnorm(n=100, mu=c(0,0), Sigma=diag(2))

Please help me out with this. Thanks.


Solution

  • If you want to create a contingency table from your continuous data you will first have to transform it into categorical data and then do the counts on the categories. You will have to define breaks to something that makes sense for your analysis:

    library(MASS)
    
    x<-mvrnorm(n=100, mu=c(0,0), Sigma=diag(2))
    
    x <- apply(x,FUN = function(y) {cut(y,breaks = c(-Inf,-0.5,0.5,Inf))},MARGIN = 2)
    
    table(x[,1],x[,2])