# [R] fisher exact vs. simulated chi-square

Dirk Janssen dirkj at rz.uni-leipzig.de
Tue Apr 22 14:07:40 CEST 2003

```Dear All,

I have a problem understanding the difference between the outcome of a
fisher exact test and a chi-square test (with simulated p.value).

For some sample data (see below), fisher reports p=.02337. The normal
chi-square test complains about "approximation may be incorrect",
because there is a column with cells with very small values. I
therefore tried the chi-square with simulated p-values, but this still
gives p=.04037. I also simulated the p-value myself, using r2dtable,
getting the same result, p=0.04 (approx).

Why is this substantially higher than what the fisher exact says? Do
the two tests make different assumptions? I noticed that the
discrepancy gets smaller when I increase the number of observations
for column A3. Does this mean that the simulated chi-square is still
sensitive to cells with small counts, even though it does not give me
the warning?

Dirk Janssen

------------------------------------------------------------------

> ta <- matrix(c(45,85,27,32,40,34,1,2,1),nc=3,
dimnames=list(c("A","B","C"),c("A1","A2","A3")))
> ta
A1 A2 A3
A 45 32  1
B 85 40  2
C 27 34  1

> fisher.test(ta)

Fisher's Exact Test for Count Data

data:  ta
p-value = 0.02337
alternative hypothesis: two.sided

> chisq.test(ta, simulate=T, B=100000)

Pearson's Chi-squared test with simulated p-value (based on 1e+05
replicates)

data:  ta
X-squared = 9.6976, df = NA, p-value = 0.04037

> chisq.test(ta)

Pearson's Chi-squared test

data:  ta
X-squared = 9.6976, df = 4, p-value = 0.04584

Warning message:
Chi-squared approximation may be incorrect in: chisq.test(ta)

# simulate values by hand, based on r2dtable example
> expected <- outer(rowSums(ta), colSums(ta), "*") / sum(ta)
> meanSqResid <- function(x) mean((x - expected) ^ 2 / expected)
> sum(sapply(r2dtable(100000, rowSums(ta), colSums(ta)), meanSqResid)
>= meanSqResid(ta))/ 100000
 0.03939

#  is  similar to
> sum(sapply(r2dtable(100000, rowSums(ta), colSums(ta)),
function(x) { chisq.test(x)\$statistic })
>= 9.6976)/ 100000
 0.04044
There were 50 or more warnings (use warnings() to see the first 50)

```