Examples in Summary and Analysis of Extension Program Evaluation
SAEPER: Association Tests for Nominal Variables
Packages used in this chapter
The following commands will install these packages if they
are not already installed:
if(!require(rcompanion)){install.packages("rcompanion")}
See the Handbook for information on these topics.
Post-hoc tests
For the following example of post-hoc pairwise testing, we’ll use the fisher.multcomp function from the package RVAideMemoire to make the task easier. Then we’ll use pairwise.table in the native stats package as an alternative.
Post-hoc pairwise Fisher’s exact tests with RVAideMemoire
###
--------------------------------------------------------------
### Post-hoc example, Fisher’s exact test, p. 79
### --------------------------------------------------------------
Input =("
Frequency Damaged Undamaged
Daily 1 24
Weekly 5 20
Monthly 14 11
Quarterly 11 14
")
Matriz = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
Matriz
fisher.test(Matriz,
alternative="two.sided")
p-value = 0.0001228
alternative hypothesis: two.sided
library(rcompanion)
PT = pairwiseNominalIndependence(Matriz,
fisher = TRUE,
gtest = FALSE,
chisq = FALSE,
digits = 3)
PT
Comparison p.Fisher p.adj.Fisher
1 Daily : Weekly 0.189000 0.227000
2 Daily : Monthly 0.000102 0.000612
3 Daily : Quarterly 0.001920 0.005760
4 Weekly : Monthly 0.018600 0.037200
5 Weekly : Quarterly 0.128000 0.192000
6 Monthly : Quarterly 0.572000 0.572000
library(rcompanion)
cldList(comparison = PT$Comparison,
p.value =
PT$p.adj.Fisher,
threshold = 0.05)
Group Letter MonoLetter
1 Daily a a
2 Weekly ab ab
3 Monthly c c
4 Quarterly bc bc
Summary of results
Frequency Damaged Letter
Daily 4% a
Weekly 20% ab
Quarterly 44% bc
Monthly 56% c
Groups sharing a letter are not significantlt different (alpha = 0.05)
# # #
See the Handbook for information on this topic.
Examples
Examples of Fisher’s exact test with data in a matrix
###
--------------------------------------------------------------
### Chipmunk example, Fisher’s exact test, p. 80
### --------------------------------------------------------------
Input =("
Distance Trill No.trill
10m 16 8
100m 3 18
")
Matriz = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
Matriz
fisher.test(Matriz,
alternative="two.sided")
p-value = 0.0006862
# # #
###
--------------------------------------------------------------
### Drosophila example, Fisher’s exact test, p. 81
### --------------------------------------------------------------
Input =("
Variation Synonymous Replacement
'Polymorphisms' 43 2
'Fixed differences' 17 7
")
Matriz = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
Matriz
fisher.test(Matriz,
alternative="two.sided")
p-value = 0.006653
# # #
###
--------------------------------------------------------------
### King penguin example, Fisher’s exact test, p. 81
### --------------------------------------------------------------
Input =("
Site Alive Dead
Lower 43 7
Middle 44 6
Upper 49 1
")
Matriz = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
Matriz
fisher.test(Matriz,
alternative="two.sided")
p-value = 0.08963
alternative hypothesis: two.sided
# # #
###
--------------------------------------------------------------
### Moray eel example, Fisher’s exact test, pp. 81–82
### --------------------------------------------------------------
Input =("
Site G.moringa G.vicinus
Grass 127 116
Sand 99 67
Border 264 161
")
Matriz = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
Matriz
fisher.test(Matriz,
alternative="two.sided")
p-value = 0.04438
alternative hypothesis: two.sided
# # #
###
--------------------------------------------------------------
### Herons example, Fisher’s exact test, p. 82
### --------------------------------------------------------------
Input =("
Site Heron Egret
Vegetation 15 8
Shoreline 20 5
Water 14 7
Structures 6 1
")
Matriz = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
Matriz
fisher.test(Matriz,
alternative="two.sided")
p-value = 0.5491
alternative hypothesis: two.sided
# # #
Graphing is discussed above in the “Chi-square Test of Independence” section.
Similar tests – McNemar’s test
Care is needed in setting up the data for McNemar’s test. For a before-and-after test, the contingency table is set-up as before and after as row and column headings, or vice-versa. Note that the total observations in the contingency table is equal to the number of experimental units. That is, in the following example there are 62 men, and the sum of the counts in the contingency table is 62. If you set up the table incorrectly, you might end with double this number, and this will not yield the correct results.
McNemar’s test with data in a matrix
### --------------------------------------------------------------
### Dysfunction example, McNemar test, pp. 82–83
### --------------------------------------------------------------
Input =("
Row After.no After.yes
Before.no 46 10
Before.yes 0 6
")
Matriz = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
Matriz
mcnemar.test(Matriz, correct=FALSE)
McNemar's chi-squared = 10, df = 1, p-value = 0.001565
# # #
McNemar’s test with data in a data frame
###
--------------------------------------------------------------
### Dysfunction example, McNemar test, pp. 82–83
### Example using cross-tabulation
### --------------------------------------------------------------
Input =("
ED.before ED.after Count
no no 46
no yes 10
yes no 0
yes yes 6
")
Data = read.table(textConnection(Input),header=TRUE)
Data.xtabs = xtabs(Count ~ ED.before + ED.after, data=Data)
Data.xtabs
ED.after
ED.before no yes
no 46 10
yes 0 6
mcnemar.test(Data.xtabs, correct=FALSE)
McNemar's chi-squared = 10, df = 1, p-value = 0.001565
# # #
How to do the test
Fisher’s exact test with data as a data frame
###
--------------------------------------------------------------
### Chipmunk example, Fisher’s exact test, SAS example, p. 83
### Example using cross-tabulation
### --------------------------------------------------------------
Input =("
Distance Sound Count
10m trill 16
10m notrill 8
100m trill 3
100m notrill 18
")
Data = read.table(textConnection(Input), header=TRUE)
Data.xtabs = xtabs(Count ~ Distance + Sound, data=Data)
Data.xtabs
Sound
Distance notrill trill
100m 18 3
10m 8 16
summary(Data.xtabs)
### Fisher’s exact test of independence
fisher.test(Data.xtabs,
alternative="two.sided")
p-value = 0.0006862
# # #
###
--------------------------------------------------------------
### Bird example, Fisher’s exact test, SAS example, p. 84
### Example using cross-tabulation
### --------------------------------------------------------------
Input =("
Bird Substrate Count
heron vegetation 15
heron shoreline 20
heron water 14
heron structures 6
egret vegetation 8
egret shoreline 5
egret water 7
egret structures 1
")
Data = read.table(textConnection(Input), header=TRUE)
Data.xtabs = xtabs(Count ~ Bird + Substrate, data=Data)
Data.xtabs
Substrate
Bird shoreline structures vegetation water
egret 5 1 8 7
heron 20 6 15 14
summary(Data.xtabs)
### Fisher’s exact test of independence
fisher.test(Data.xtabs,
alternative="two.sided")
p-value = 0.5491
alternative hypothesis: two.sided
# # #
Power analysis
To calculate power or required samples, follow examples in the “Chi-square Test of Independence” section.
There, the result was
N = 1640.537 # Total observations
compared with the value in the Handbook of Ntotal = 1523 for this section.