library(vcd)

## View the data

structable(~gender + right + left, data=VisualAcuity)
##             right    1    2    3    4
## gender left
## male   1           821  116   72   43
##        2           112  494  151   34
##        3            85  145  583  106
##        4            35   27   87  331
## female 1          1520  234  117   36
##        2           266 1512  362   82
##        3           124  432 1772  179
##        4            66   78  205  492

## Select and process women

women <- subset(VisualAcuity, gender=="female", select=-gender)
structable(~right + left, data=women)
##       left    1    2    3    4
## right
## 1          1520  266  124   66
## 2           234 1512  432   78
## 3           117  362 1772  205
## 4            36   82  179  492
sieve(Freq ~ right + left,  data = women,
main="Vision data: Women")

## Select and process men

men <- subset(VisualAcuity, gender=="male", select=-gender)
structable(~right + left, data=men)
##       left   1   2   3   4
## right
## 1          821 112  85  35
## 2          116 494 145  27
## 3           72 151 583  87
## 4           43  34 106 331
sieve(Freq ~ right + left,  data = men,
main="Vision data: Men")

## plot both together

cotabplot(Freq ~ right + left | gender, data=VisualAcuity, panel=cotab_sieve, gp=shading_Friendly)

## Some statistical tests for association

chisq.test(xtabs(Freq ~ left + right, data=women))
##
##  Pearson's Chi-squared test
##
## data:  xtabs(Freq ~ left + right, data = women)
## X-squared = 8096.9, df = 9, p-value < 2.2e-16
chisq.test(xtabs(Freq ~ left + right, data=men))
##
##  Pearson's Chi-squared test
##
## data:  xtabs(Freq ~ left + right, data = men)
## X-squared = 3304.4, df = 9, p-value < 2.2e-16
# mutual independence of gender, right, left
MASS::loglm(Freq ~ gender + right + left, data=VisualAcuity)
## Call:
## MASS::loglm(formula = Freq ~ gender + right + left, data = VisualAcuity)
##
## Statistics:
##                        X^2 df P(> X^2)
## Likelihood Ratio  9685.509 24        0
## Pearson          11913.131 24        0