Sri Priya
2019-Aug-19 06:36 UTC
[R] Simulate I x J contingency tables using correlation coefficient using bivariate normal distribution
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]]
Thierry Onkelinx
2019-Aug-19 09:22 UTC
[R] Simulate I x J contingency tables using correlation coefficient using bivariate normal distribution
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. >[[alternative HTML version deleted]]
Sri Priya
2019-Aug-19 16:24 UTC
[R] Simulate I x J contingency tables using correlation coefficient using bivariate normal distribution
Dear Thierry, Thank you very much for your answer. I got some idea about generating tables from bivariate normal from your code. But still there will be an argument to choose the lambda value. So, I am thinking whether to generate count data using margial CDF? Because one may wish to test the independence from any contingency table. I am planning to test the same for this kind of table versus multinomial tables. Once again thank you for your timely response. Regards Sripriya On Mon, Aug 19, 2019 at 2:53 PM Thierry Onkelinx <thierry.onkelinx at inbo.be> wrote:> 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. >> >[[alternative HTML version deleted]]
Abby Spurdle
2019-Aug-19 22:21 UTC
[R] Simulate I x J contingency tables using correlation coefficient using bivariate normal distribution
> 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 distributionThis requires a cautionary note. The method above is simple in the univariate case (where people would usually start with a uniform distribution), however, generalizing it to the multivariate case, is not as simple as Thierry as suggested. That doesn't necessarily mean that his solution is wrong. (Because there's more than one way of defining a bivariate Poisson distribution).