Exercise 4.2
library(vcd)
## Warning: package 'vcd' was built under R version 3.5.3
## Loading required package: grid
data("Abortion", package = "vcdExtra")
str(Abortion)
## 'table' num [1:2, 1:2, 1:2] 171 152 138 167 79 148 112 133
## - attr(*, "dimnames")=List of 3
## ..$ Sex : chr [1:2] "Female" "Male"
## ..$ Status : chr [1:2] "Lo" "Hi"
## ..$ Support_Abortion: chr [1:2] "Yes" "No"
structure(Abortion)
## , , Support_Abortion = Yes
##
## Status
## Sex Lo Hi
## Female 171 138
## Male 152 167
##
## , , Support_Abortion = No
##
## Status
## Sex Lo Hi
## Female 79 112
## Male 148 133
fourfold(Abortion)
b.
fourfold(aperm(Abortion))
c.
oddsratio(Abortion,log = F)
## odds ratios for Sex and Status by Support_Abortion
##
## Yes No
## 1.3614130 0.6338682
oddsratio(aperm(Abortion),log = F)
## odds ratios for Support_Abortion and Status by Sex
##
## Female Male
## 1.7567419 0.8179317
Exersice 4.4 a.
library(vcd)
library(grid)
data("Hospital", package = "vcd")
str(Hospital)
## 'table' num [1:3, 1:3] 43 6 9 16 11 18 3 10 16
## - attr(*, "dimnames")=List of 2
## ..$ Visit frequency: chr [1:3] "Regular" "Less than monthly" "Never"
## ..$ Length of stay : chr [1:3] "2-9" "10-19" "20+"
structure(Hospital)
## Length of stay
## Visit frequency 2-9 10-19 20+
## Regular 43 16 3
## Less than monthly 6 11 10
## Never 9 18 16
HS=margin.table(Hospital,1:2)
HS
## Length of stay
## Visit frequency 2-9 10-19 20+
## Regular 43 16 3
## Less than monthly 6 11 10
## Never 9 18 16
chisq.test(HS)
##
## Pearson's Chi-squared test
##
## data: HS
## X-squared = 35.171, df = 4, p-value = 4.284e-07
assocstats(HS)
## X^2 df P(> X^2)
## Likelihood Ratio 38.353 4 9.4755e-08
## Pearson 35.171 4 4.2842e-07
##
## Phi-Coefficient : NA
## Contingency Coeff.: 0.459
## Cramer's V : 0.365
assoc(HS,shade=TRUE)
Answer: In the association plot, Cell of regular visit-stay 2-9 days, and cell of never visit -stay 20+ days, the observed > expected frequency is colored as blue; cell of regular visit-stay 20+ days, and cell of never visit - stay 2-9 days that contain less than the expected frequency fall below it (and are shaded red). It indicates that strong association between stay period and visit frequency.
library(vcdExtra)
## Warning: package 'vcdExtra' was built under R version 3.5.3
## Loading required package: gnm
## Warning: package 'gnm' was built under R version 3.5.3
CMHtest(HS)
## Cochran-Mantel-Haenszel Statistics for Visit frequency by Length of stay
##
## AltHypothesis Chisq Df Prob
## cor Nonzero correlation 29.138 1 6.7393e-08
## rmeans Row mean scores differ 34.391 2 3.4044e-08
## cmeans Col mean scores differ 29.607 2 3.7233e-07
## general General association 34.905 4 4.8596e-07
Answer: The p-value is still less than 0.05, and both variables can be considered ordinal, the association between two variables still stay true.
Exercise 4.6 a.
library(vcdExtra)
data("Mammograms")
structure(Mammograms)
## Reader1
## Reader2 Absent Minimal Moderate Severe
## Absent 34 10 2 0
## Minimal 6 8 8 2
## Moderate 2 5 4 12
## Severe 0 1 2 14
Kappa(Mammograms)
## value ASE z Pr(>|z|)
## Unweighted 0.3713 0.06033 6.154 7.560e-10
## Weighted 0.5964 0.04923 12.114 8.901e-34
agreementplot(Mammograms, main = "agreement")
assocstats(Mammograms)
## X^2 df P(> X^2)
## Likelihood Ratio 92.619 9 4.4409e-16
## Pearson 83.516 9 3.2307e-14
##
## Phi-Coefficient : NA
## Contingency Coeff.: 0.657
## Cramer's V : 0.503
Answer: The unweighted Kappa value shows a minimal agreement between rater1 and rater2, and the weighted Kappa value suggested stronger.The Likelihood ratio and Pearson Chi Square test also suggested the two raters has different opinions on rating.
Exercise 4.7 a.
data <- matrix(c(24,8,13,8,13,11,10,9,64), nrow=3, ncol = 3, byrow = TRUE)
rownames(data) <- c("Con", "Mixed", "Pro")
colnames(data) <- c("Con", "Mixed", "Pro")
data
## Con Mixed Pro
## Con 24 8 13
## Mixed 8 13 11
## Pro 10 9 64
Kappa(data)
## value ASE z Pr(>|z|)
## Unweighted 0.3888 0.05979 6.503 7.870e-11
## Weighted 0.4269 0.06350 6.723 1.781e-11
agreementplot(data, main = "aggreement")