(Rothman, 2012)
England <- matrix(c(139, 443,230, 502), 2, 2,dimnames = list(VitalStatus = c('Dead', 'Alive'),
Smoking=c('Yes', 'No')))
England
## Smoking
## VitalStatus Yes No
## Dead 139 230
## Alive 443 502
England2 <- cbind(England, Total = rowSums(England))
rbind(England2, Total = colSums(England2))
## Yes No Total
## Dead 139 230 369
## Alive 443 502 945
## Total 582 732 1314
rtot <- apply(England, 1, sum) # row totals
England.rowdist <- sweep(England, 1, rtot, '/');
England.rowdist
## Smoking
## VitalStatus Yes No
## Dead 0.3766938 0.6233062
## Alive 0.4687831 0.5312169
ctot <- apply(England, 2, sum) # column totals
ctot
## Yes No
## 582 732
England.coldist <- sweep(England, 2, ctot, '/')
England.coldist
## Smoking
## VitalStatus Yes No
## Dead 0.2388316 0.3142077
## Alive 0.7611684 0.6857923
rowdist<-sweep(England, 1, apply(England, 1, sum), '/')
rowdist #row distribution
## Smoking
## VitalStatus Yes No
## Dead 0.3766938 0.6233062
## Alive 0.4687831 0.5312169
coldist<- sweep(England, 2, apply(England, 2, sum), '/')
coldist #column distribution
## Smoking
## VitalStatus Yes No
## Dead 0.2388316 0.3142077
## Alive 0.7611684 0.6857923
joindist <- England/sum(England)
joindist # joint distrib
## Smoking
## VitalStatus Yes No
## Dead 0.1057839 0.1750381
## Alive 0.3371385 0.3820396
pEnglands<-list(row.distribution=rowdist, column.distribution=coldist, joint.distribution=joindist)
pEnglands
## $row.distribution
## Smoking
## VitalStatus Yes No
## Dead 0.3766938 0.6233062
## Alive 0.4687831 0.5312169
##
## $column.distribution
## Smoking
## VitalStatus Yes No
## Dead 0.2388316 0.3142077
## Alive 0.7611684 0.6857923
##
## $joint.distribution
## Smoking
## VitalStatus Yes No
## Dead 0.1057839 0.1750381
## Alive 0.3371385 0.3820396
risks <- England['Dead', ]/ctot
risk.ratio <- risks/risks[2]
odds <- risks/(1 - risks)
odds.ratio <- odds/odds[2]
England
## Smoking
## VitalStatus Yes No
## Dead 139 230
## Alive 443 502
#risk ratio
#odds ratio
# display results
rbind(risks, risk.ratio, odds, odds.ratio)
## Yes No
## risks 0.2388316 0.3142077
## risk.ratio 0.7601076 1.0000000
## odds 0.3137698 0.4581673
## odds.ratio 0.6848366 1.0000000
library(mosaicData)
data("Whickham")
y<-xtabs(~outcome + smoker, data =Whickham)
y
## smoker
## outcome No Yes
## Alive 502 443
## Dead 230 139
w <- xtabs(~outcome + age + smoker, data = Whickham)
wtot <- apply(w, c(2, 3), sum)
wrisk<-sweep(w,c(2,3), wtot, "/")
wrisk2<-round(wrisk, 2)
wrisk2
## , , smoker = No
##
## age
## outcome 18 19 20 21 22 23 24 25 26 27 28 29 30
## Alive 0.91 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 0.95 1.00 0.89 1.00
## Dead 0.09 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.05 0.00 0.11 0.00
## age
## outcome 31 32 33 34 35 36 37 38 39 40 41 42 43
## Alive 1.00 0.94 0.94 1.00 0.87 0.86 1.00 0.92 1.00 1.00 1.00 0.88 0.80
## Dead 0.00 0.06 0.06 0.00 0.13 0.14 0.00 0.08 0.00 0.00 0.00 0.12 0.20
## age
## outcome 44 45 46 47 48 49 50 51 52 53 54 55 56
## Alive 1.00 0.80 0.67 0.86 1.00 0.60 0.80 0.85 0.89 1.00 0.80 0.71 0.56
## Dead 0.00 0.20 0.33 0.14 0.00 0.40 0.20 0.15 0.11 0.00 0.20 0.29 0.44
## age
## outcome 57 58 59 60 61 62 63 64 65 66 67 68 69
## Alive 0.71 0.83 0.56 0.50 0.69 0.60 0.78 0.71 0.23 0.46 0.40 0.11 0.00
## Dead 0.29 0.17 0.44 0.50 0.31 0.40 0.22 0.29 0.77 0.54 0.60 0.89 1.00
## age
## outcome 70 71 72 73 74 75 76 77 78 79 80 81 82
## Alive 0.14 0.13 0.12 0.09 0.42 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
## Dead 0.86 0.87 0.88 0.91 0.58 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00
## age
## outcome 83 84
## Alive 0.00 0.00
## Dead 1.00 1.00
##
## , , smoker = Yes
##
## age
## outcome 18 19 20 21 22 23 24 25 26 27 28 29 30
## Alive 1.00 1.00 0.88 0.88 1.00 1.00 1.00 1.00 1.00 0.88 1.00 1.00 1.00
## Dead 0.00 0.00 0.12 0.12 0.00 0.00 0.00 0.00 0.00 0.12 0.00 0.00 0.00
## age
## outcome 31 32 33 34 35 36 37 38 39 40 41 42 43
## Alive 0.93 1.00 0.94 1.00 1.00 0.94 1.00 0.90 0.90 0.60 1.00 0.82 0.82
## Dead 0.07 0.00 0.06 0.00 0.00 0.06 0.00 0.10 0.10 0.40 0.00 0.18 0.18
## age
## outcome 44 45 46 47 48 49 50 51 52 53 54 55 56
## Alive 0.91 0.64 0.80 0.86 0.82 0.80 0.87 0.73 0.77 0.67 1.00 0.67 0.50
## Dead 0.09 0.36 0.20 0.14 0.18 0.20 0.13 0.27 0.23 0.33 0.00 0.33 0.50
## age
## outcome 57 58 59 60 61 62 63 64 65 66 67 68 69
## Alive 0.58 0.73 0.44 0.27 0.46 0.75 0.56 0.70 0.00 0.17 0.67 0.00 0.00
## Dead 0.42 0.27 0.56 0.73 0.54 0.25 0.44 0.30 1.00 0.83 0.33 1.00 1.00
## age
## outcome 70 71 72 73 74 75 76 77 78 79 80 81 82
## Alive 0.00 0.12 0.00 0.00 0.67 0.00 0.00 0.00 0.00 0.00 0.00 0.00
## Dead 1.00 0.88 1.00 1.00 0.33 1.00 1.00 1.00 1.00 1.00 1.00 1.00
## age
## outcome 83 84
## Alive 0.00 0.00
## Dead 1.00 1.00