[R] Simulating contingency table (Basic question, help please)
Turner Rolf
r.turner at auckland.ac.nz
Wed Feb 25 05:11:49 CET 2009
________________________________________
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
######################################################################
More information about the R-help
mailing list