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 |
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 -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.
Maintainer: Tingting Zhan [email protected] (ORCID) [copyright holder]
Convert a DemographicTable to flextable object.
## S3 method for class 'DemographicTable' as_flextable(x, ...)
## S3 method for class 'DemographicTable' as_flextable(x, ...)
x |
a DemographicTable object |
... |
potential additional parameters, not currently in use |
Function as_flextable.DemographicTable returns a flextable object.
End user may use set_caption to add a caption to the output demographic table.
Create a demographic table with simple summary statistics, with optional comparison(s) over one or more groups.
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, ... )
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, ... )
data |
|
... |
additional parameters, currently not in use |
data.name |
character scalar, or the argument call of |
groups |
character scalar or vector,
the name(s) of sub-group(s) for which the summary statistics are to be provided.
Default |
keep_missing_group |
logical scalar.
If |
exclude |
character vector,
the name(s) of variable(s) to be excluded.
Default |
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 |
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 |
robust |
logical scalar.
If |
overall |
logical scalar.
If |
compare |
logical scalar.
If |
pairwise |
integer scalar,
minimum number of groups where pairwise comparisons need to be performed.
Default |
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, -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).
Function DemographicTable returns an object of S3 class 'DemographicTable'
,
which inherits from matrix.
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)
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)
-value from modified Shapiro-Wilk Normality TestObtain -value from shapiro.test,
taking into consideration of several exceptions.
pval_shapiro(x, CLT = FALSE, ...)
pval_shapiro(x, CLT = FALSE, ...)
x |
|
CLT |
logical scalar, whether to allow the use of Central Limit Theorem, default |
... |
additional parameters, currently not in use |
Function pval_shapiro provides a pseudo -value for several exceptions of
shapiro.test, serving as a criteria of whether robust statistics/tests need to be used
length(x) < 3L
returns , indicating that robust methods are needed.
length(x) > 5e3L
returns , indicating that no robust method is needed. For such large sample size, robust methods could be too slow.
CLT & length(x) > 30L
returns , indicating that no robust method is needed because of the use of Central Limit Theorem.
x
values are identical (or all.equal, to be implemented in future release)returns , indicating that robust methods are needed.
use the -value from shapiro.test.
Function pval_shapiro returns a double scalar.
pval_shapiro(rnorm(5)) sapply(with(airquality, split(Ozone, f = Month)), FUN = pval_shapiro)
pval_shapiro(rnorm(5)) sapply(with(airquality, split(Ozone, f = Month)), FUN = pval_shapiro)
Method dispatch to DemographicTable for S3 generic rmd_
(in a different master package).
rmd_.DemographicTable(x, xnm, type, ...)
rmd_.DemographicTable(x, xnm, type, ...)
x |
|
xnm |
|
type |
.. |
... |
additional parameters, currently not in use |
Function rmd_.DemographicTable returns a character vector.
To create a short paragraph to describe a DemographicTable
Sprintf.DemographicTable(model, ...)
Sprintf.DemographicTable(model, ...)
model |
|
... |
additional parameters, currently not in use |
Function Sprintf.DemographicTable returns a noquote character scalar.
Sprintf.DemographicTable(DemographicTable(esoph)) Sprintf.DemographicTable(DemographicTable(ToothGrowth, groups = 'supp', include = 'len'))
Sprintf.DemographicTable(DemographicTable(esoph)) Sprintf.DemographicTable(DemographicTable(ToothGrowth, groups = 'supp', include = 'len'))
Provide the summary text of an R object
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", ...)
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", ...)
x |
an R object |
fmt |
|
... |
additional parameters, currently not in use |
useNA |
Function summaryText returns a character scalar.
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))
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.
## S3 method for class 'DemographicTable' xtable(x, ...)
## S3 method for class 'DemographicTable' xtable(x, ...)
x |
a DemographicTable object |
... |
additional parameters of xtable |
Function xtable.DemographicTable returns an xtable object.
(tb = DemographicTable(ToothGrowth, groups = 'supp')) library(xtable) print(xtable(tb), sanitize.text.function = identity, sanitize.colnames.function = NULL, include.rownames = FALSE)
(tb = DemographicTable(ToothGrowth, groups = 'supp')) library(xtable) print(xtable(tb), sanitize.text.function = identity, sanitize.colnames.function = NULL, include.rownames = FALSE)