Debian Codegraph @

inst/doc/ftable-matrix.Rmd

raw
1 ---
2 title: Combining flattened contingency tables (and other tables)
3 output: rmarkdown::html_vignette
4 vignette: >
5 % \VignetteIndexEntry{Combining flattened contingency tables (and other tables)}
6 % \VignetteEngine{knitr::rmarkdown}
7 % \VignetteEncoding{UTF-8}
8 ---
9
10 ```{r,echo=FALSE,message=FALSE}
11 knitr::opts_chunk$set(comment=NA,
12 fig.align="center",
13 results="markup")
14 ```
15
16 This vignette demonstrates how flattened contingency tables can be combined into more
17 complex tables. For starters, we create a few contingendy tables.
18
19 ```{r}
20 tab.Class.Age <- xtabs(Freq~Class+Age,data=Titanic)
21 tab.Survived.Class.Age <- xtabs(Freq~Survived+Class+Age,data=Titanic)
22 tab.Survived.Class.Sex <- xtabs(Freq~Survived+Class+Sex,data=Titanic)
23 tab.Survived.Class <- xtabs(Freq~Survived+Class,data=Titanic)
24 tab.Survived.Sex <- xtabs(Freq~Survived+Sex,data=Titanic)
25 tab.Survived.Age <- xtabs(Freq~Survived+Age,data=Titanic)
26 tab.Survived <- xtabs(Freq~Survived,data=Titanic)
27 ```
28 Then we load the *memisc* package:
29 ```{r,message=FALSE}
30 library(memisc)
31 ```
32
33 Next, we create a few flattened contingency tables:
34 ```{r}
35 (ftab.Survived.Age <- ftable(tab.Survived.Age))
36 (ftab.Survived.Sex <- ftable(tab.Survived.Sex))
37 ```
38 Since these tables have the same row variables, we can combine them columnwise:
39 ```{r}
40 cbind(ftab.Survived.Age,
41 ftab.Survived.Sex)
42 ```
43 We can even add a simple table of counts:
44 ```{r}
45 cbind(ftab.Survived.Age,
46 ftab.Survived.Sex,
47 Total=tab.Survived)
48 ```
49 Of course, it is not enough to see such tables on screen, we also may need them in a presentable
50 format, such as LaTeX or HTML. For the latter format we again have `format_html` and `show_html`.
51 To see how this works, we first look at the individual `ftable`s:
52 ```{r,results='asis'}
53 show_html(ftab.Survived.Age)
54 ```
55 ```{r,results='asis'}
56 show_html(ftab.Survived.Sex)
57 ```
58 ... and then at their combination
59 ```{r,results='asis'}
60 show_html(
61 cbind(ftab.Survived.Age,
62 ftab.Survived.Sex,
63 Total=tab.Survived)
64 )
65 ```
66 To make, in "knitr", the creation of HTML-versions automatic one can use the following little trick:
67 ```{r}
68 knit_print.ftable_matrix <-function(x,options,...)
69 knitr::asis_output(
70 format_html(x,
71 digits=if(length(options$ftable.digits))
72 options$ftable.digits
73 else 0,
74 ...))
75 ```
76 Now we do not need to call `show_html` while using "knitr":
77 ```{r}
78 cbind(ftab.Survived.Age,
79 ftab.Survived.Sex,
80 Total=tab.Survived)
81 ```
82 This can be undone by removing the method function of `knit_print`:
83 ```{r}
84 rm(knit_print.ftable_matrix)
85 ```
86 For `show_html` and `toLatex` there are some variants in how the
87 variable names are positioned, for example:
88 ```{r,results='asis'}
89 show_html(
90 cbind(ftab.Survived.Age,
91 ftab.Survived.Sex,
92 Total=tab.Survived),
93 varinfront=FALSE
94 )
95 ```
96
97 ```{r,results='asis'}
98 show_html(
99 cbind(ftab.Survived.Age,
100 ftab.Survived.Sex,
101 Total=tab.Survived),
102 varontop=FALSE
103 )
104 ```
105 Of course it is also possible to combine flat contingency tables rowwise:
106 ```{r}
107 ftab.Age.Survived <- ftable(tab.Survived.Age,col.vars=1)
108 ftab.Sex.Survived <- ftable(tab.Survived.Sex,col.vars=1)
109 ftab.Class.Survived <- ftable(tab.Survived.Class,col.vars=1)
110
111 rbind(
112 ftab.Age.Survived,
113 ftab.Sex.Survived,
114 ftab.Class.Survived,
115 Total=tab.Survived
116 )
117 ```
118 ```{r,results='asis'}
119 show_html(
120 rbind(
121 ftab.Age.Survived,
122 ftab.Sex.Survived,
123 ftab.Class.Survived,
124 Total=tab.Survived
125 )
126 )
127 ```
128 It is also possible to create the `ftable`s from tables of percentages or generic tables (created
129 with `genTable`) etc.
130 ```{r,results='asis'}
131 ptab.Survived.Age<-percentages(Survived~Age,data=Titanic)
132 ptab.Survived.Sex<-percentages(Survived~Sex,data=Titanic)
133 ptab.Survived.Class<-percentages(Survived~Class,data=Titanic)
134
135 fptab.Age.Survived <- ftable(ptab.Survived.Age,col.vars=1)
136 fptab.Sex.Survived <- ftable(ptab.Survived.Sex,col.vars=1)
137 fptab.Class.Survived <- ftable(ptab.Survived.Class,col.vars=1)
138
139 show_html(
140 rbind(
141 fptab.Age.Survived,
142 fptab.Sex.Survived,
143 fptab.Class.Survived
144 ),
145 digits=1
146 )
147 ```
148
149 It is also possible to combine `rbind` and `cbind`:
150 ```{r,results='asis'}
151 tab.Age <- xtabs(Freq~Age,data=Titanic)
152 tab.Sex <- xtabs(Freq~Sex,data=Titanic)
153 tab.Class <- xtabs(Freq~Class,data=Titanic)
154
155
156 show_html(
157 rbind(
158 cbind(fptab.Age.Survived,Total=tab.Age),
159 cbind(fptab.Sex.Survived,Total=tab.Sex),
160 cbind(fptab.Class.Survived,Total=tab.Class)
161 ),
162 digits=c(1,0) # One digit after dot for percentages
163 # no digits for total counts.
164 )
165 ```
166 The same construct as LaTeX code:
167 ```{r}
168 toLatex(
169 rbind(
170 cbind(fptab.Age.Survived,Total=tab.Age),
171 cbind(fptab.Sex.Survived,Total=tab.Sex),
172 cbind(fptab.Class.Survived,Total=tab.Class)
173 ),
174 digits=c(1,0) # One digit after dot for percentages
175 # no digits for total counts.
176 )
177 ```
178
179