Package 'DemographicTable'

Title: Creating Demographic Table
Description: Functions for creating demographic table with simple summary statistics, with optional comparison(s) over one or more groups. Numeric variables are summarized in means, standard deviations, medians, inter-quartile-ranges (IQR), skewness, Shapiro-Wilk normality test and ranges, and compared using two-sample t-test, Wilcoxon test, ANOVA and/or Kruskal-Wallis test. Logical and factor variables are summarized in counts and percentages and compared using chi-squared test and/or Fisher's exact test.
Authors: Tingting Zhan [aut, cre, cph]
Maintainer: Tingting Zhan <[email protected]>
License: GPL-2
Version: 0.1.9
Built: 2024-11-17 03:39:49 UTC
Source: https://github.com/cran/DemographicTable

Help Index


Create Demographic Table

Description

Functions for creating demographic table with simple summary statistics, with optional comparison(s) over one or more groups. Numeric variables are summarized in means, standard deviations, medians, inter-quartile-ranges (IQR), skewness, Shapiro-Wilk normality test and ranges, and compared using two-sample tt-test, Wilcoxon test, ANOVA and/or Kruskal-Wallis test. Logical and factor variables are summarized in counts and percentages and compared using chi-squared test and/or Fisher's exact test.

Author(s)

Maintainer: Tingting Zhan [email protected] (ORCID) [copyright holder]


Convert DemographicTable to flextable

Description

Convert a DemographicTable to flextable object.

Usage

## S3 method for class 'DemographicTable'
as_flextable(x, ...)

Arguments

x

a DemographicTable object

...

potential additional parameters, not currently in use

Value

Function as_flextable.DemographicTable returns a flextable object.

Note

End user may use set_caption to add a caption to the output demographic table.


Create Demographic Table

Description

Create a demographic table with simple summary statistics, with optional comparison(s) over one or more groups.

Usage

DemographicTable(data, ...)

## S3 method for class 'data.frame'
DemographicTable(
  data,
  data.name = substitute(data),
  groups = NULL,
  keep_missing_group = TRUE,
  exclude = NULL,
  exclude_pattern,
  include,
  include_pattern,
  paired = FALSE,
  robust = TRUE,
  overall = TRUE,
  compare = TRUE,
  pairwise = 3L,
  ...
)

Arguments

data

a data.frame

...

additional parameters, currently not in use

data.name

character scalar, or the argument call of data. A user-friendly name of the input data.

groups

character scalar or vector, the name(s) of sub-group(s) for which the summary statistics are to be provided. Default NULL indicating no sub-groups.

keep_missing_group

logical scalar. If TRUE (default), the subjects with missing group are put into a new group ('.missing'). if FALSE, these subjects are removed from group-wise summary statistics.

exclude

character vector, the name(s) of variable(s) to be excluded. Default NULL indicating no variable are to be excluded.

exclude_pattern

(optional) character scalar as regex (regular expression), the pattern of the names of the variable(s) to be excluded.

include

character vector, the name(s) of variable(s) to be included. Default names(data) indicating all variables are to be included.

include_pattern

(optional) character scalar as regex (regular expression), the pattern of the names of the variable(s) to be included.

paired

logical scalar, whether to perform paired test (default FALSE)

robust

logical scalar. If TRUE (default), use non-parametric methods for non-normally distributed numeric variables.

overall

logical scalar. If TRUE (default), a column of overall summary statistics will be provided.

compare

logical scalar. If TRUE (default), comparisons between group(s) will be made.

pairwise

integer scalar, minimum number of groups where pairwise comparisons need to be performed. Default 3L.

Details

A demographic table with simple summary statistics, with optional comparison(s) over one or more groups, is created.

numeric variables are summarized in means, standard deviations, medians, inter-quartile-ranges (IQR), skewness, pp-value of Shapiro-Wilk normality test and ranges. If group is specified, they are compared using two-sample t.test, wilcox.test (Wilcoxon / Mann-Whitney), one-way aov (ANOVA) and/or kruskal.test (Kruskal-Wallis).

logical and factor variables are summarized in counts and percentages. If group is specified, they are compared using prop.test (chi-squared) and/or fisher.test (Fisher's exact).

Value

Function DemographicTable returns an object of S3 class 'DemographicTable', which inherits from matrix.

Examples

DemographicTable(esoph)
DemographicTable(ToothGrowth, groups = 'supp', include = 'len')
DemographicTable(ToothGrowth, groups = 'supp', include = 'len', paired = TRUE)
DemographicTable(ToothGrowth, groups = 'supp', include = 'len', compare = FALSE)
DemographicTable(warpbreaks, groups = c('wool', 'tension'))
DemographicTable(mtcars, groups = c('vs', 'am'), include = c('mpg', 'cyl', 'disp'))

# with missing value
DemographicTable(airquality, groups = 'Month', exclude = 'Day')
DemographicTable(MASS::survey, groups = 'Smoke', keep_missing_group = FALSE)
DemographicTable(MASS::survey, groups = 'Smoke', keep_missing_group = FALSE, useNA = 'always')

# write to Word file
library(flextable)
library(officer)
x = read_docx() |> body_add_flextable(value = as_flextable(DemographicTable(esoph)))
(out = file.path(tempdir(), 'demotable.docx'))
print(x, target = out)
# system(paste('open', out)) # works on Mac & Windows, but requires Microsoft Word
file.remove(out)

pp-value from modified Shapiro-Wilk Normality Test

Description

Obtain pp-value from shapiro.test, taking into consideration of several exceptions.

Usage

pval_shapiro(x, CLT = FALSE, ...)

Arguments

x

double vector

CLT

logical scalar, whether to allow the use of Central Limit Theorem, default FALSE

...

additional parameters, currently not in use

Details

Function pval_shapiro provides a pseudo pp-value for several exceptions of shapiro.test, serving as a criteria of whether robust statistics/tests need to be used

length(x) < 3L

returns p=0p=0, indicating that robust methods are needed.

length(x) > 5e3L

returns p=1p=1, indicating that no robust method is needed. For such large sample size, robust methods could be too slow.

CLT & length(x) > 30L

returns p=1p=1, indicating that no robust method is needed because of the use of Central Limit Theorem.

all x values are identical (or all.equal, to be implemented in future release)

returns p=0p=0, indicating that robust methods are needed.

Otherwise

use the pp-value from shapiro.test.

Value

Function pval_shapiro returns a double scalar.

Examples

pval_shapiro(rnorm(5))
sapply(with(airquality, split(Ozone, f = Month)), FUN = pval_shapiro)

Create R Markdown Script for DemographicTable

Description

Method dispatch to DemographicTable for S3 generic rmd_ (in a different master package).

Usage

rmd_.DemographicTable(x, xnm, type, ...)

Arguments

x

a DemographicTable

xnm

language or character scalar, call of x

type

..

...

additional parameters, currently not in use

Value

Function rmd_.DemographicTable returns a character vector.


Short Paragraph to Describe a DemographicTable

Description

To create a short paragraph to describe a DemographicTable

Usage

Sprintf.DemographicTable(model, ...)

Arguments

model

a DemographicTable

...

additional parameters, currently not in use

Value

Function Sprintf.DemographicTable returns a noquote character scalar.

Examples

Sprintf.DemographicTable(DemographicTable(esoph))
Sprintf.DemographicTable(DemographicTable(ToothGrowth, groups = 'supp', include = 'len'))

Summary Text

Description

Provide the summary text of an R object

Usage

summaryText(x, fmt, ...)

## Default S3 method:
summaryText(x, fmt = "%.2f", ...)

## S3 method for class 'factor'
summaryText(x, fmt = "%.1f", useNA = c("no", "always"), ...)

## S3 method for class 'ordered'
summaryText(x, fmt = "%.1f", useNA = c("no", "always"), ...)

## S3 method for class 'character'
summaryText(x, ...)

## S3 method for class 'logical'
summaryText(x, fmt = "%.1f", ...)

Arguments

x

an R object

fmt

character scalar, format string, see sprintf

...

additional parameters, currently not in use

useNA

character scalar, 'no' (default) or 'always', see table

Value

Function summaryText returns a character scalar.

Examples

x = rpois(n = 20L, lambda = 2)
x[sample.int(length(x), 3L)] = NA_integer_
summaryText(x)

# factor 
x = state.region
x[2L] = NA_integer_
summaryText(x)

# binary
summaryText(c(TRUE, FALSE, TRUE, NA))
summaryText(c(TRUE, FALSE, TRUE))
summaryText(c(FALSE, FALSE, NA))
summaryText(c(FALSE, FALSE, FALSE))
summaryText(c(NA, NA, NA))

Write DemographicTable to LaTeX

Description

Write DemographicTable to LaTeX.

Usage

## S3 method for class 'DemographicTable'
xtable(x, ...)

Arguments

x

a DemographicTable object

...

additional parameters of xtable

Value

Function xtable.DemographicTable returns an xtable object.

Examples

(tb = DemographicTable(ToothGrowth, groups = 'supp'))
library(xtable)
print(xtable(tb), sanitize.text.function = identity, 
 sanitize.colnames.function = NULL, include.rownames = FALSE)