---
title: "The tableby function"
author: "Beth Atkinson, Ethan Heinzen, Jason Sinnwell, Shannon McDonnell and Greg Dougherty"
output: 
  rmarkdown::html_vignette:
    toc: yes
    toc_depth: 3
vignette: |
  %\VignetteIndexEntry{The tableby function}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
---

```{r echo = FALSE}
options(width = 100)
ge330 <- getRversion() >= "3.3.0"
```

# Introduction

One of the most common tables in medical literature includes summary statistics for a set of variables,
often stratified by some group (e.g. treatment arm).  Locally at Mayo, the SAS macros `%table` and `%summary` were
written to create summary tables with a single call. With the increasing interest in R, we have developed
the function `tableby` to create similar tables within the R environment.

In developing the `tableby()` function, the goal was to bring the best features of these macros into an R function.
However, the task was not simply to duplicate all the functionality, but rather to make use of R's strengths
(modeling, method dispersion, flexibility in function definition and output format) and make a tool that fits
the needs of R users.  Additionally, the results needed to fit within the general reproducible research framework
so the tables could be displayed within an R markdown report.

This report provides step-by-step directions for using the functions associated with `tableby()`.
All functions presented here are available within the `arsenal` package.  An assumption is made that users
are somewhat familiar with R Markdown documents.  For those who are new to the topic, a good initial resource
is available at [rmarkdown.rstudio.com](https://rmarkdown.rstudio.com/). 

# Simple Example

The first step when using the `tableby` function is to load the `arsenal` package.  All the examples in this report use
a dataset called `mockstudy` made available by Paul Novotny which includes a variety of types of variables (character,
numeric, factor, ordered factor, survival) to use as examples.

```{r, load-data}
library(arsenal)
require(knitr)
require(survival)
data(mockstudy) ##load data
dim(mockstudy)  ##look at how many subjects and variables are in the dataset 
# help(mockstudy) ##learn more about the dataset and variables
str(mockstudy) ##quick look at the data
```

To create a simple table stratified by treatment arm, use a formula statement to specify the variables that you want summarized.
The example below uses age (a continuous variable) and sex (a factor). 

```{r, simple1}
tab1 <- tableby(arm ~ sex + age, data=mockstudy)
```

If you want to take a quick look at the table, you can use `summary()` on your tableby object and the table
will print out as text in your R console window.  If you use `summary()` without any options you will see a
number of $\ $ statements which translates to "space" in HTML.

## Pretty text version of table

If you want a nicer version in your console window then add the `text=TRUE` option.  

```{r, simple-text}
summary(tab1, text=TRUE)
```

## Pretty Rmarkdown version of table

In order for the report to look nice within an R markdown (knitr) report, you just need to specify
`results="asis"` when creating the R chunk. This changes the layout slightly (compresses it) and bolds the variable names. 

```{r, simple-markdown, results='asis'}
summary(tab1)
```

## Data frame version of table

If you want a data.frame version, simply use `as.data.frame`.

```{r}
as.data.frame(tab1)
```

## Summaries using standard R code

```{r}
## base R frequency example
tmp <- table(Gender=mockstudy$sex, "Study Arm"=mockstudy$arm)
tmp

# Note: The continuity correction is applied by default in R (not used in %table)
chisq.test(tmp)

## base R numeric summary example
tapply(mockstudy$age, mockstudy$arm, summary)
summary(aov(age ~ arm, data=mockstudy))

```

# Modifying Output

## Add labels

In the above example, age is shown with a label (Age in Years), but sex is listed "as is" with lower case letters.
This is because the data was created in SAS and in the SAS dataset, age had a label but sex did not.
The label is stored as an attribute within R.

```{r, check-labels}
## Look at one variable's label
attr(mockstudy$age,'label')

## See all the variables with a label
unlist(lapply(mockstudy,'attr','label'))
# Can also use labels(mockstudy)
```

If you want to add labels to other variables, there are a couple of options.  First, you could add labels to the variables in your dataset.

```{r, add-label, results='asis'}
attr(mockstudy$sex,'label')  <- 'Gender'

tab1 <- tableby(arm ~ sex + age, data=mockstudy)
summary(tab1)
```

You can also use the built-in `data.frame` method for `labels<-`:

```{r, results = 'asis'}
labels(mockstudy)  <- c(age = 'Age, yrs', sex = "Gender")

tab1 <- tableby(arm ~ sex + age, data=mockstudy)
summary(tab1)
```

Another option is to add labels after you have created the table

```{r, results='asis'}
mylabels <- list(sex = "SEX", age = "Age, yrs")
summary(tab1, labelTranslations = mylabels)
```

Alternatively, you can check the variable labels and manipulate them with a function called `labels`, which works on the `tableby` object.

```{r, assignlabels}
labels(tab1)
labels(tab1) <- c(arm="Treatment Assignment", age="Baseline Age (yrs)")
labels(tab1)
```

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

## Change summary statistics globally

Currently the default behavior is to summarize continuous variables with: Number of missing values,
Mean (SD), 25th - 75th quantiles, and Minimum-Maximum values with an ANOVA (t-test with equal variances) p-value.
For categorical variables the default is to show: Number of missing values and count (column percent) with a
chi-square p-value.  This behavior can be modified using the tableby.control function. In fact, you can save
your standard settings and use that for future tables. Note that `test=FALSE` and `total=FALSE` results in the
total column and p-value column not being printed.  


```{r, results='asis'}
mycontrols  <- tableby.control(test=FALSE, total=FALSE,
                               numeric.test="kwt", cat.test="chisq",
                               numeric.stats=c("N", "median", "q1q3"),
                               cat.stats=c("countpct"),
                               stats.labels=list(N='Count', median='Median', q1q3='Q1,Q3'))
tab2 <- tableby(arm ~ sex + age, data=mockstudy, control=mycontrols)
summary(tab2)
```

You can also change these settings directly in the tableby call.  

```{r, results='asis'}
tab3 <- tableby(arm ~ sex + age, data=mockstudy, test=FALSE, total=FALSE, 
                numeric.stats=c("median","q1q3"), numeric.test="kwt")
summary(tab3)
```


## Change summary statistics within the formula

In addition to modifying summary options globally, it is possible to modify the test and summary statistics for
specific variables within the formula statement.  For example, both the kwt (Kruskal-Wallis rank-based) and anova
(asymptotic analysis of variance) tests apply to numeric variables, and we can use one for the variable "age",
another for the variable "bmi", and no test for the variable "ast". A list of all the options is shown at the end of the vignette.

The `tests` function can do a quick check on what tests were performed on each variable in tableby.

```{r, testformula}
tab.test <- tableby(arm ~ kwt(age) + anova(bmi) + notest(ast), data=mockstudy)
tests(tab.test)
```

```{r, results='asis'}
summary(tab.test)
```

Summary statistics for any individual variable can also be modified, but it must be done as secondary
arguments to the test function. The function names must be strings that are functions already written for tableby,
built-in R functions like mean and range, or user-defined functions.

```{r, testsAndStats, results='asis'}
tab.test <- tableby(arm ~ kwt(ast, "Nmiss2","median") + anova(age, "N","mean") +
                    notest(bmi, "Nmiss","median"), data=mockstudy)
summary(tab.test)
```

These can also be passed to the `stats=` argument.

```{r, eval=FALSE}
tab.test <- tableby(arm ~ kwt(ast, stats = c("Nmiss2", "median")) + anova(age, stats = c("N","mean")) +
                    notest(bmi, stats = c("Nmiss","median")), data=mockstudy)
summary(tab.test)
```


## Controlling Options for Categorical Tests (Chisq and Fisher's)

The formal tests for categorical variables against the levels of the by variable, chisq and fe, have
options to simulate p-values. We show how to turn on the simulations for these with 500 replicates
for the Fisher's test (fe).

```{r, simfe, results='asis'}
set.seed(100)
tab.catsim <- tableby(arm ~ sex + race, cat.test="fe", simulate.p.value=TRUE, B=500, data=mockstudy)
tests(tab.catsim)
```

The chi-square test on 2x2 tables applies Yates' continuity correction by default, so we provide an option
to turn off the correction. We show the results with and without the correction that is applied to
treatment arm by sex, if we use subset to ignore one of the three treatment arms.

```{r, chisqcorrect, results='asis'}
cat.correct <- tableby(arm ~ sex + race, cat.test="chisq", subset = !grepl("^F", arm), data=mockstudy)
tests(cat.correct)
cat.nocorrect <- tableby(arm ~ sex + race, cat.test="chisq", subset = !grepl("^F", arm),
     chisq.correct=FALSE, data=mockstudy)
tests(cat.nocorrect)
```

## Modifying the look & feel in Word documents 

You can easily create Word versions of `tableby` output via an Rmarkdown report and the default options will give you a reasonable table in Word - just select the "Knit Word" option in RStudio.

**The functionality listed in this next paragraph is coming soon but needs an upgraded version of RStudio**
If you want to modify fonts used for the table, then you'll need to add an extra line to your header at the beginning of your file.
You can take the `WordStylesReference01.docx` file and modify the fonts (storing the format preferences in your project directory).
To see how this works, run your report once using WordStylesReference01.docx and then WordStylesReference02.docx. 

```
output: word_document
  reference_docx: /projects/bsi/gentools/R/lib320/arsenal/doc/WordStylesReference01.docx 
```

For more information on changing the look/feel of your Word document, see the [Rmarkdown documentation](https://pkg.yihui.org/rmarkdown-book/word-document.html) website.


# Additional Examples

Here are multiple examples showing how to use some of the different options.

## 1. Summarize without a group/by variable

```{r, nobyvar, results='asis'}
tab.noby <- tableby(~ bmi + sex + age, data=mockstudy)
summary(tab.noby)
```

## 2. Display footnotes indicating which "test" was used

```{r, results="asis"}
summary(tab.test, pfootnote=TRUE)
```

## 3. Summarize an ordered factor

When comparing groups of ordered data there are a couple of options.  The **default** uses a general independence test available from the `coin` package.
For two-group comparisons, this is essentially the Armitage trend test.  The other option is to specify the Kruskal Wallis test.
The example below shows both options.

```{r}
mockstudy$age.ordnew <- ordered(c("a",NA,as.character(mockstudy$age.ord[-(1:2)])))
table(mockstudy$age.ord, mockstudy$sex)
table(mockstudy$age.ordnew, mockstudy$sex)
class(mockstudy$age.ord)
```

```{r, results="asis", eval=requireNamespace("coin", quietly = TRUE)}
summary(tableby(sex ~ age.ordnew, data = mockstudy), pfootnote = TRUE)
summary(tableby(sex ~ age.ord, data = mockstudy), pfootnote = TRUE)
```

## 4. Summarize a survival variable

First look at the information that is presented by the `survfit()` function, then see how the same results can be seen with tableby.
The default is to show the median survival (time at which the probability of survival = 50%). 

```{r, eval=ge330}
survfit(Surv(fu.time, fu.stat)~sex, data=mockstudy)
survdiff(Surv(fu.time, fu.stat)~sex, data=mockstudy)
```

```{r, results='asis'}
summary(tableby(sex ~ Surv(fu.time, fu.stat), data=mockstudy))
```

It is also possible to obtain summaries of the % survival at certain time points (say the probability of surviving 1-year).

```{r, eval=ge330}
summary(survfit(Surv(fu.time/365.25, fu.stat)~sex, data=mockstudy), times=1:5)
```

```{r, results='asis', eval=ge330}
summary(tableby(sex ~ Surv(fu.time/365.25, fu.stat), data=mockstudy, times=1:3, surv.stats=c("NeventsSurv")))

summary(tableby(sex ~ Surv(fu.time/365.25, fu.stat), data=mockstudy, times=1:5, surv.stats=c("NriskSurv")))

summary(tableby(sex ~ Surv(fu.time/365.25, fu.stat), data=mockstudy, surv.stats="medSurvCI", survconf.type='log-log'), digits=2)

summary(tableby(sex ~ Surv(fu.time/365.25, fu.stat), data=mockstudy, surv.stats="medSurvQuant"), digits=2)


```

## 5. Summarize date variables

Date variables by default are summarized with the number of missing values, the median, and the range.
For example purposes we've created a random date.  Missing values are introduced for impossible February dates.

```{r, results='asis'}
set.seed(100)
N <- nrow(mockstudy)
mockstudy$dtentry <- mdy.Date(month=sample(1:12,N,replace=T), day=sample(1:29,N,replace=T), 
                              year=sample(2005:2009,N,replace=T))
summary(tableby(sex ~ dtentry, data=mockstudy))
```

## 6. Summarize multiple variables without typing them out

Often one wants to summarize a number of variables.  Instead of typing by hand each individual variable,
an alternative approach is to create a formula using the `paste` command with the `collapse="+"` option.  

```{r, results='asis'}
## create a vector specifying the variable names
myvars <- names(mockstudy)

## select the 8th through the last variables
## paste them together, separated by the + sign
RHS <- paste(myvars[8:10], collapse="+")
RHS

## create a formula using the as.formula function
as.formula(paste('arm ~ ', RHS))

## use the formula in the tableby function
summary(tableby(as.formula(paste('arm ~', RHS)), data=mockstudy))
```

These steps can also be done using the `formulize` function.

```{r, results='asis'}
## The formulize function does the paste and as.formula steps
tmp <- formulize('arm',myvars[8:10])
tmp

## More complex formulas could also be written using formulize
tmp2 <- formulize('arm',c('ps','hgb^2','bmi'))

## use the formula in the tableby function
summary(tableby(tmp, data=mockstudy))
```

To change summary statistics or statistical tests en masse, consider using `paste0()` together with `formulize()`:

```{r results='asis'}
varlist1 <- c('age','sex','hgb')
varlist2 <- paste0("anova(", c('bmi','alk.phos','ast'), ", 'meansd')")

summary(tableby(formulize("arm", c(varlist1, varlist2)),
                data = mockstudy, numeric.test = "kwt"), pfootnote = TRUE)
```

## 7. Subset the dataset used in the analysis

Here are two ways to get the same result (limit the analysis to subjects age>5 and in the F: FOLFOX treatment group). 

* The first approach uses the subset function applied to the dataset `mockstudy`.  This example also selects a subset of variables.
The `tableby` function is then applied to this subsetted data.


```{r}
newdata <- subset(mockstudy, subset=age>50 & arm=='F: FOLFOX', select = c(sex,ps:bmi))
dim(mockstudy)
table(mockstudy$arm)
dim(newdata)
names(newdata)
```

```{r, results='asis'}
summary(tableby(sex ~ ., data=newdata))
```

* The second approach does the same analysis but uses the subset
argument within `tableby` to subset the data.

```{r, results='asis'}
summary(tableby(sex ~ ps + hgb + bmi, subset=age>50 & arm=="F: FOLFOX", data=mockstudy))
```

## 8. Create combinations of variables on the fly

```{r}
## create a variable combining the levels of mdquality.s and sex
with(mockstudy, table(interaction(mdquality.s,sex)))
```

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

```{r, results='asis'}
## create a new grouping variable with combined levels of arm and sex
summary(tableby(interaction(mdquality.s, sex) ~  age + bmi, data=mockstudy, subset=arm=="F: FOLFOX"))
```

## 9. Transform variables on the fly

Certain transformations need to be surrounded by `I()` so that R knows to treat it as a variable transformation and
not some special model feature.  If the transformation includes any of the symbols `/ - + ^ *` then surround the new variable by `I()`.


```{r, maketrans, results='asis'}
trans <- tableby(arm ~ I(age/10) + log(bmi) + factor(mdquality.s, levels=0:1, labels=c('N','Y')),
                 data=mockstudy)
summary(trans)
```

The labels for these variables aren't exactly what we'd like, so we can change modify those after the fact.
Instead of typing out the very long variable names, you can modify specific labels by position. 

```{r, assignlabels2}
labels(trans)
labels(trans)[2:4] <- c('Age per 10 yrs', 'log(BMI)', 'MD Quality')
labels(trans)
```

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

Note that if we had not changed `mdquality.s` to a factor, it would have been summarized as though it were a continuous variable.

```{r, results='asis'}
class(mockstudy$mdquality.s)
summary(tableby(arm~mdquality.s, data=mockstudy))
```

Another option would be to specify the test and summary statistics.
In fact, if I had a set of variables coded 0/1 and that was all I was summarizing, then I could change the global option
for continuous variables to use the chi-square test and show countpct.

```{r, results='asis'}
summary(tableby(arm ~ chisq(mdquality.s, "Nmiss","countpct"), data=mockstudy))
```


## 10. Subsetting (change the ordering of the variables, delete a variable, sort by p-value, filter by p-value, show only certain by-levels)

```{r, results='asis'}
mytab <- tableby(arm ~ sex + alk.phos + age, data=mockstudy)
mytab2 <- mytab[c('age','sex','alk.phos')]
summary(mytab2)
summary(mytab[c('age','sex')], digits = 2)
summary(mytab[c(3,1)], digits = 3)
summary(sort(mytab, decreasing = TRUE))
summary(mytab[mytab < 0.5])
head(mytab, 1) # can also use tail()

summary(tableby(list(arm, sex) ~ sex + alk.phos + age, data=mockstudy)[, "sex"])
summary(tableby(list(arm, sex) ~ sex + alk.phos + age, data=mockstudy)[, list(sex = "Female", arm = c("F: FOLFOX", "Total"))])
```

## 11. Merge two `tableby` objects together 

It is possible to combine two tableby objects so that they print out together. Overlapping by-variables will have their x-variables
concatenated, and (if `all=TRUE`) non-overlapping by-variables will have their tables printed separately.

```{r, results="asis"}
## demographics
tab1 <- tableby(arm ~ sex + age, data=mockstudy,
                control=tableby.control(numeric.stats=c("Nmiss","meansd"), total=FALSE))
## lab data
tab2 <- tableby(arm ~ hgb + alk.phos, data=mockstudy,
                control=tableby.control(numeric.stats=c("Nmiss","median","q1q3"),
                                        numeric.test="kwt", total=FALSE))
tab12 <- merge(tab1, tab2)
class(tab12)
summary(tab12)
```

For tables with two different outcomes, consider the `all=TRUE` argument:

```{r, results='asis'}
summary(merge(
  tableby(sex ~ age, data = mockstudy),
  tableby(arm ~ bmi, data = mockstudy),
  all = TRUE
))
```

## 12. Add a title to the table

When creating a pdf the tables are automatically numbered and the title appears below the table.  In Word and HTML, the titles appear un-numbered and above the table.  

```{r, results='asis'}
t1 <- tableby(arm ~ sex + age, data=mockstudy)
summary(t1, title='Demographics')
```

With multiple left-hand sides, you can pass a vector or list to determine labels for each table:

```{r, results='asis'}
summary(tableby(list(arm, sex) ~ age, data = mockstudy), title = c("arm table", "sex table"))
```

## 13. Modify how missing values are displayed

Depending on the report you are writing you have the following options:

* Show how many subjects have each variable

* Show how many subjects are missing each variable

* Show how many subjects are missing each variable only if there are any missing values

* Don't indicate missing values at all

```{r}
## look at how many missing values there are for each variable
apply(is.na(mockstudy),2,sum)
```

```{r, results='asis'}
## Show how many subjects have each variable (non-missing)
summary(tableby(sex ~ ast + age, data=mockstudy,
                control=tableby.control(numeric.stats=c("N","median"), total=FALSE)))

## Always list the number of missing values
summary(tableby(sex ~ ast + age, data=mockstudy,
                control=tableby.control(numeric.stats=c("Nmiss2","median"), total=FALSE)))

## Only show the missing values if there are some (default)
summary(tableby(sex ~ ast + age, data=mockstudy, 
                control=tableby.control(numeric.stats=c("Nmiss","mean"),total=FALSE)))

## Don't show N at all
summary(tableby(sex ~ ast + age, data=mockstudy, 
                control=tableby.control(numeric.stats=c("mean"),total=FALSE)))
```

One might also consider the use of `includeNA()` to include NAs in the counts and percents for categorical variables.

```{r, results = 'asis'}
mockstudy$ps.cat <- factor(mockstudy$ps)
attr(mockstudy$ps.cat, "label") <- "ps"
summary(tableby(sex ~ includeNA(ps.cat), data = mockstudy, cat.stats = "countpct"))
```

## 14. Modify the number of digits used

Within tableby.control function there are 4 options for controlling the number of significant digits shown.  

* digits: controls the number of digits after the decimal place for continuous values

* digits.count: controls the number of digits after the decimal point for counts

* digits.pct: controls the number of digits after the decimal point for percents

* digits.p: controls the number of digits after the decimal point for p-values

```{r, results='asis'}
summary(tableby(arm ~ sex + age + fu.time, data=mockstudy), digits=4, digits.p=2, digits.pct=1)
```

All of these can be specified on a per-variable basis
using the in-formula functions that specify which tests are run:

```{r results='asis'}
summary(tableby(arm ~ chisq(sex, digits.pct=1) + anova(age, digits=4) +
                  anova(fu.time, digits = 1, digits.p = 6, format.p = FALSE), data=mockstudy))
```


## 15. Create a user-defined summary statistic

For purposes of this example, the code below creates a trimmed mean function (trims 10%)
and use that to summarize the data. Note the use of the `...` which tells R to pass extra arguments on - this is required
for user-defined functions.  In this case, `na.rm=T` is passed to `myfunc`.  The *weights* argument is also required, even though
it isn't passed on to the internal function in this particular example.   

```{r, results='asis'}
trim10 <- function(x, weights=NULL, ...){
  mean(x, trim=.1, ...)
}

summary(tableby(sex ~ hgb, data=mockstudy, 
                control=tableby.control(numeric.stats=c("Nmiss","trim10"), numeric.test="kwt",
                    stats.labels=list(Nmiss='Missing values', trim10="Trimmed Mean, 10%"))))
```

For statistics to be formatted appropriately, you may want to use `as.tbstat()`.
For example, suppose you want to create a trimmed mean function that trims by both 5 and 10 percent.
The first example shows them separated by a comma; the second puts the 10% trimmed mean in brackets.
`as.tbstat()` takes a `fmt=` argument with a `glue` string specification, where the current value is exposed as
`x` and a formatted value (using `tbfmt()`) is exposed as `y`. See `?as.tbstat` for details.

```{r, results='asis'}
trim510comma <- function(x, ...){
  tmp <- c(mean(x, trim = 0.05, ...), mean(x, trim = 0.1, ...))
  as.tbstat(tmp, fmt = "{y[1]}, {y[2]}")
}
trim510bracket <- function(x, ...){
  tmp <- c(mean(x, trim = 0.05, ...), mean(x, trim = 0.1, ...))
  as.tbstat(tmp, fmt = "{y[1]} [{y[2]}]")
}

summary(tableby(sex ~ hgb, data=mockstudy, numeric.stats=c("Nmiss", "trim510comma", "trim510bracket"), test = FALSE))
```

Or perhaps it's useful to put the amount of trimming in parentheses. Since it is a percent, we can
flag it as such:

```{r results='asis'}
trim10pct <- function(x, ...){
  tmp <- mean(x, trim = 0.05, ...)
  as.tbstat(c(tmp, 10), fmt = "{y[1]} ({y[2]}%)", which.pct = 2L)
}
summary(tableby(sex ~ hgb, data=mockstudy, numeric.stats=c("Nmiss", "trim10pct"),
                digits = 2, digits.pct = 0, test = FALSE))

```

Finally, if there's a pre-computed summary statistic, you can easily change the formatting, like in `meanpmsd()`:

```{r}
meanpmsd
```

For example:

```{r}
dollarmean <- function(x, na.rm = TRUE, ...) {
  if(na.rm && allNA(x)) {
    as.tbstat(NA_real_)
  } else {
    out <- meansd(x, na.rm = na.rm, ...)
    attr(out, "fmt") <- "${y[1]}"
    out
  }
}

summary(tableby(sex ~ notest(age, "dollarmean", digits = 2), data = mockstudy))
```

## 16. Use case-weights for creating summary statistics

When comparing groups, they are often unbalanced when it comes to nuisances such as age and sex.
The `tableby` function allows you to create weighted summary statistics.  If this option us used then p-values are not calculated (`test=FALSE`).

```{r}
##create fake group that is not balanced by age/sex 
set.seed(200)
mockstudy$fake_arm <- ifelse(mockstudy$age>60 & mockstudy$sex=='Female',sample(c('A','B'),replace=T, prob=c(.2,.8)),
                            sample(c('A','B'),replace=T, prob=c(.8,.4)))

mockstudy$agegp <- cut(mockstudy$age, breaks=c(18,50,60,70,90), right=FALSE)

## create weights based on agegp and sex distribution
tab1 <- with(mockstudy,table(agegp, sex))
tab2 <- with(mockstudy, table(agegp, sex, fake_arm))
tab2
gpwts <- rep(tab1, length(unique(mockstudy$fake_arm)))/tab2
gpwts[gpwts>50] <- 30

## apply weights to subjects
index <- with(mockstudy, cbind(as.numeric(agegp), as.numeric(sex), as.numeric(as.factor(fake_arm)))) 
mockstudy$wts <- gpwts[index]

## show weights by treatment arm group
tapply(mockstudy$wts,mockstudy$fake_arm, summary)
```

```{r, results='asis', eval=ge330}
orig <- tableby(fake_arm ~ age + sex + Surv(fu.time/365, fu.stat), data=mockstudy, test=FALSE)
summary(orig, title='No Case Weights used')
tab1 <- tableby(fake_arm ~ age + sex + Surv(fu.time/365, fu.stat), data=mockstudy, weights=wts)
summary(tab1, title='Case Weights used')
```
                
                
## 17. Create your own p-value and add it to the table

When using weighted summary statistics, it is often desirable to then show a p-value from a model that corresponds to the weighted analysis. 
It is possible to add your own p-value and modify the column title for that new p-value.  Another use for this would be to add standardized 
differences or confidence intervals instead of a p-value.

To add the p-value, you simply need to create a data frame and use the function `modpval.tableby()`.
The first few columns in the data.frame are required: (1) the by-variable, (2) the strata value (if
the table has a strata term), (3) the x-variable, and (4) the new p-value (or test statistic).
Another optional column can be used to indicate what method was used to calculate the p-value.
If you specify `use.pname=TRUE` then the column name indicating the p-value will be also be used in the tableby summary.


```{r, results='asis', eval=ge330}
mypval <- data.frame(
  byvar = "fake_arm",
  variable = c('age','sex','Surv(fu.time/365, fu.stat)'), 
  adj.pvalue = c(.953,.811,.01), 
  method = c('Age/Sex adjusted model results')
)
tab2 <- modpval.tableby(tab1, mypval, use.pname=TRUE)
summary(tab2, title='Case Weights used, p-values added', pfootnote=TRUE)
```

## 18. For two-level categorical variables or one-line numeric variables, simplify the output.

If the `cat.simplify` option is set to `TRUE`, then only the second level of two-level
categorical varialbes is shown.  In the example below, `sex` has two levels,
and "Female" is the second level, hence only the counts and percents for Female are shown.
Similarly, "mdquality.s" was turned to a factor, and "1" is the second level, but since
there are missings, the table ignores `cat.simplify` and displays all levels (since the
output can no longer be displayed on one line).

```{r, results='asis'}
table2 <- tableby(arm~sex + factor(mdquality.s), data=mockstudy, cat.simplify=TRUE)
summary(table2, labelTranslations=c(sex="Female", "factor(mdquality.s)"="MD Quality"))
```

Similarly, if `numeric.simplify` is set to `TRUE`, then any numerics which only have one
row of summary statistics are simplified into a single row. Note again that `ast` has
missing values and so is not simplified to a single row.

```{r results='asis'}
summary(tableby(arm ~ age + ast, data = mockstudy,
                numeric.simplify=TRUE, numeric.stats=c("Nmiss", "meansd")))
```

The in-formula functions to change which tests are run can also be used to specify these
options for each variable at a time.

```{r results='asis'}
summary(tableby(arm ~ anova(age, "meansd", numeric.simplify=TRUE) +
                  chisq(sex, cat.simplify=TRUE), data = mockstudy))
```

The `cat.simplify` and `ord.simplify` argument also accept the special string `"label"`, which appends the shown level's label to the overall label:

```{r results='asis'}
summary(tableby(arm ~ sex, cat.simplify = "label", data = mockstudy))
```


## 19. Use `tableby` within an Sweave document

For those users who wish to create tables within an Sweave document, the following code seems to work.

```
\documentclass{article}

\usepackage{longtable}
\usepackage{pdfpages}

\begin{document}

\section{Read in Data}
<<echo=TRUE>>=
require(arsenal)
require(knitr)
require(rmarkdown)
data(mockstudy)

tab1 <- tableby(arm~sex+age, data=mockstudy)
@

\section{Convert Summary.Tableby to LaTeX}
<<echo=TRUE, results='hide', message=FALSE>>=
capture.output(summary(tab1), file="Test.md")

## Convert R Markdown Table to LaTeX
render("Test.md", pdf_document(keep_tex=TRUE))
@ 

\includepdf{Test.pdf}

\end{document}
```

## 20. Export `tableby` object to a .CSV file

When looking at multiple variables it is sometimes useful to export the results to a csv file. The `as.data.frame` function creates a data frame object that can be exported or further manipulated within R.

```{r}
tab1 <- summary(tableby(arm~sex+age, data=mockstudy), text = NULL)
as.data.frame(tab1)

# write.csv(tab1, '/my/path/here/my_table.csv')
```

## 21. Write `tableby` object to a separate Word or HTML file

```{r eval = FALSE}
## write to an HTML document
tab1 <- tableby(arm ~ sex + age, data=mockstudy)
write2html(tab1, "~/trash.html")

## write to a Word document
write2word(tab1, "~/trash.doc", title="My table in Word")
```

## 22. Use `tableby` in R Shiny

The easiest way to output a `tableby()` object in an R Shiny app is to use the `tableOutput()` UI in combination with
the `renderTable()` server function and `as.data.frame(summary(tableby()))`:

```{r eval=FALSE}
# A standalone shiny app
library(shiny)
library(arsenal)
data(mockstudy)

shinyApp(
  ui = fluidPage(tableOutput("table")),
  server = function(input, output) {
    output$table <- renderTable({
      as.data.frame(summary(tableby(sex ~ age, data = mockstudy), text = "html"))
    }, sanitize.text.function = function(x) x)
  }
)
```

This can be especially powerful if you feed the selections from a `selectInput(multiple = TRUE)` into `formulize()` to make
the table dynamic!

## 23. Use `tableby` in bookdown

Since the backbone of `tableby()` is `knitr::kable()`, tables still render well in bookdown. However, `print.summary.tableby()` 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://pkg.yihui.org/bookdown/tables.html) to give the table a tag/ID.

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

## 24. Adjust `tableby` for multiple p-values

The `padjust()` function is a new S3 generic piggybacking off of `p.adjust()`. It works on both `tableby` and `summary.tableby` objects:

```{r results='asis'}
tab <- summary(tableby(sex ~ age + fu.time + bmi + mdquality.s, data = mockstudy))
tab
padjust(tab, method = "bonferroni")
```

## 25. Tabulate multiple endpoints

You can now use `list()` on the left-hand side of `tableby()` to give multiple endpoints.

```{r results='asis'}
summary(tableby(list(sex, mdquality.s, ps) ~ age + bmi, data = mockstudy))
```

To avoid confusion about which table is which endpoint, you can set `term.name=TRUE` in `summary()`. This takes the labels
for each by-variable and puts them in the top-left of the table.

```{r results='asis'}
summary(tableby(list(sex, mdquality.s, ps) ~ age + bmi, data = mockstudy), term.name = TRUE)
```

## 26. Tabulate data by a non-test group (strata)

You can also specify a second grouping variable that doesn't get tested (but instead separates results): a *strata* variable.

```{r results='asis'}
summary(tableby(list(sex, ps) ~ age + bmi, strata = arm, data = mockstudy))
```

# Available Function Options

## Summary statistics

The **default** summary statistics, by varible type, are:

* `numeric.stats`: Continuous variables will show by default `Nmiss, meansd, range`
* `cat.stats`: Categorical and factor variables will show by default `Nmiss, countpct`
* `ordered.stats`: Ordered factors will show by default `Nmiss, countpct`
* `surv.stats`: Survival variables will show by default `Nmiss, Nevents, medsurv`
* `date.stats`: Date variables will show by default `Nmiss, median, range`

There are a number of extra functions defined specifically for the tableby function.

* `N`: a count of the non-missing number of observations for a particular group
* `Npct`: a count of the non-missing number of observations and the percentage of the column total (missing + non-missing) in the format `N (%)`
* `Nrowpct`: a count of the non-missing number of observations and the row-percentage (of non-missings) in the format `N (%)`
* `Nmiss`: only show the count of the number of missing values if there are some missing values
* `Nmiss2`: always show a count of the number of missing values for a variable within each group
* `Nmisspct`: show the count of the number of missing values and the percentage of the column total (missing+non-missing) if there are some missing values
* `Nmisspct2`: The same as `Nmisspct`, but always show.
* `meanse`: print the mean and standard error in the format `mean(se)`
* `meanCI`: print the mean and a (t) confidence interval
* `count`: print the number of values in a category
* `countN`: print the number of values in a category plus the total N for the group in the format `N/Total`
* `countpct`: print the number of values in a category plus the column-percentage in the format `N (%)`
* `pct`: print the column-percentage
* `countrowpct`: print the number of values in a category plus the row-percentage in the format `N (%)`
* `rowpct`: print the row-percentage
* `countcellpct`: print the number of values in a category plus the cell-percentage in the format `N (%)`
* `binomCI`: print the proportion in a category plus a binomial confidence interval.
* `rowbinomCI`: print the row proportion in a category plus a binomial confidence interval.
* `medianq1q3`: print the median, 25th, and 75th quantiles `median (Q1, Q3)`
* `q1q3`: print the 25th and 75th quantiles `Q1, Q3`
* `iqr`: print the inter-quartile range.
* `medianrange`: print the median, minimum and maximum values `median (minimum, maximum)`
* `medianmad`: print the median and median absolute deviation (mad)
* `Nevents`: print number of events for a survival object within each grouping level
* `medSurv`: print the median survival
* `NeventsSurv`: print number of events and survival at given times
* `NriskSurv`: print the number still at risk and survival at given times
* `Nrisk`: print the number still at risk at given times
* `medTime`: print the median follow-up time
* `sum`
* `max`
* `min`
* `mean`
* `sd`
* `var`
* `median`
* `range`
* `gmean`, `gsd`, `gmeansd`, `gmeanCI`: geometric means, sds, and confidence intervals.

## Testing options

The tests used to calculate p-values differ by the variable type, but can be specified
explicitly in the formula statement or in the control function.

The following tests are accepted:

* `anova`: analysis of variance test; the default test for continuous variables. When
    the grouping variable has two levels, it is equivalent to the two-sample t-test with equal variance. 
    
* `kwt`: Kruskal-Wallis test, optional test for continuous
    variables. When the grouping variable has two levels, it is equivalent to the Wilcoxon Rank Sum test.

* `wt`: An explicit Wilcoxcon test.
    
* `medtest`: Median test test, optional test for continuous variables.

* `chisq`: chi-square goodness of fit test or Pearson chi-squared test; the default for categorical
    or factor variables

* `fe`: Fisher's exact test for categorical variables; optional 
  
* `logrank`: log-rank test, the default test for time-to-event
    variables

* `trend`: The `independence_test` function from the `coin` is used to test for trends.  Whenthe grouping variable has two levels,
  it is equivalent to the Armitage trend test.  This is the default for ordered factors
  
* `stddiff`: perform standardized differences.
  
* `notest`: Don't perform a test.

## `tableby.control` settings

A quick way to see what arguments are possible to utilize in a function is to use the `args()` command. Settings involving the number of digits can be set in `tableby.control` or in `summary.tableby`.

```{r}
args(tableby.control)
```

## `summary.tableby` settings

The `summary.tableby` function has options that modify how the table appears (such as adding a title or modifying labels).  

```{r}
args(arsenal:::summary.tableby)
```
