--- title: "summaryByVisit" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{summaryByVisit} %\VignetteEncoding{UTF-8} %\VignetteEngine{knitr::rmarkdown} editor_options: markdown: wrap: 72 --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", echo = TRUE, eval = TRUE, warning=FALSE, fig.height = 6, fig.width = 9, fig.align='center' ) ``` ```{r color-function, echo = FALSE} colorize <- function(text, color) { if (knitr::is_latex_output()) { sprintf("\\textcolor{%s}{%s}", color, text) } else if (knitr::is_html_output()) { sprintf("%s", color, text) } else text } ``` ```{r setup, echo = FALSE, message = FALSE, warning = FALSE} library(dplyr) library(tidyverse) library(gtsummary) library(summarySCI) library(flextable) ``` The function `summaryByVisit()` produces a table with descriptive statistics for continues variables at different time points (visits). It is largely based on the function `gtsummary::tbl_summary()` and `gtsummary::tbl_strata_nested_stack`. The changes as compared to these functions are: - A hierarchical summary table is created by variable (lab-value), visit and visitgroup. ## Setup and data To demonstrate the various functionalities of the function, we will create a small dataset. We have three hypotetical lab-values: LDH, ANC and Lymphocytes. Each lab-value has been measured at 10 time points (visits), which can further be divided into three visit groups (baseline, treatment and follow-up). In addition, we have a grouping variable called 'arm'. ```{r, message = FALSE} data<-NULL visit <- c(paste0(rep("Visit ", 10), rbind(c(1:10))), paste0(rep("Visit ", 10), rbind(c(1:10))), paste0(rep("Visit ", 10), rbind(c(1:10)))) data <- as.data.frame(cbind( visit, rnorm(30))) data<-as.data.frame(rbind(data, data, data, data, data)) data$visitgroup<- ifelse(data$visit %in% c("Visit 1", "Visit 2"), "Baseline", ifelse(data$visit %in% c("Visit 3", "Visit 4"), "Treatment", "Follow-up")) data$visitgroup<-factor(data$visitgroup, levels = c("Baseline", "Treatment", "Follow-up")) data$LDH<-rnorm(150) data$Lymphocytes<-rnorm(150) data$ANC<-rnorm(150) data$LDH[3]<-NA data$arm<- c(rep("Arm A", 70), rep("Arm B", 80)) ``` ## Basic table Now, we use `summarySCI::summaryLevels` to create a summary table for median and range of each lab-value by visit. ```{r} summaryByVisit(data, vars = c("LDH", "Lymphocytes", "ANC"), visit = "visit", add_n = TRUE) ``` ## Add visit group We can the visit group to group visits accordingly. Visitgroup needs to be an ordered factor. ```{r} summaryByVisit(data, vars = c("LDH", "Lymphocytes", "ANC"), visitgroup = "visitgroup", visit = "visit") ``` ## By group We can stratify the table by groups via the `group` argument. The overall column can still be shown if desired, using the `overall = TRUE` argument. A maximum of 3 groups are supported. ```{r} summaryByVisit(data, vars = c("LDH", "Lymphocytes", "ANC"), group = "arm", visitgroup = "visitgroup", visit = "visit") ``` ```{r} summaryByVisit(data, vars = c("LDH", "Lymphocytes", "ANC"), group = "arm", overall = TRUE, visitgroup = "visitgroup", visit = "visit") ``` ## Add N Sample size can be shown for each column, if the option `add_n` is set to `TRUE`. ```{r} summaryByVisit(data, vars = c("LDH", "Lymphocytes", "ANC"), group = "arm", overall = TRUE, visitgroup = "visitgroup", visit = "visit", add_n = TRUE) ``` # Further steps: - improve costumization