Categorical Data Analysis & graphs

Topics

Introduction

Introduction

  • vcd package (Visualizing categorical data)
  • Visit datavis.ca for more info
#data() shows the list of all datasets currently available
## Load vcd package
library(vcd)

## Load Arthritis dataset (data frame)
data(Arthritis)

Introduction

  • Proportions for a single variable table
tab1=table(Arthritis$Improved)
prop.table(tab1) #table showing proportion

     None      Some    Marked 
0.5000000 0.1666667 0.3333333 

Cross table by two variables

xtab1 = xtabs(~ Treatment +Improved, Arthritis)
xtab1
         Improved
Treatment None Some Marked
  Placebo   29    7      7
  Treated   13    7     21

Add margins (sums)

addmargins(xtab1)
         Improved
Treatment None Some Marked Sum
  Placebo   29    7      7  43
  Treated   13    7     21  41
  Sum       42   14     28  84

Proportions in cross table

  • (margin 1: row proportion; 2: column proportion)
# prop.to total
prop.table(xtab1)    
# prop. to row sum
prop.table(xtab1, margin = 1)  
# prop. to column sum
prop.table(xtab1, margin = 2) 

Stratified table

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

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

Basic Mosaic plot

  • mosaicplot is a basic function from graphics package
mosaicplot(xtab1,main = "Arthritis treatment", color = c("#7fc97f","#beaed4","#fdc086"), las = 1)

plot of chunk unnamed-chunk-8

mosaicplot(HairEyeColor)

plot of chunk unnamed-chunk-8

Extended Mosaic function

  • mosaic is an extended function from vcd package
mosaic(xtab1, shade=TRUE, legend=TRUE)
mosaic(HairEyeColor, shade=TRUE,legend=TRUE)

Graphs

Graphs