library(MASS)
library(vcd)
## Loading required package: grid
library(vcdExtra)
## Loading required package: gnm
data(DaytonSurvey)
Dayton.ACM <- aggregate(Freq ~ cigarette + alcohol + marijuana, data = DaytonSurvey, FUN = sum)
attach(DaytonSurvey)
str(DaytonSurvey)
## 'data.frame': 32 obs. of 6 variables:
## $ cigarette: Factor w/ 2 levels "Yes","No": 1 2 1 2 1 2 1 2 1 2 ...
## $ alcohol : Factor w/ 2 levels "Yes","No": 1 1 2 2 1 1 2 2 1 1 ...
## $ marijuana: Factor w/ 2 levels "Yes","No": 1 1 1 1 2 2 2 2 1 1 ...
## $ sex : Factor w/ 2 levels "female","male": 1 1 1 1 1 1 1 1 2 2 ...
## $ race : Factor w/ 2 levels "white","other": 1 1 1 1 1 1 1 1 1 1 ...
## $ Freq : num 405 13 1 1 268 218 17 117 453 28 ...
Daytona.ACM =aggregate(Freq~cigarette +alcohol +marijuana,data= DaytonSurvey, FUN=sum)
ACM.ind =loglm(Freq ~., data=Daytona.ACM)
ACM.ind
## Call:
## loglm(formula = Freq ~ ., data = Daytona.ACM)
##
## Statistics:
## X^2 df P(> X^2)
## Likelihood Ratio 1286.020 4 0
## Pearson 1411.386 4 0
Daytona.tab = xtabs(Freq ~ ., data = Daytona.ACM)
pairs(Daytona.tab, type = "pairwise", shade = TRUE)
The expected values (and residuals) are taken from the mutual independence model. The graphs ilustrates that the variables are associated mutually. In particular, in the mosaic for cigarette and alcohol, two many people who drink also smoke (and, conversely, two many people who don’t drink also don’t smoke) than would be expected under the mutual independence model.
fourfold(aperm(Daytona.tab, c(1, 2, 3)))
fourfold(aperm(Daytona.tab, c(2, 3, 1)))
fourfold(aperm(Daytona.tab, c(3, 1, 2)))
Above graphs display a positive association between two of the three drugs (more people who drink also smoke than expected under independence) which illustrates that the hypothesis of mutual independence is not tenable. However, these mutual association patterns are independent from the third, so a reasonable model might be the homogeneous association model (= no three-way association).