bbbnc
2009-Feb-24 17:01 UTC
[R] Simulating contingency table (Basic question, help please)
I'd like to carry out a Monte Carlo simulation test where given data is a contingency table. I think this is something to do with using rmultinonom(), but I'm not sure how to code this, to simulate contingency tables. Could anyone please help with how to use R to simulate contingency tables like this? -- View this message in context: http://www.nabble.com/Simulating-contingency-table-%28Basic-question%2C-help-please%29-tp22184961p22184961.html Sent from the R help mailing list archive at Nabble.com.
Achim Zeileis
2009-Feb-24 19:56 UTC
[R] Simulating contingency table (Basic question, help please)
On Tue, 24 Feb 2009, bbbnc wrote:> I'd like to carry out a Monte Carlo simulation test where given data is a > contingency table. I think this is something to do with using rmultinonom(), > but I'm not sure how to code this, to simulate contingency tables. Could > anyone please help with how to use R to simulate contingency tables like > this?If you want a two-way table with certain fixed margins, you can use r2dtable() which implements the Patefield algorithm. For testing (conditional) independence of two variables based on r2dtable(), you can use coindep_test() in package "vcd". Z> -- > View this message in context: http://www.nabble.com/Simulating-contingency-table-%28Basic-question%2C-help-please%29-tp22184961p22184961.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. > >
Turner Rolf
2009-Feb-25 04:11 UTC
[R] Simulating contingency table (Basic question, help please)
________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of bbbnc [benwong7 at gmail.com] Sent: Wednesday, February 25, 2009 6:01 AM To: r-help at r-project.org Subject: [R] Simulating contingency table (Basic question, help please) I'd like to carry out a Monte Carlo simulation test where given data is a contingency table. I think this is something to do with using rmultinonom(), but I'm not sure how to code this, to simulate contingency tables. Could anyone please help with how to use R to simulate contingency tables like this? Let m be your matrix (contingency table). Do something like:> p <- (rowSums(m)%o%colSums(m))/(sum(m)^2) > X <- rmultinom(2000,sum(m),p)and then> simstat <- apply(X,2,function(x,k){chisq.test(matrix(x,nrow=k))$statistic},k=nrow(m))will give you simulated chi-squared statistics. If you want some other statistic then obviously you need to modify the function which is applied to the columns of X. Note that the simulation is done under the usual contingency table null hypothesis of independence or homogeneity. If you have something else in mind you'll need to modify accordingly. cheers, Rolf Turner ###################################################################### Attention: This e-mail message is privileged and confidential. If you are not the intended recipient please delete the message and notify the sender. Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshal www.marshalsoftware.com ######################################################################