library(dplyr)
library(epitable)
library(knitr)
knitr::opts_chunk$set( cache = FALSE )
color: black;
border-bottom: 1px black; border-top: 2px black;

r Sys.Date()

Introduction

epitable produces tables in the style of epidemiological papers. The tables are intended for knitr Rmarkdown documents. The look of the tables depend on the CSS of the Rmarkdown document. This vignette was created using Rstudios default CSS. The package has two functions:

freq_by

The freq_by-function creates a frequency and percentage table commonly used in epidemiological papers. It is a wrapper for the excellent htmlTables-package.

head(example_data) 

The by_group variable must be of class factor, to ensure correct order. The argument min_cell_count sets the minimum number of observations in a cell. It is inteded to preserve anonymity in case of sensitive data. When the cell count is <= min_cell_count, the number is deleted from that cell.

freq_by(dataset = example_data, var_vector = c("color", "clarity"), by_group = "cut" )

model_to_html

The model_to_html-function can print simple models to HTML, also printing the reference levels of the categorical covariates. It does not support variables of class ordered.

Printing two lists of models of logistic regression

The example below tages two lists of models. One list of univariate models, and another list of multivariate models. The multivariate models include incrementally more variables, so that the final multivariate model include all the variables used in the univariate models.

# univariate models in a list:
c("Age" , "Embarked" , "Sex" , "Fare" , "Pclass") %>%
  paste0( "Survived ~ ", . ) %>%
  purrr::map( ~ glm( as.formula(.), data = titanic, family = "binomial" )) -> univar_list

# multivariate models in a list:
glm_logistic_1 <- glm( Survived ~  Age + Embarked, data = titanic, family = "binomial")
glm_logistic_2 <- glm( Survived ~  Age + Embarked + Sex + Fare, data = titanic, family = "binomial")
glm_logistic_3 <- glm( Survived ~  Age + Embarked + Sex + Fare + Pclass, data = titanic, family = "binomial")
multi_model_list <- list(glm_logistic_1, glm_logistic_2,  glm_logistic_3 )

Using the two lists of models defined above the table is created:

model_to_html( univar_list,  multi_model_list, exponentiate = TRUE )

Printing two lists of models of cox proportional hazards regression

The example below creates a list of univariate models of class coxph, and a list of multivariate models of class coxph. The output is a table showing the how the parameters change as more are added sequentially.

lung <- survival::lung
c( "inst",  "age", "sex", "ph.ecog", "ph.karno", "pat.karno", "meal.cal", "wt.loss" )  %>%
  paste0( " survival::Surv( time, status==2) ~ ", . ) %>%
  purrr::map( ~ survival::coxph( as.formula(.), data = lung )) -> univariate_coxph_list

coxph_model1 <- survival::coxph( survival::Surv( time, status==2) ~ inst + age + sex, lung)
coxph_model2 <- survival::coxph( survival::Surv( time, status==2) ~ inst + age + sex + ph.ecog + ph.karno, lung)
coxph_model3 <- survival::coxph( survival::Surv( time, status==2) ~ inst + age + sex + ph.ecog + ph.karno + pat.karno, lung)
coxph_model4 <- survival::coxph( survival::Surv( time, status==2) ~ inst + age + sex + ph.ecog + ph.karno + pat.karno + meal.cal + wt.loss, lung)

multivariate_coxph_list <- list( coxph_model1, coxph_model2, coxph_model3, coxph_model4 )
model_to_html( univariate_coxph_list, multivariate_models_list = multivariate_coxph_list )

You can change the number of digits and the font:

model_to_html( univariate_coxph_list, multivariate_models_list = multivariate_coxph_list, font_css = "font-family: Times;", decimals_estimate = 4)

It is possible to change the font using the font_css argument, but the font needs to be web-friendly. Below the font is set to "font-family: Courier":

freq_by(dataset = example_data, var_vector = c("color", "clarity"), by_group = "cut", font_css = "font-family: Arial" )

To do in a possible future:

On width of CSS output

The default CSS of Rstudio looks great, but sometimes the content part is too narrow, which can introduce linebreaks in a wide table. To avoid this you can increase the width of the content. To do this put this code at the top of the rmarkdown document (but below the yaml). It sets the content width to 1400 pixels.

<style type="text/css">
.main-container {
  max-width: 1400px;
  margin-left: auto;
  margin-right: auto;
}
</style>


rasmusrhl/epitable documentation built on May 30, 2019, 7:04 p.m.