Q1.

##install.packages("logmult")
##library(logmult)
data("criminal", package = "logmult")
criminal
##       Age
## Year    15  16  17  18  19
##   1955 141 285 320 441 427
##   1956 144 292 342 441 396
##   1957 196 380 424 462 427
##   1958 212 424 399 442 430
  1. Use loglm() to test whether there is an association between Year and Age. Is there evidence that dropping of charges in relation to age changed over the years recorded here?
library(MASS)
loglm(~Year + Age, data = criminal)
## Call:
## loglm(formula = ~Year + Age, data = criminal)
## 
## Statistics:
##                       X^2 df     P(> X^2)
## Likelihood Ratio 38.24466 12 0.0001400372
## Pearson          38.41033 12 0.0001315495
  1. Use mosaic() with the option shade=TRUE to display the pattern of signs and magnitudes of the residuals. Compare this with the result of mosaic() using “Friendly shading,” from the option gp=shading_Friendly. Describe verbally what you see in each regarding the pattern of association in this table.
library(vcd)
## Loading required package: grid
mosaic(criminal, shade = TRUE)

mosaic(criminal, gp = shading_Friendly)

In both charts, squares with highest residuals are shaded in blue, although you see a level of detail further in gradation int the friendly chart.

Q2.

library(vcdExtra)
## Loading required package: gnm
data("Accident", package = "vcdExtra")
str(Accident, vec.len = 2)
## 'data.frame':    80 obs. of  5 variables:
##  $ age   : Ord.factor w/ 5 levels "0-9"<"10-19"<..: 5 5 5 5 5 ...
##  $ result: Factor w/ 2 levels "Died","Injured": 1 1 1 1 1 ...
##  $ mode  : Factor w/ 4 levels "4-Wheeled","Bicycle",..: 4 4 2 2 3 ...
##  $ gender: Factor w/ 2 levels "Female","Male": 2 1 2 1 2 ...
##  $ Freq  : int  704 378 396 56 742 ...
  1. Use loglm() to fit the model of mutual independence, Freq ~ age+mode+gender+result to this data set.
loglm(Freq~ age+mode+gender+result, data=Accident)
## Call:
## loglm(formula = Freq ~ age + mode + gender + result, data = Accident)
## 
## Statistics:
##                       X^2 df P(> X^2)
## Likelihood Ratio 60320.05 70        0
## Pearson          76865.31 70        0
  1. Use mosaic() to produce an interpretable mosaic plot of the associations among all variables under the model of mutual independence. Try different orders of the variables in the mosaic. (Hint: the abbreviate component of the labeling_args argument to mosaic() will be useful to avoid some overlap of the category labels.)
mosaic(Freq ~ age + mode + gender + result, data = Accident, shade = TRUE, labeling_args = list(abbreviate = c(gender = TRUE)))

c. Treat result (“Died” vs. “Injured”) as the response variable, and fit the model Freq ~ agemodegender + result that asserts independence of result from all others jointly.

loglm(Freq ~ (age*mode*gender) + result, data = Accident)
## Call:
## loglm(formula = Freq ~ (age * mode * gender) + result, data = Accident)
## 
## Statistics:
##                      X^2 df P(> X^2)
## Likelihood Ratio 2217.72 39        0
## Pearson          2347.60 39        0
  1. Construct a mosaic display for the residual associations in this model. Which combinations of the predictor factors are more likely to result in death?
mosaic(loglm(Freq ~ (gender*age*mode) + result, data = Accident))

Males over 50 years of age are more likely to die in traffic accidents.