library(logmult)
## Warning: package 'logmult' was built under R version 3.4.4
## Loading required package: gnm
## Warning: package 'gnm' was built under R version 3.4.4
##
## Attaching package: 'logmult'
## The following object is masked from 'package:gnm':
##
## se
library(vcd)
## Warning: package 'vcd' was built under R version 3.4.4
## Loading required package: grid
##
## Attaching package: 'vcd'
## The following object is masked from 'package:logmult':
##
## assoc
library(MASS)
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
loglm(Freq ~ Age + Year, data = criminal)
## Call:
## loglm(formula = Freq ~ Age + Year, data = criminal)
##
## Statistics:
## X^2 df P(> X^2)
## Likelihood Ratio 38.24466 12 0.0001400372
## Pearson 38.41033 12 0.0001315495
Based on the p value being very small, we can assume that the association is significant. However, the chi square value is also very small, so there is not enough information to conclude a significant relation.
mosaic(criminal,shade = TRUE, labeling = labeling_residuals, supress = 0)
This first mosaic plot shows the associations between the year and the age. We can observe that the most related ones are age 19 in year 1955 and age 16 in year 1958.
mosaic(criminal, gp = shading_Friendly,labeling = labeling_residuals,suppress=0)
This second mosaic plot shows the strength of association, but it also distinguishes positive association (in blue) and negative association (in red). Both Mosaic Plot showed that there was association between Age 19 and Year 1955, as well as Age 15 and Year 1958. For others, there is little evidence of association.
data("Accident", package = "vcdExtra")
str(Accident)
## 'data.frame': 80 obs. of 5 variables:
## $ age : Ord.factor w/ 5 levels "0-9"<"10-19"<..: 5 5 5 5 5 5 5 5 5 5 ...
## $ result: Factor w/ 2 levels "Died","Injured": 1 1 1 1 1 1 1 1 2 2 ...
## $ mode : Factor w/ 4 levels "4-Wheeled","Bicycle",..: 4 4 2 2 3 3 1 1 4 4 ...
## $ gender: Factor w/ 2 levels "Female","Male": 2 1 2 1 2 1 2 1 2 1 ...
## $ Freq : int 704 378 396 56 742 78 513 253 5206 5449 ...
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
mosaic(Freq ~ age + mode + gender + result, data = Accident, shade = TRUE, labeling_args = list(clip = c(result = TRUE)))
mosaic (Freq ~ gender + mode + result + age , data = Accident, shade = TRUE, labeling_args = list(clip = c(result = TRUE)))
mosaic (Freq ~ result + gender + age + mode, data = Accident, shade = TRUE, labeling_args = list(clip = c(result = TRUE)))
mode_new = loglm(Freq ~ age * mode * gender + result, data = Accident)
mode_new
## 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
mosaic(mode_new, shade = TRUE, labeling = labeling_residuals, rot_labels = c(20, 20, 30, 90))