install.packages("logmult", repos = "http://cran.us.r-project.org")
## 
## The downloaded binary packages are in
##  /var/folders/xj/tl6pm98x5qb3b_94_pxtg29h0000gn/T//RtmpNqHmMZ/downloaded_packages
library(logmult)
## Loading required package: gnm
## 
## Attaching package: 'logmult'
## The following object is masked from 'package:gnm':
## 
##     se
library(MASS)
library(vcd)
## Loading required package: grid
## 
## Attaching package: 'vcd'
## The following object is masked from 'package:logmult':
## 
##     assoc
library(vcdExtra)

Exercise 5.1 (a)

data("criminal", pacakge="logmult")
## Warning in data("criminal", pacakge = "logmult"): data set 'logmult' not
## found
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
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

Based on statistic of this regression, we could make the conclusion that there is not evidence that dropping of charges in relation to age changed over the years recorded here.

Exercise 5.1 (b)

mosaic(criminal, shade = TRUE, labeling = labeling_residuals, supress = 0)

mosaic(criminal, gp = shading_Friendly,labeling = labeling_residuals,suppress=0)

Verbally, the pattern of association in this table becomes significant at 1955 for age 19, and at 1958 for age 16.

Exercise 5.9

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 ...

5.9 (a)

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

5.9 (b)

mosaic(Freq ~ age+result+gender+mode, data=Accident, shade=TRUE, labeling_args=list(rot_labels = c(bottom=35,right=10,top=35), offset_varnames = c(right=1), offset_labels=c(right =0.2), margins=c(right=3, bottom=3)))

Accident$mode <- ordered(Accident$mode, levels=levels(Accident$mode)[c(4,1,3,2)])
mosaic(Freq ~ age+result+gender+mode, data=Accident, shade=TRUE, labeling_args=list(rot_labels = c(bottom=35,right=10,top=35), offset_varnames = c(right=1), offset_labels=c(right =0.2), margins=c(right=3, bottom=3)))

Accident$mode <- ordered(Accident$mode, levels=levels(Accident$mode)[c(1,2,4,3)])
mosaic(Freq ~ age+result+gender+mode, data=Accident, shade=TRUE, labeling_args=list(rot_labels = c(bottom=35,right=10,top=35), offset_varnames = c(right=1), offset_labels=c(right =0.2), margins=c(right=3, bottom=3)))

5.9 (c)

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
rel <- loglm(Freq ~ age*mode*gender+result, data=Accident)
mosaic(rel, shade=TRUE, labeling=labeling_residuals,rot_labels=c(35, 45, 20, 50))

From the above table, combinations of the male in the age of 20-29 and motorcycle are more likely to result in death.