Hello all, Let say I have 2-way contingency table: Tab <- matrix(c(8, 10, 12, 6), nr = 2) and the Chi-squared test could not reject the independence: > chisq.test(Tab) Pearson's Chi-squared test with Yates' continuity correction data: Tab X-squared = 1.0125, df = 1, p-value = 0.3143 However I want to get all possible contingency tables under this independence scenario (one of them would obviously be the given table as, we could not reject the independence), and for each such table I want to calculate the Ch-sq statistic. Can somebody help me how to generate all such tables? Thanks and regards,
Are you basically asking for all possible permutations of the table? If so see ?permn in the combinat package. John Kane Kingston ON Canada> -----Original Message----- > From: bogaso.christofer at gmail.com > Sent: Sat, 01 Dec 2012 18:10:15 +0545 > To: r-help at r-project.org > Subject: [R] Getting all possible contingency tables > > Hello all, > > Let say I have 2-way contingency table: > > Tab <- matrix(c(8, 10, 12, 6), nr = 2) > > and the Chi-squared test could not reject the independence: > > > chisq.test(Tab) > > Pearson's Chi-squared test with Yates' continuity correction > > data: Tab > X-squared = 1.0125, df = 1, p-value = 0.3143 > > > However I want to get all possible contingency tables under this > independence scenario (one of them would obviously be the given table > as, we could not reject the independence), and for each such table I > want to calculate the Ch-sq statistic. > > Can somebody help me how to generate all such tables? > > Thanks and regards, > > ______________________________________________ > 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.____________________________________________________________ FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! Visit http://www.inbox.com/photosharing to find out more!
R can usually do almost anything but if the function exists I am not aware of it. Sorry. I did misunderstand what you were doing. John Kane Kingston ON Canada> -----Original Message----- > From: bogaso.christofer at gmail.com > Sent: Sat, 01 Dec 2012 19:33:09 +0545 > To: jrkrideau at inbox.com > Subject: Re: [R] Getting all possible contingency tables > > Thanks John for your reply. However still not clear how I should proceed. > > My goal is to generate all possible contingency tables. Basically I want > to see the distribution of Chi-squared Statistic under independence > (NULL). > > So I was thinking if I can generate all possible permutation of integer > numbers having sum equal to (8 + 10 + 12 + 6) = 36. Is there any R > function to do that? > > Thanks and regards, > > > On 01-12-2012 18:39, John Kane wrote: >> Are you basically asking for all possible permutations of the table? If >> so see ?permn in the combinat package. >> >> John Kane >> Kingston ON Canada >> >> >>> -----Original Message----- >>> From: bogaso.christofer at gmail.com >>> Sent: Sat, 01 Dec 2012 18:10:15 +0545 >>> To: r-help at r-project.org >>> Subject: [R] Getting all possible contingency tables >>> >>> Hello all, >>> >>> Let say I have 2-way contingency table: >>> >>> Tab <- matrix(c(8, 10, 12, 6), nr = 2) >>> >>> and the Chi-squared test could not reject the independence: >>> >>> > chisq.test(Tab) >>> >>> Pearson's Chi-squared test with Yates' continuity correction >>> >>> data: Tab >>> X-squared = 1.0125, df = 1, p-value = 0.3143 >>> >>> >>> However I want to get all possible contingency tables under this >>> independence scenario (one of them would obviously be the given table >>> as, we could not reject the independence), and for each such table I >>> want to calculate the Ch-sq statistic. >>> >>> Can somebody help me how to generate all such tables? >>> >>> Thanks and regards, >>> >>> ______________________________________________ >>> 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. >> ____________________________________________________________ >> FREE ONLINE PHOTOSHARING - Share your photos online with your friends >> and family! >> Visit http://www.inbox.com/photosharing to find out more! >> >> >>____________________________________________________________ FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
I believe you are looking for a detailed construction of Fisher's Exact Test. I have an example with code in my book and R package. The book discussion is Section 15.2 of Heiberger, Richard M., and Burt Holland (2004). Statistical Analysis and Data Display: An Intermediate Course with Examples in S-Plus, R, and SAS. Springer--Verlag, New York. http://springer.com/0-387-40270-5 Table 15.2 shows the full set of tables for the example. The package is HH install.packages("HH") The code to construct Table 15.2 is in file library/HH/scripts/Ch15-twtb.r Search for the line: ## study the construction of the Fisher Exact test and read through to the end of the glasses example. Rich On Sat, Dec 1, 2012 at 7:25 AM, Christofer Bogaso < bogaso.christofer@gmail.com> wrote:> Hello all, > > Let say I have 2-way contingency table: > > Tab <- matrix(c(8, 10, 12, 6), nr = 2) > > and the Chi-squared test could not reject the independence: > > > chisq.test(Tab) > > Pearson's Chi-squared test with Yates' continuity correction > > data: Tab > X-squared = 1.0125, df = 1, p-value = 0.3143 > > > However I want to get all possible contingency tables under this > independence scenario (one of them would obviously be the given table as, > we could not reject the independence), and for each such table I want to > calculate the Ch-sq statistic. > > Can somebody help me how to generate all such tables? > > Thanks and regards, > > ______________________________**________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide http://www.R-project.org/** > posting-guide.html <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Thanks Ted for your correction. I was depressed thinking that I did not understand the theory. However now it comes as just a plotting mistake! Thanks, On 02 December 2012 20:47:48, (Ted Harding) wrote:> On 02-Dec-2012 14:17:20 Christofer Bogaso wrote: >> Thanks Ted (and other) for your suggestion. Here I have implemented >> following: >> >> Tab <- matrix(c(8, 10, 12, 6), nr = 2) >> >> Simu_Number <- 50000 >> Tab_Simulate <- vector("list", length = Simu_Number) >> for (i in 1:Simu_Number) { >> Tab_Simulate[[i]] <- matrix(rmultinom(1, sum(Tab), rep(0.25, >> 4)), nrow = 2) ## All Cells have equal probability >> } >> Sample_ChiSq <- sapply(Tab_Simulate, function(x) { >> Statistic <- >> sum((chisq.test(as.table(x))$observed - >> chisq.test(as.table(x))$expected)^2/chisq.test(as.table(x))$expected) >> return(Statistic) >> }) >> >> length(Sample_ChiSq[Sample_ChiSq < qchisq(0.95, 1)])/Simu_Number >> >> hist(Sample_ChiSq, freq = FALSE) >> lines(dchisq(seq(min(Sample_ChiSq), max(Sample_ChiSq), by = 0.5), 1)) >> >> >> However I think I am making some serious mistake as histogram did not >> match the density curve. >> >> Can somebody help me where I am making mistake? >> >> Thanks and regards, >> [the remainder (copies of previous posts) snipped] > > The reasons for the mis-match are: > > A: that you have put the curve in the wrong place, by not > supplying x-coordinates to lines(), so that it plots its > points at x = 1,2,3,4,... > > B: that you need to multiply the plotted density by the width > of the histogram cells, so as to match the density curve to the > discrete density of the histogram. It will also then look better > when the chis-squared curve is plotted at the mid-points of the cells. > > Hence, try something like: > > hist(Sample_ChiSq, freq = FALSE, breaks=0.5*(0:40)) > x0 <- 0.25+0.5*(0:20) > lines(x0,dchisq(x0,1)) > > Hoping this helps, > Ted. > > ------------------------------------------------- > E-Mail: (Ted Harding) <Ted.Harding at wlandres.net> > Date: 02-Dec-2012 Time: 15:02:45 > This message was sent by XFMail > ------------------------------------------------- >