Several functions for descriptive analyses (tables, figures, etc.)
Descriptive table
tableDESC : To create a comparative table with a treatment and tests.
This function calculates descriptive statistics for continuous and categorical variables.
tableDESC(data, vars=NULL, labels=NULL, trt=NULL, digits=c(1,0), nolevels=NULL, nonnormal=NULL, nrpval=4, boldpval=0.05, boldsmd=0.1, reference=TRUE, export=FALSE, pvalue=TRUE, smd=FALSE, missing=FALSE, paired=FALSE, legend=TRUE, binary01=FALSE)
| data | An object of the class data.frame containing the variables cited in vars. |
| vars | A list of variable names to be described. If NULL, all the variable in data will be described except those with more than 20 levels (date, subject id …). |
| labels | A list of labels for the variables. If NULL, vars will be used. |
| trt | A character string with the name of the comparative variable. |
| digits | A list of length 2. The first argument specifies the number of decimal for continuous variables. The second specifies the number of decimal for percentages. The default value is c(1,0). |
| nolevels | A list of variables with only 2 levels which will be described on a single line and the first level will be described, thus relevel needed. |
| nonnormal | A list of variables not normally distributed. Specify "none" if every continuous variable is normally distributing and "all" if every continuous variable is non-normally distributing. See details to know how variables will be described according to their distribution. |
| nrpval | Number of decimals to be used for the pvalue. The default value is 4. |
| boldpval | The statistical significance for p-value. Pvalues lower than set level will be in bold. The default value is 0.05. |
| boldsmd | The SMD threshold to considering an unbalance. SMD upper than set level will be in bold. The default value is 0.1 (10%). |
| reference | TRUE by default, any other value will remove the ", n (%)" after the label for categorical variables. |
| export | A logical value specifying if a word document with the descriptive table should be exporting. Default is FALSE. |
| pvalue | A logical value specifying if the pvalue should be add to compared groups. See details to know which test will be used. Default is TRUE. |
| smd | A logical value specifying if the standardized differences should be estimating. Default is FALSE. |
| missing | A logical value specifying if the missing values should be describing. Default is TRUE. |
| paired | A logical value indicating whether you want paired test. Default is FALSE. |
| legend | A logical value indicating whether a footnote should be printed to indicate which tests were used. Default is TRUE. |
| binary01 | A logical value indicating if binary variables are expressed as 0/1. Default is FALSE. |
This function returns a flextable object. If the continuous variable is normally distributed, mean and standard deviation will be presented and groups compared using a Student t-test. Else, median and 25th-75th percentile will be presented and groups compared using a Mann-Whitney test. If nonnormal = NULL, a Shapiro test will be performed to qualify the distribution. Categorical variable will be presented as effectives and percentage and compared according to the group using a Chi-2 test or Fisher exact tests if expected numbers of patients are less than 5.
#Install the required packages
installed.packages(flextable)
library(flextable)
installed.packages(officer)
library(officer)
source("https://raw.githubusercontent.com/chupverse/descriptive-tools/main/tableDESC.R")
data("mtcars")
mtcars$am <- factor(mtcars$am, levels=c(0,1), labels = c("Automatic","Manual"))
mtcars$cyl <- factor(mtcars$cyl, c(4,6,8))
vars <- c("mpg","cyl","hp","vs")
labels <- c("Miles/(US) gallon", "Number of cylinders", "Gross horsepower", "V/S")
nolevels <- c("vs")
tableDESC(data=mtcars, vars=vars, labels=labels, trt="am", digits=c(1,0), nolevels=nolevels, nonnormal = "none", smd = TRUE, export = FALSE, binary01 = TRUE)Simple version:
tableDESC(data=mtcars, trt="am")