Combining Flattened Contingency Tables (and Other Tables)

This vignette demonstrates how flattened contingency tables can be combined into more complex tables. For starters, we create a few contingendy tables.

tab.Class.Age <- xtabs(Freq~Class+Age,data=Titanic)
tab.Survived.Class.Age <- xtabs(Freq~Survived+Class+Age,data=Titanic)
tab.Survived.Class.Sex <- xtabs(Freq~Survived+Class+Sex,data=Titanic)
tab.Survived.Class <- xtabs(Freq~Survived+Class,data=Titanic)
tab.Survived.Sex <- xtabs(Freq~Survived+Sex,data=Titanic)
tab.Survived.Age <- xtabs(Freq~Survived+Age,data=Titanic)
tab.Survived <- xtabs(Freq~Survived,data=Titanic)

Then we load the memisc package:

library(memisc)

Next, we create a few flattened contingency tables:

(ftab.Survived.Age <- ftable(tab.Survived.Age))
         Age Child Adult
Survived
No              52  1438
Yes             57   654
(ftab.Survived.Sex <- ftable(tab.Survived.Sex))
         Sex Male Female
Survived
No           1364    126
Yes           367    344

Since these tables have the same row variables, we can combine them columnwise:

cbind(ftab.Survived.Age,
      ftab.Survived.Sex)
         Age Child Adult  Sex Male Female
Survived
No              52  1438      1364    126
Yes             57   654       367    344

We can even add a simple table of counts:

cbind(ftab.Survived.Age,
      ftab.Survived.Sex,
      Total=tab.Survived)
         Age Child Adult  Sex Male Female   Total
Survived
No              52  1438      1364    126    1490
Yes             57   654       367    344     711

Of course, it is not enough to see such tables on screen, we also may need them in a presentable format, such as LaTeX or HTML. For the latter format we again have format_html and show_html. To see how this works, we first look at the individual ftables:

show_html(ftab.Survived.Age)
Age
SurvivedChildAdult
No521438
Yes57654
show_html(ftab.Survived.Sex)
Sex
SurvivedMaleFemale
No1364126
Yes367344

… and then at their combination

show_html(
  cbind(ftab.Survived.Age,
      ftab.Survived.Sex,
      Total=tab.Survived)
)
AgeSex
ChildAdultMaleFemaleTotal
SurvivedNo52143813641261490
Yes57654367344711

To make, in “knitr”, the creation of HTML-versions automatic one can use the following little trick:

knit_print.ftable_matrix <-function(x,options,...)
  knitr::asis_output(
    format_html(x,
                digits=if(length(options$ftable.digits))
                          options$ftable.digits
                       else 0,
                ...))

Now we do not need to call show_html while using “knitr”:

cbind(ftab.Survived.Age,
      ftab.Survived.Sex,
      Total=tab.Survived)
AgeSex
ChildAdultMaleFemaleTotal
SurvivedNo52143813641261490
Yes57654367344711

This can be undone by removing the method function of knit_print:

rm(knit_print.ftable_matrix)

For show_html and toLatex there are some variants in how the variable names are positioned, for example:

show_html(
  cbind(ftab.Survived.Age,
      ftab.Survived.Sex,
      Total=tab.Survived),
  varinfront=FALSE
)
AgeSex
SurvivedChildAdultMaleFemaleTotal
No52143813641261490
Yes57654367344711
ftab.Age.Survived <- ftable(tab.Survived.Age,col.vars=1)
ftab.Sex.Survived <- ftable(tab.Survived.Sex,col.vars=1)
ftab.Class.Survived <- ftable(tab.Survived.Class,col.vars=1)

rbind(
  ftab.Age.Survived,
  ftab.Sex.Survived,
  ftab.Class.Survived,
  Total=tab.Survived
)
       Survived   No Yes
Age
Child             52  57
Adult           1438 654

Sex
Male            1364 367
Female           126 344

Class
1st              122 203
2nd              167 118
3rd              528 178
Crew             673 212


Total           1490 711
show_html(
  rbind(
    ftab.Age.Survived,
    ftab.Sex.Survived,
    ftab.Class.Survived,
    Total=tab.Survived
  )
)
Survived
NoYes
AgeChild5257
Adult1438654
SexMale1364367
Female126344
Class1st122203
2nd167118
3rd528178
Crew673212
Total1490711

It is also possible to create the ftable`s from tables of percentages or generic tables (created with `genTable) etc.

ptab.Survived.Age<-percentages(Survived~Age,data=Titanic)
ptab.Survived.Sex<-percentages(Survived~Sex,data=Titanic)
ptab.Survived.Class<-percentages(Survived~Class,data=Titanic)

fptab.Age.Survived <- ftable(ptab.Survived.Age,col.vars=1)
fptab.Sex.Survived <- ftable(ptab.Survived.Sex,col.vars=1)
fptab.Class.Survived <- ftable(ptab.Survived.Class,col.vars=1)

show_html(
  rbind(
    fptab.Age.Survived,
    fptab.Sex.Survived,
    fptab.Class.Survived
  ),
  digits=1
)
Survived
NoYes
AgeChild47.752.3
Adult68.731.3
SexMale78.821.2
Female26.873.2
Class1st37.562.5
2nd58.641.4
3rd74.825.2
Crew76.024.0

It is also possible to combine rbind and cbind:

tab.Age <- xtabs(Freq~Age,data=Titanic)
tab.Sex <- xtabs(Freq~Sex,data=Titanic)
tab.Class <- xtabs(Freq~Class,data=Titanic)


show_html(
  rbind(
    cbind(fptab.Age.Survived,Total=tab.Age),
    cbind(fptab.Sex.Survived,Total=tab.Sex),
    cbind(fptab.Class.Survived,Total=tab.Class)
  ),
  digits=c(1,0) # One digit after dot for percentages
                # no digits for total counts.
)
Survived
NoYesTotal
AgeChild47.752.3109
Adult68.731.32092
SexMale78.821.21731
Female26.873.2470
Class1st37.562.5325
2nd58.641.4285
3rd74.825.2706
Crew76.024.0885

The same construct as LaTeX code:

toLatex(
  rbind(
    cbind(fptab.Age.Survived,Total=tab.Age),
    cbind(fptab.Sex.Survived,Total=tab.Sex),
    cbind(fptab.Class.Survived,Total=tab.Class)
  ),
  digits=c(1,0) # One digit after dot for percentages
                # no digits for total counts.
)
\begin{tabular}{llD{.}{.}{1}D{.}{.}{1}cD{.}{.}{0}}
\toprule
&&\multicolumn{2}{c}{Survived}&&\\
\cmidrule{3-4}
&&\multicolumn{1}{c}{No}&\multicolumn{1}{c}{Yes}&&\multicolumn{1}{c}{Total}\\
\midrule
Age&Child&47.7&52.3&&109\\
&Adult&68.7&31.3&&2092\\
\midrule Sex&Male&78.8&21.2&&1731\\
&Female&26.8&73.2&&470\\
\midrule Class&1st&37.5&62.5&&325\\
&2nd&58.6&41.4&&285\\
&3rd&74.8&25.2&&706\\
&Crew&76.0&24.0&&885\\
\bottomrule
\end{tabular}

and typeset with LaTeX

../../../_images/ftable-matrix-xmpl.png