Performing Fisher’s exact test
In this tutorial we will be using data provided by Freeman and Campbell from The University of Sheffield that examined whether intra-muscular magnesium better treated chronic fatigue syndrome compared to a placebo treatment in 32 patients. The results of the study are provided in the table below.
Magnesium | Placebo | |
Felt better |
|
|
Did not feel better |
|
|
For this example we will manually put the data into R rather than
reading it from a file. Using the data.frame()
function we
can build a data frame object where we input the data from top to bottom
row-wise then left to right column-wise. We can do so by first providing
a name for the column, placing an equal sign, then using
c()
to record the data row-wise. The row.names
option can also be used to create a list of names for the rows in the
data frame.
<- data.frame(Magnesium = c(12, 3), Placebo = c(3, 14),
df row.names = c("Felt better", "Did not feel better"))
df
## Magnesium Placebo
## Felt better 12 3
## Did not feel better 3 14
We can now run the test by wrapping the data frame in the
fisher.test()
function. Like other functions we can change
whether we want the test to be two- or one-sided through the
alternative
option, however because the magnesium treatment
could have a better, worse, or no effect we should keep it to the
default two.sided
.
fisher.test(df)
##
## Fisher's Exact Test for Count Data
##
## data: df
## p-value = 0.001033
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 2.507119 159.407427
## sample estimates:
## odds ratio
## 16.41917
From the results of the test we can see that with a p-value less than
0.05 (p-value = 0.001033
) we can reject
H0</i> to conclude that the rows and columns are not
independent. In the context of our data, we can conclude that patients
given the magnesium treatment overall felt better than those who took
the placebo.</p>
Full code block
# Create a data frame
<- data.frame(Magnesium = c(12, 3),Placebo = c(3, 14),
df row.names = c("Felt better", "Did not feel better"))
df
# Run a two-sided Fisher's exact test
fisher.test(df)