# Fisher's exact test in R - 2x4 table

I have a question on categorical data -

I have a 2x4 matrix and I want to test the difference in the number of males or females in each group, and so I entered the data into R.

So I input this:

A=c(31,7)
B=c(8,1)
C=c(39,16)
D=c(2,6)
tab=as.table(cbind(A,B,C,D))
row.names(tab)=c('males','females')
fisher.test(tab)


This is the output that I get -

Fisher's Exact Test for Count Data

data:  tab
p-value = 0.01077
alternative hypothesis: two.sided


The only statistical information I get is the p-value - how do I know how the groups are different?

In a sense this is analogous to a situation where you test for differences in group means with ANOVA and then perform a post hoc test, such as Tukey's HSD, to tell which groups are the ones that actually differ. But, there is no equivalent post hoc test for Fisher's test.

The only "post hoc" thing that comes to mind is to run all pairwise comparisons for the table, and correct the p-values accordingly with, e.g., the Bonferroni method.

For a Chi square test, you could check the residuals or simply the expected-observed counts. In addition, going throught the percentages of observations in each group would probably answer your question at least partly, and this could be used with either Fisher's or Chi square test.

In R these can be done as follows:

# Percentages for rows and columns
# These a higher proportion of females than males in group D
prop.table(tab, 1) # rows
prop.table(tab, 2) # columns

# Chi square residuals
# The largest residuals are in the group D
chisq.test(tab)$residuals # Chi square expected-observed chisq.test(tab)$expected-chisq.test(tab)\$observed

# Chi square "post hoc" test
# For Fisher you need to do this by hand
library(NCstats) # from rforge.net
chisqPostHoc(chisq.test(t(tab))) # for A-D
chisqPostHoc(chisq.test(tab)) # for gender

• Thanks - and how do I report it in writing? Is there a way to transform it into Pearson chi-square?
– DPY
Sep 18, 2013 at 1:55
• I'll give a couple of links here, see e.g., stats.stackexchange.com/questions/40542/… and yatani.jp/HCIstats/ChiSquare. Much of the specifics of reporting depends on what you are aiming at. But including the table and the test results (as shown in the documents behind the links) would be the minimum, I would say.
– anon
Sep 18, 2013 at 16:25
• Note that the package in the answer can be installed with source("http://www.rforge.net/NCStats/InstallNCStats.R"). Then the package is loaded with library(NCStats). Nov 10, 2017 at 14:39

As a follow-up to @JTT 's answer. The package rcompanion has a function for pairwise chi-square, fisher, or g-test of association, with options for p-value adjustment.

if(!require(rcompanion)){install.packages("rcompanion")}
library(rcompanion)

#   #   #

PT = pairwiseNominalIndependence(tab,
compare = "column",
method = "fdr")

PT