Frequency tables

We are using here the Arthritis dataset from the package vcd.
The data represent a double-blind clinical trial of new treatments for rheumatoid arthritis.

library(vcd)
head(Arthritis)   # top few observations from the data
##   ID Treatment  Sex Age Improved
## 1 57   Treated Male  27     Some
## 2 46   Treated Male  29     None
## 3 77   Treated Male  30     None
## 4 17   Treated Male  32   Marked
## 5 36   Treated Male  46   Marked
## 6 23   Treated Male  58   Marked

Three way table

xtabs() can be used to generate multidimensional tables based on three or more categoricals variables.

mytable <- xtabs(~ Treatment+Sex+Improved, data=Arthritis)
mytable
## , , Improved = None
## 
##          Sex
## Treatment Female Male
##   Placebo     19   10
##   Treated      6    7
## 
## , , Improved = Some
## 
##          Sex
## Treatment Female Male
##   Placebo      7    0
##   Treated      5    2
## 
## , , Improved = Marked
## 
##          Sex
## Treatment Female Male
##   Placebo      6    1
##   Treated     16    5

Also, we can use table() function to generate multidimensional tables.

Here we get the cell frequencies for the three-way classification. Here the ftable() function can be used to print a more compact and attractive version of the table.

Three way table

The ftable() function can be used to print multidimensional tables in a compact and attarctive manner.

ftable(mytable) 
##                  Improved None Some Marked
## Treatment Sex                             
## Placebo   Female            19    7      6
##           Male              10    0      1
## Treated   Female             6    5     16
##           Male               7    2      5

Three way table

The margin.table(), prop.table(), and addmargins() functions extend naturally to more than two dimensionals.

margin.table(mytable, 1)
## Treatment
## Placebo Treated 
##      43      41

Here we get the marginal frequencies for the Treatment. * Treatment is referred to by index 1

Three way table

margin.table(mytable, 2)
## Sex
## Female   Male 
##     59     25
margin.table(mytable, 2)
## Sex
## Female   Male 
##     59     25

Here we get the marginal frequencies for the Sex and Improved. * Sex is referred to by index 2 * Improved is referred to by index 3

Three way table

margin.table(mytable, c(1,3))
##          Improved
## Treatment None Some Marked
##   Placebo   29    7      7
##   Treated   13    7     21

Here we get the marginal frequencies for the Treatment \(\times\) Improved classification.

Three way table

Improved proportions for Treatment \(\times\) Sex

ftable(prop.table(mytable, c(1,2)))
##                  Improved       None       Some     Marked
## Treatment Sex                                             
## Placebo   Female          0.59375000 0.21875000 0.18750000
##           Male            0.90909091 0.00000000 0.09090909
## Treated   Female          0.22222222 0.18518519 0.59259259
##           Male            0.50000000 0.14285714 0.35714286

The proportion of patients with None, Some and Marked improvement for each Treatment \(\times\) Sex combination.

Three way table

ftable(addmargins(prop.table(mytable, c(1, 2)), 3))
##                  Improved       None       Some     Marked        Sum
## Treatment Sex                                                        
## Placebo   Female          0.59375000 0.21875000 0.18750000 1.00000000
##           Male            0.90909091 0.00000000 0.09090909 1.00000000
## Treated   Female          0.22222222 0.18518519 0.59259259 1.00000000
##           Male            0.50000000 0.14285714 0.35714286 1.00000000

Here we add a sum margin over the third index.

Three way table

We can get the percentages instead of proportions, simply multiply the resulting table by 100.

ftable(addmargins(prop.table(mytable, c(1, 2)), 3))*100
##                  Improved       None       Some     Marked        Sum
## Treatment Sex                                                        
## Placebo   Female           59.375000  21.875000  18.750000 100.000000
##           Male             90.909091   0.000000   9.090909 100.000000
## Treated   Female           22.222222  18.518519  59.259259 100.000000
##           Male             50.000000  14.285714  35.714286 100.000000