File: freqlist.Rmd

package info (click to toggle)
r-cran-arsenal 3.6.3-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,788 kB
  • sloc: sh: 18; makefile: 5
file content (274 lines) | stat: -rw-r--r-- 9,722 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
---
title: "The freqlist function"
author: "Tina Gunderson and Ethan Heinzen"
output:
  rmarkdown::html_vignette:
    toc: yes
    toc_depth: 3
vignette: |
  %\VignetteIndexEntry{The freqlist function}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, tidy.opts=list(width.cutoff=80), tidy=TRUE, comment=NA)
options(width=80, max.print=1000)
```

# Overview 

`freqlist()` is a function meant to produce output similar to SAS's `PROC FREQ` procedure when using the `/list` option of the `TABLE` statement.
`freqlist()` provides options for handling missing or sparse data and can provide cumulative counts and percentages based on subgroups.
It depends on the `knitr` package for printing. 

```{r message = FALSE}
require(arsenal)
```

## Sample dataset

For our examples, we'll load the `mockstudy` data included with this package and use it to create a basic table.
Because they have fewer levels, for brevity, we'll use the variables arm, sex, and mdquality.s to create the example table.
We'll retain NAs in the table creation.
See the appendix for notes regarding default NA handling and other useful information regarding tables in R. 

```{r loading.data}
# load the data
data(mockstudy)

# retain NAs when creating the table using the useNA argument
tab.ex <- table(mockstudy[, c("arm", "sex", "mdquality.s")], useNA="ifany")
```
 
# The `freqlist` object

The `freqlist()` function is an S3 generic (with methods for tables and formulas) which returns an object of class `"freqlist"`.

```{r console.output}
example1 <- freqlist(tab.ex)

str(example1)

# view the data frame portion of freqlist output
head(as.data.frame(example1)) ## or use as.data.frame(example1)
```

# Basic output using `summary()` 

The `summary` method for `freqlist()` relies on the `kable()` function (in the `knitr` package) for printing.
`knitr::kable()` converts the output to markdown which can be printed in the console or easily rendered in
Word, PDF, or HTML documents.

Note that you must supply `results="asis"` to properly format the markdown output.

```{r, results = 'asis'}
summary(example1)
```

You can print a title for the table using the `title=` argument.

```{r, results = 'asis'}
summary(example1, title="Basic freqlist output")
```

You can also easily pull out the `freqlist` data frame for more complicated formatting or manipulation
(e.g. with another function such as `xtable()` or `pander()`) using `as.data.frame(summary())`:

```{r}
head(as.data.frame(summary(example1)))
```

# Using a formula with `freqlist`

Instead of passing a pre-computed table to `freqlist()`, you can instead pass a formula, which will be
in turn passed to the `xtabs()` function. Additional `freqlist()` arguments are passed through the `...`
to the `freqlist()` table method.

Note that `freqlist()` sets the `addNA=TRUE` argument by default:

```{r results='asis'}
summary(freqlist(~ arm + sex + mdquality.s, data = mockstudy))
```

One can also set NAs to an explicit value using `includeNA()`.

```{r results='asis'}
summary(freqlist(~ arm + sex + includeNA(mdquality.s, "Missing"), data = mockstudy))
```

In fact, since `xtabs()` allows for left-hand-side weights, so does `freqlist()`!

```{r results='asis'}
mockstudy$weights <- c(10000, rep(1, nrow(mockstudy) - 1))
summary(freqlist(weights ~ arm + sex + addNA(mdquality.s), data = mockstudy))
```

You can also specify multiple weights:

```{r results='asis'}
mockstudy$weights2 <- c(rep(1, nrow(mockstudy) - 1), 10000)
summary(freqlist(list(weights, weights2) ~ arm + sex + addNA(mdquality.s), data = mockstudy))
```

# Rounding percentage digits or changing variable names for printing

The `digits.pct=` argument takes a single numeric value and controls the number of digits of percentages in the output.
The `digits.count=` argument takes a similar argument and controls the number of digits of the count columns.
The `labelTranslations=` argument is a named character vector or list.
Both options are applied in the following example. 

```{r labelTranslations, results = 'asis'}
example2 <- freqlist(tab.ex, labelTranslations = c(arm = "Treatment Arm", sex = "Gender", mdquality.s = "LASA QOL"),
                      digits.pct = 1, digits.count = 1)
summary(example2)
```

# Additional examples

## Including combinations with frequencies of zero

The `sparse=` argument takes a single logical value as input. The default option is `FALSE`.
If set to `TRUE`, the sparse option will include combinations with frequencies of zero in the list of results.
As our initial table did not have any such levels, we create a second table to use in our example.

```{r sparse, results = 'asis'}
summary(freqlist(~ race + sex + arm, data = mockstudy, sparse = TRUE, digits.pct=1))
```

## Options for NA handling

The various `na.options=` allow you to include or exclude data with missing values for one or more factor
levels in the counts and percentages, as well as show the missing data but exclude it from the cumulative
counts and percentages. The default option is to include all combinations with missing values. 

```{r na.options, results = 'asis'}
summary(freqlist(tab.ex, na.options="include"))
summary(freqlist(tab.ex, na.options="showexclude"))
summary(freqlist(tab.ex, na.options="remove"))
```

## Frequency counts and percentages subset by factor levels 

The `strata=` argument internally subsets the data by the specified factor prior to calculating cumulative counts and percentages.
By default, when used each subset will print in a separate table. Using the `single = TRUE` option when printing will collapse
the subsetted result into a single table.

```{r freq.counts, results='asis'}
example3 <- freqlist(tab.ex, strata = c("arm","sex"))
summary(example3)

#using the single = TRUE argument will collapse results into a single table for printing
summary(example3, single = TRUE)

```

## Show only the "n" most common combinations in each table (`head()` and `sort()`)

You can now sort `freqlist()` objects, and, by taking the `head()` of the summary, output the most common frequencies. This looks
the prettiest with `dupLabels=TRUE`.

```{r}
head(summary(sort(example1, decreasing = TRUE), dupLabels = TRUE))
```

## Change labels on the fly

```{r changelabs, results = 'asis'}
labs <- c(arm = "Arm", sex = "Sex", mdquality.s = "QOL", freqPercent = "%")
labels(example1) <- labs
summary(example1)
```

You can also supply `labelTranslations=` to `summary()`.

```{r, results = 'asis'}
summary(example1, labelTranslations = labs)
```

## Using `xtable()` to format and print `freqlist()` results

Fair warning: `xtable()` has kind of a steep learning curve. These examples are given without explanation, for more advanced users.

```{r results='asis'}
require(xtable)

# set up custom function for xtable text
italic <- function(x) paste0('<i>', x, '</i>')

xftbl <- xtable(as.data.frame(summary(example1)), 
  caption = "xtable formatted output of freqlist data frame", align="|r|r|r|r|c|c|c|r|")

# change the column names
names(xftbl)[1:3] <- c("Arm", "Gender", "LASA QOL")

print(xftbl, sanitize.colnames.function = italic, include.rownames = FALSE, type = "html", comment = FALSE)
```

## Use `freqlist` in bookdown

Since the backbone of `freqlist()` is `knitr::kable()`, tables still render well in bookdown. However, `print.summary.freqlist()` doesn't use
the `caption=` argument of `kable()`, so some tables may not have a properly numbered caption. To fix this, use the method described
[on the bookdown site](https://bookdown.org/yihui/bookdown/tables.html) to give the table a tag/ID.

```{r eval=FALSE}
summary(freqlist(~ sex + age, data = mockstudy), title="(\\#tab:mytableby) Caption here")
```

# Appendix: Notes regarding table options in R

## NAs

There are several widely used options for basic tables in R. The `table()` function in base R is probably the most common;
by default it excludes NA values. You can change NA handling in `base::table()` using the `useNA=` or `exclude=` arguments.

```{r}
# base table default removes NAs
tab.d1 <- base::table(mockstudy[, c("arm", "sex", "mdquality.s")], useNA="ifany")
tab.d1
```

`xtabs()` is similar to `table()`, but uses a formula-based syntax. However, NAs must be explicitly added to each factor
using the `addNA()` function or using the argument `addNA = TRUE`.

```{r}
# without specifying addNA
tab.d2 <- xtabs(formula = ~ arm + sex + mdquality.s, data = mockstudy)
tab.d2

# now with addNA
tab.d3 <- xtabs(~ arm + sex + addNA(mdquality.s), data = mockstudy)
tab.d3

```

Since the formula method of `freqlist()` uses `xtabs()`, NAs should be treated in the same way.
`includeNA()` can also be helpful here for setting explicit NA values.

## Table dimname names (dnn)

Supplying a data.frame to the `table()` function without giving columns individually will create a contingency table
using all variables in the data.frame.

However, if the columns of a data.frame or matrix are supplied separately (i.e., as vectors),
column names will not be preserved.

```{r}
# providing variables separately (as vectors) drops column names
table(mockstudy$arm, mockstudy$sex, mockstudy$mdquality.s)
```

If desired, you can use the `dnn=` argument to pass variable names.

```{r}
# add the column name labels back using dnn option in base::table
table(mockstudy$arm, mockstudy$sex, mockstudy$mdquality.s, dnn=c("Arm", "Sex", "QOL"))
```

You can also name the arguments to `table()`:

```{r}
table(Arm = mockstudy$arm, Sex = mockstudy$sex, QOL = mockstudy$mdquality.s)
```

If using `freqlist()`, you can provide the labels directly to `freqlist()` or to `summary()` using `labelTranslations=`.