A R function applying G-test for 2-way contingency data in R is demonstrated. Yates’ correction and Williams’ correction are also included in the output.
R Source
Example
R code
my.2way.contingency.table <- matrix(
c(
5,0,3,12,6,
4,2,7,23,11
),
nrow = 2, ncol=5, byrow=T
)
g.test.result <- g.test.2way(my.2way.contingency.table)
g.test.result
str(g.test.result)
R output
> my.2way.contingency.table <- matrix(
+ c(
+ 5,0,3,12,6,
+ 4,2,7,23,11
+ ),
+ nrow = 2, ncol=5, byrow=T
+ )
>
> g.test.result <- g.test.2way(my.2way.contingency.table)
> g.test.result
Observed value:
[,1] [,2] [,3] [,4] [,5]
[1,] 5 0 3 12 6
[2,] 4 2 7 23 11
Expected value:
[,1] [,2] [,3] [,4] [,5]
[1,] 3.205479 0.7123288 3.561644 12.46575 6.054795
[2,] 5.794521 1.2876712 6.438356 22.53425 10.945205
Observed value with Yates' correction:
[,1] [,2] [,3] [,4] [,5]
[1,] 4.5 0.5 3.5 12.5 6.5
[2,] 4.5 1.5 6.5 22.5 10.5
Degree of freedom = 4
G-test:
G = 3.41127, p = 0.491497
G-test with Yates' correction:
G = 1.28744, p = 0.863503
G-test with Williams' correction:
G = 3.07349, q_min = 1.1099, p = 0.545603
> str(g.test.result)
List of 11
$ data.observed : num [1:2, 1:5] 5 4 0 2 3 7 12 23 6 11
$ data.expected : num [1:2, 1:5] 3.205 5.795 0.712 1.288 3.562 ...
$ data.observed.Yates: num [1:2, 1:5] 4.5 4.5 0.5 1.5 3.5 6.5 12.5 22.5 6.5 10.5
$ df : num 4
$ q.min : num 1.11
$ g : num 3.41
$ g.Yates : num 1.29
$ g.Williams : num 3.07
$ p : num 0.491
$ p.Yates : num 0.864
$ p.Williams : num 0.546
- attr(*, "class")= chr "g.test.2way"