Load packages & data
library(vcdExtra)
library(gnm) # for Diag(), Symm()
data(VisualAcuity, package="vcd")
Work with the data for females
women <- subset(VisualAcuity, gender=="female", select=-gender)
Fit the independence model
indep <- glm(Freq ~ right + left, data = women, family=poisson)
mosaic(indep, residuals_type="rstandard", gp=shading_Friendly,
main="Vision data: Independence (women)" )
Quasi-independence: ignore the diagonal cells by fitting them
exactly
quasi.indep <- glm(Freq ~ right + left + Diag(right, left),
data = women, family = poisson)
mosaic(quasi.indep, residuals_type="rstandard", gp=shading_Friendly,
main="Quasi-Independence (women)" )
Symmetry: test F[i,j] = F[j,i].
Note that this model does not include the ‘main’ effects of right and
left, so assumes marginal homogeneity
symmetry <- glm(Freq ~ Symm(right, left),
data = women, family = poisson)
mosaic(symmetry, residuals_type="rstandard", gp=shading_Friendly,
main="Symmetry model (women)" )
Quasi-symmetry: allow different marginal frequencies for left and
right
quasi.symm <- glm(Freq ~ right + left + Symm(right, left),
data = women, family = poisson)
mosaic(quasi.symm, residuals_type="rstandard", gp=shading_Friendly,
main="Quasi-Symmetry model (women)")
Model comparisons: for nested models
anova(indep, quasi.indep, quasi.symm, test="Chisq")
## Analysis of Deviance Table
##
## Model 1: Freq ~ right + left
## Model 2: Freq ~ right + left + Diag(right, left)
## Model 3: Freq ~ right + left + Symm(right, left)
## Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1 9 6672
## 2 5 199 4 6472 <2e-16 ***
## 3 3 7 2 192 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(symmetry, quasi.symm, test="Chisq")
## Analysis of Deviance Table
##
## Model 1: Freq ~ Symm(right, left)
## Model 2: Freq ~ right + left + Symm(right, left)
## Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1 6 19.25
## 2 3 7.27 3 12 0.0075 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
model summaries, with AIC and BIC
models <- glmlist(indep, quasi.indep, symmetry, quasi.symm)
LRstats(models)
## Likelihood summary table:
## AIC BIC LR Chisq Df Pr(>Chisq)
## indep 6803 6808 6672 9 <2e-16 ***
## quasi.indep 338 347 199 5 <2e-16 ***
## symmetry 157 164 19 6 0.0038 **
## quasi.symm 151 161 7 3 0.0638 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
IycgLS0tDQojJyB0aXRsZTogIlZpc3VhbEFjdWl0eTogUXVhc2ktaW5kZXBlbmRlbmNlIGFuZCBxdWFzaS1zeW1tZXRyeSINCiMnIGF1dGhvcjogIk1pY2hhZWwgRnJpZW5kbHkiDQojJyBvdXRwdXQ6DQojJyAgIGh0bWxfZG9jdW1lbnQ6DQojJyAgICAgdGhlbWU6IHJlYWRhYmxlDQojJyAgICAgY29kZV9kb3dubG9hZDogdHJ1ZQ0KIycgLS0tDQoNCiMrIGVjaG89RkFMU0UNCmtuaXRyOjpvcHRzX2NodW5rJHNldCgNCiAgd2FybmluZyA9IEZBTFNFLCAgICMgYXZvaWQgd2FybmluZ3MgYW5kIG1lc3NhZ2VzIGluIHRoZSBvdXRwdXQNCiAgbWVzc2FnZSA9IEZBTFNFDQopDQoNCiMnICMjIExvYWQgcGFja2FnZXMgJiBkYXRhDQpsaWJyYXJ5KHZjZEV4dHJhKQ0KbGlicmFyeShnbm0pICAgICAgIyBmb3IgRGlhZygpLCBTeW1tKCkNCg0KZGF0YShWaXN1YWxBY3VpdHksIHBhY2thZ2U9InZjZCIpDQoNCiMnICMjIFdvcmsgd2l0aCB0aGUgZGF0YSBmb3IgZmVtYWxlcw0Kd29tZW4gPC0gc3Vic2V0KFZpc3VhbEFjdWl0eSwgZ2VuZGVyPT0iZmVtYWxlIiwgc2VsZWN0PS1nZW5kZXIpDQoNCiMnICMjIEZpdCB0aGUgaW5kZXBlbmRlbmNlIG1vZGVsDQppbmRlcCA8LSBnbG0oRnJlcSB+IHJpZ2h0ICsgbGVmdCw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