[R] Simulate I x J contingency tables using correlation coefficient using bivariate normal distribution

Thierry Onkelinx
Mon, 19 Aug 2019 09:22:59 -0000

Dear Sripriya,

Step 1. generate random values from a multivariate normal distribution
Step 2. convert the random values into probabilities
Step 3. convert the probabilities into values from the target distribution

library(mvtnorm)
n <- 1e3
correl <- 0.9
lambda <- c(10, 50)
sigma <- matrix(correl, ncol = length(lambda), nrow = length(lambda))
diag(sigma) <- 1
binorm <- rmvnorm(n, sigma = sigma)
bip <- apply(binorm, 2, pnorm)
bipois <- sapply(
  seq_along(lambda),
  function(i) {
    qpois(bip[, i], lambda = lambda[i])
  }
)
plot(bipois)
table(data.frame(bipois))

Best regards,

ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND
FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
thierry.onkelinx at inbo.be
Havenlaan 88 bus 73, 1000 Brussel
www.inbo.be

///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////

<https://www.inbo.be>


Op ma 19 aug. 2019 om 10:29 schreef Sri Priya <sri.chocho at gmail.com>:

> Dear R Users,
>
> I am interested in generating contingency tables from bivariate normal
> distribution using different correlation coefficient values.
>
> I am experimenting numerous ways of generating contingency tables, and one
> possible way is to generate from multinomial distribution.
>
> I wonder how to generate the count variables from a continuous distribution
> using correlation structure. Generating bivariate normal variables can be
> easily done using mvrnorm() in R. I am struggling to write R code for
> generating count from continuous variables.
>
> Any suggestions is very much appreciated.
>
> Thanks.
> Sripriya.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>