#data() shows the list of all datasets currently available
## Load vcd package
library(vcd)
## Load Arthritis dataset (data frame)
data(Arthritis)
tab1=table(Arthritis$Improved)
prop.table(tab1) #table showing proportion
None Some Marked
0.5000000 0.1666667 0.3333333
xtab1 = xtabs(~ Treatment +Improved, Arthritis)
xtab1
Improved
Treatment None Some Marked
Placebo 29 7 7
Treated 13 7 21
addmargins(xtab1)
Improved
Treatment None Some Marked Sum
Placebo 29 7 7 43
Treated 13 7 21 41
Sum 42 14 28 84
# prop.to total
prop.table(xtab1)
# prop. to row sum
prop.table(xtab1, margin = 1)
# prop. to column sum
prop.table(xtab1, margin = 2)
## 3rd variable as stratified variable
xtab2 <- xtabs(~ Treatment +Improved +Sex, Arthritis)
xtab2
, , Sex = Female
Improved
Treatment None Some Marked
Placebo 19 7 6
Treated 6 5 16
, , Sex = Male
Improved
Treatment None Some Marked
Placebo 10 0 1
Treated 7 2 5
## flat table
ftable(xtab2)
Sex Female Male
Treatment Improved
Placebo None 19 10
Some 7 0
Marked 6 1
Treated None 6 7
Some 5 2
Marked 16 5
mosaicplot(xtab1,main = "Arthritis treatment", color = c("#7fc97f","#beaed4","#fdc086"), las = 1)
mosaicplot(HairEyeColor)
mosaic(xtab1, shade=TRUE, legend=TRUE)
mosaic(HairEyeColor, shade=TRUE,legend=TRUE)