Professional Documents
Culture Documents
Contingency PDF
Contingency PDF
Contingency Analysis
Mosaic Plots
Odds
Odds Ratio
SE & CI for Odds Ratio
2 Contingency Test
R example
Assumptions of 2
Correction for Continuity
Fisher's Exact Test
G-tests
Contingency Analysis
For example:
Mosaic Plots
3
Mosaic Plots
Odds
Let's consider a variable (e.g., our previous coin toss
example) for which a random trial yields one of two
outcomes: success or failure (heads or tails).
p
O=
1 p 5
Odds
- Example -
6
Odds
- Example -
Odds
- Example -
18496
p1 = =0.9279
19934
1 p 1 =10.9279=0.0721
So, the odds are ca. 12.87:1 of not getting cancer if taking aspirin.
8
Odds
- Example -
18515
p 2 = =0.9284
19942
1 p2 =10.9284=0.0716
p 0.9284
O 2 = 2 p 2= =12.97
1 0.0716
9
Odds Ratio
O1
O R=
O2
10
Odds Ratio
- Example -
O 1 12.86
O R= = =0.992
O 2 12.97
Odds Ratio
Because the data are highly skewed, we must convert the OR
to its natural log form and then calculate the SE from which we
can derive the CI:
SE [ ln O R]=
1 1 1 1
a b c d
SE [ ln O R]=
1
1
1
1
1438 1427 18496 18515
SE [ ln O R]=0.03878
12
Odds Ratio
Now that we have a SE calculated, we can calculate the
the 95% CI:
2 Contingency Test
14
2 Contingency Test
- Example -
15
2 Contingency Test
- Example -
16
17
2 Contingency Test
- Example -
[uninfected ]=50/141=0.3546
Pr
[eaten ]=48/141=0.3404
Pr
18
19
2 Statistic
c r 2
[Observed column , rowExpected column , row]
2 = .
[ Expected column , row]
column=1 row=1
NB : df = r1c1=2131=2
20
Example
How would we solve this problem in R? Basically, a row x
column table is a matrix; so in keeping with the approach
of using vectors for data, we create an array using the
matrix function and specify that the data are read by rows
(note how R cycles through the data to create a matrix from
a vector):
> fish<-matrix(c(1,49,10,35,37,9),nrow=2)
> fish
[,1] [,2] [,3]
[1,] 1 10 37
[2,] 49 35 9
21
Example
While we have a perfectly workable matrix, let's prettify it and
add the appropriate variable names and levels:
22
Example
> chisq.test(fish)
data: fish
X-squared = 69.7557, df = 2, p-value
= 7.124e-16
23
> chisq.test(fish)$observed
Infection
Predation Uninfected Light Heavy
Eaten 1 10 37
Not Eaten 49 35 9
> chisq.test(fish)$expected
Infection
Predation Uninfected Light Heavy
Eaten 17.02128 15.31915 15.65957
Not Eaten 32.97872 29.68085 30.34043
24
> mosaicplot(t(fish),cex=1.25,color=TRUE)
25
26
2
1
c r Observed column ,rowExpected column , row
2
= .
2
27
Fisher's Exact Test
29
> bats<-matrix(c(15,7,6,322),nrow=2)
> bats
[,1] [,2]
[1,] 15 6
[2,] 7 322
> fisher.test(bats)
data: bats
p-value < 2.2e-16
alternative hypothesis: true odds ratio
is not equal to 1
95 percent confidence interval:
29.94742 457.26860
sample estimates:
odds ratio
108.3894
30
G-tests
R code for G-test statistics are available, but are not part of the
normal stats base package or related packages.
31