Skip to content
Prev 312635 / 398506 Next

Getting all possible contingency tables

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,
On 01-12-2012 21:45, (Ted Harding) wrote: