Title: | Create Summary Tables for Statistical Reports |
---|---|
Description: | Contains functions for creating various types of summary tables, e.g. comparing characteristics across levels of a categorical variable and summarizing fitted generalized linear models, generalized estimating equations, and Cox proportional hazards models. Functions are available to handle data from simple random samples as well as complex surveys. |
Authors: | Dane R. Van Domelen |
Maintainer: | Dane R. Van Domelen <[email protected]> |
License: | GPL (>=3) |
Version: | 5.1.1 |
Built: | 2024-11-04 02:47:51 UTC |
Source: | https://github.com/vandomed/tab |
Formats p-values for tables generated by the functions in the tab package. Handles rounding and presentation of p-values.
formatp( p, decimals = c(2, 3), cuts = 0.01, lowerbound = 0.001, leading0 = TRUE, avoid1 = FALSE )
formatp( p, decimals = c(2, 3), cuts = 0.01, lowerbound = 0.001, leading0 = TRUE, avoid1 = FALSE )
p |
Numeric vector of p-values. |
decimals |
Number of decimal places for p-values. If a vector is
provided rather than a single value, number of decimal places will depend on
what range the p-value lies in. See |
cuts |
Cut-point(s) to control number of decimal places used for
p-values. For example, by default |
lowerbound |
Controls cut-point at which p-values are no longer printed
as their value, but rather <lowerbound. For example, by default
|
leading0 |
If |
avoid1 |
If |
Character vector.
# Generate vector of numeric p-values set.seed(123) p <- c(runif(n = 5, min = 0, max = 1), 1, 0, 4e-7, 0.009) # Round to nearest 2 decimals for p in (0.01, 1] and 3 decimals for p < 0.01 pvals <- formatp(p = p) # Use 2 decimal places, a lower bound of 0.01, and omit the leading 0 pvals <- formatp(p = p, decimals = 2, lowerbound = 0.01, leading0 = FALSE)
# Generate vector of numeric p-values set.seed(123) p <- c(runif(n = 5, min = 0, max = 1), 1, 0, 4e-7, 0.009) # Round to nearest 2 decimals for p in (0.01, 1] and 3 decimals for p < 0.01 pvals <- formatp(p = p) # Use 2 decimal places, a lower bound of 0.01, and omit the leading 0 pvals <- formatp(p = p, decimals = 2, lowerbound = 0.01, leading0 = FALSE)
You can call this function as you would glm
or pass a
previously fitted glm
object. Either way, the result is
a summary table printed to the Viewer.
glm_v(...)
glm_v(...)
... |
Arguments to pass to glm. |
kable
# Fit and view glm_v(death_1yr ~ Age + Sex + Race, data = tabdata, family = "binomial") # Fit then view fit <- glm(death_1yr ~ Age + Sex + Race, data = tabdata, family = "binomial") glm_v(fit) # Piping is OMG so cool Hashtag HexStickerz fit %>% glm_v()
# Fit and view glm_v(death_1yr ~ Age + Sex + Race, data = tabdata, family = "binomial") # Fit then view fit <- glm(death_1yr ~ Age + Sex + Race, data = tabdata, family = "binomial") glm_v(fit) # Piping is OMG so cool Hashtag HexStickerz fit %>% glm_v()
Contains functions for creating various types of summary tables, e.g. comparing characteristics across levels of a categorical variable and summarizing fitted generalized linear models, generalized estimating equations, and Cox proportional hazards models. Functions are available to handle data from simple random samples as well as complex surveys.
Package: | tab |
Type: | Package |
Version: | 5.1.1 |
Date: | 2021-08-01 |
License: | GPL-3 |
See CRAN documentation for full list of functions.
Dane R. Van Domelen
[email protected]
Acknowledgment: This material is based upon work supported by the National Science Foundation Graduate Research Fellowship under Grant No. DGE-0940903.
Creates a table summarizing a GEE fit using the coxph
function.
tabcoxph( fit, columns = c("beta.se", "hr.ci", "p"), var.labels = NULL, factor.compression = 1, sep.char = ", ", decimals = 2, formatp.list = NULL )
tabcoxph( fit, columns = c("beta.se", "hr.ci", "p"), var.labels = NULL, factor.compression = 1, sep.char = ", ", decimals = 2, formatp.list = NULL )
fit |
Fitted |
columns |
Character vector specifying what columns to include. Choies
for each element are |
var.labels |
Named list specifying labels to use for certain predictors.
For example, if |
factor.compression |
Integer value from 1 to 5 controlling how much compression is applied to factor predictors (higher value = more compression). If 1, rows are Variable, Level 1 (ref), Level 2, ...; if 2, rows are Variable (ref = Level 1), Level 2, ...; if 3, rows are Level 1 (ref), Level 2, ...; if 4, rows are Level 2 (ref = Level 1), ...; if 5, rows are Level 2, ... |
sep.char |
Character string with separator to place between lower and
upper bound of confidence intervals. Typically |
decimals |
Numeric value specifying number of decimal places for numbers other than p-values. |
formatp.list |
List of arguments to pass to |
1. Therneau, T. (2015). A Package for Survival Analysis in S. R package version 2.38. https://cran.r-project.org/package=survival.
2. Therneau, T.M. and Grambsch, P.M. (2000). Modeling Survival Data: Extending the Cox Model. Springer, New York. ISBN 0-387-98784-3.
# Cox PH model with age, sex, race, and treatment library("survival") fit <- coxph( Surv(time = time, event = delta) ~ Age + Sex + Race + Group, data = tabdata ) tabcoxph(fit) # Can also use piping fit %>% tabcoxph() # Same as previous, but with custom labels for Age and Race and factors # displayed in slightly more compressed format fit %>% tabcoxph( var.labels = list(Age = "Age (years)", Race = "Race/ethnicity"), factor.compression = 2 ) # Cox PH model with some higher-order terms fit <- coxph( Surv(time = time, event = delta) ~ poly(Age, 2, raw = TRUE) + Sex + Race + Group + Race*Group, data = tabdata ) fit %>% tabcoxph()
# Cox PH model with age, sex, race, and treatment library("survival") fit <- coxph( Surv(time = time, event = delta) ~ Age + Sex + Race + Group, data = tabdata ) tabcoxph(fit) # Can also use piping fit %>% tabcoxph() # Same as previous, but with custom labels for Age and Race and factors # displayed in slightly more compressed format fit %>% tabcoxph( var.labels = list(Age = "Age (years)", Race = "Race/ethnicity"), factor.compression = 2 ) # Cox PH model with some higher-order terms fit <- coxph( Surv(time = time, event = delta) ~ poly(Age, 2, raw = TRUE) + Sex + Race + Group + Race*Group, data = tabdata ) fit %>% tabcoxph()
Data frame with 15 variables, used to illustrate certain functions.
Simulated data in R
Creates an I-by-J frequency table comparing the distribution of y
across levels of x
.
tabfreq( formula = NULL, data = NULL, x = NULL, y = NULL, columns = c("xgroups", "p"), cell = "counts", parenth = "col.percent", sep.char = ", ", test = "chi.fisher", xlevels = NULL, yname = NULL, ylevels = NULL, compress.binary = FALSE, yname.row = TRUE, text.label = NULL, quantiles = NULL, quantile.vals = FALSE, decimals = 1, formatp.list = NULL, n.headings = FALSE, kable = TRUE )
tabfreq( formula = NULL, data = NULL, x = NULL, y = NULL, columns = c("xgroups", "p"), cell = "counts", parenth = "col.percent", sep.char = ", ", test = "chi.fisher", xlevels = NULL, yname = NULL, ylevels = NULL, compress.binary = FALSE, yname.row = TRUE, text.label = NULL, quantiles = NULL, quantile.vals = FALSE, decimals = 1, formatp.list = NULL, n.headings = FALSE, kable = TRUE )
formula |
Formula, e.g. |
data |
Data frame containing variables named in |
x |
Vector indicating group membership for columns of I-by-J table. |
y |
Vector indicating group membership for rows of I-by-J table. |
columns |
Character vector specifying what columns to include. Choices
for each element are |
cell |
Character string specifying what statistic to display in cells.
Choices are |
parenth |
Character string specifying what statistic to display in
parentheses. Choices are |
sep.char |
Character string with separator to place between lower and
upper bound of confidence intervals. Typically |
test |
Character string specifying which test for association between
|
xlevels |
Character vector with labels for the levels of |
yname |
Character string with a label for the |
ylevels |
Character vector with labels for the levels of |
compress.binary |
Logical value for whether to compress binary |
yname.row |
Logical value for whether to include a row displaying the
name of the |
text.label |
Character string with text to put after the |
quantiles |
Numeric value. If specified, table compares |
quantile.vals |
Logical value for whether labels for |
decimals |
Numeric value specifying number of decimal places for numbers other than p-values. |
formatp.list |
List of arguments to pass to |
n.headings |
Logical value for whether to display group sample sizes in parentheses in column headings. |
kable |
Logical value for whether to return a
|
# Compare sex distribution by group (freqtable1 <- tabfreq(Sex ~ Group, data = tabdata)) # Same as previous, but showing male row only and % (SE) rather than n (%) (freqtable2 <- tabfreq(Sex ~ Group, data = tabdata, cell = "col.percent", parenth = "se", compress.binary = TRUE))
# Compare sex distribution by group (freqtable1 <- tabfreq(Sex ~ Group, data = tabdata)) # Same as previous, but showing male row only and % (SE) rather than n (%) (freqtable2 <- tabfreq(Sex ~ Group, data = tabdata, cell = "col.percent", parenth = "se", compress.binary = TRUE))
Creates an I-by-J frequency table comparing the distribution of y
across levels of x
.
tabfreq.svy( formula, design, columns = c("xgroups", "p"), cell = "col.percent", parenth = "se", sep.char = ", ", xlevels = NULL, yname = NULL, ylevels = NULL, compress.binary = FALSE, yname.row = TRUE, text.label = NULL, decimals = 1, svychisq.list = NULL, formatp.list = NULL, n.headings = FALSE, N.headings = FALSE, kable = TRUE )
tabfreq.svy( formula, design, columns = c("xgroups", "p"), cell = "col.percent", parenth = "se", sep.char = ", ", xlevels = NULL, yname = NULL, ylevels = NULL, compress.binary = FALSE, yname.row = TRUE, text.label = NULL, decimals = 1, svychisq.list = NULL, formatp.list = NULL, n.headings = FALSE, N.headings = FALSE, kable = TRUE )
formula |
Formula, e.g. |
design |
Survey design object from |
columns |
Character vector specifying what columns to include. Choices
for each element are |
cell |
Character string specifying what statistic to display in cells.
Choices are |
parenth |
Character string specifying what statistic to display in
parentheses. Choices are |
sep.char |
Character string with separator to place between lower and
upper bound of confidence intervals. Typically |
xlevels |
Character vector with labels for the levels of |
yname |
Character string with a label for the |
ylevels |
Character vector with labels for the levels of |
compress.binary |
Logical value for whether to compress binary |
yname.row |
Logical value for whether to include a row displaying the
name of the |
text.label |
Character string with text to put after the |
decimals |
Numeric value specifying number of decimal places for numbers other than p-values. |
svychisq.list |
List of arguments to pass to
|
formatp.list |
List of arguments to pass to |
n.headings |
Logical value for whether to display unweighted sample sizes in parentheses in column headings. |
N.headings |
Logical value for whether to display weighted sample sizes in parentheses in column headings. |
kable |
Logical value for whether to return a
|
Basically tabmedians
for complex survey data. Relies heavily on
the survey package.
kable
or character matrix.
# Create survey design object library("survey") design <- svydesign( data = tabsvydata, ids = ~sdmvpsu, strata = ~sdmvstra, weights = ~wtmec2yr, nest = TRUE ) # Compare race distribution by sex tabfreq.svy(Race ~ Sex, design)
# Create survey design object library("survey") design <- svydesign( data = tabsvydata, ids = ~sdmvpsu, strata = ~sdmvstra, weights = ~wtmec2yr, nest = TRUE ) # Compare race distribution by sex tabfreq.svy(Race ~ Sex, design)
Creates a table summarizing a GEE fit using the gee
function.
tabgee( fit, data = NULL, columns = NULL, robust = TRUE, var.labels = NULL, factor.compression = 1, sep.char = ", ", decimals = 2, formatp.list = NULL )
tabgee( fit, data = NULL, columns = NULL, robust = TRUE, var.labels = NULL, factor.compression = 1, sep.char = ", ", decimals = 2, formatp.list = NULL )
fit |
Fitted |
data |
Data frame that served as 'data' in function call to
|
columns |
Character vector specifying what columns to include. Choices
for each element are |
robust |
Logical value for whether to use robust standard errors. |
var.labels |
Named list specifying labels to use for certain predictors.
For example, if |
factor.compression |
Integer value from 1 to 5 controlling how much compression is applied to factor predictors (higher value = more compression). If 1, rows are Variable, Level 1 (ref), Level 2, ...; if 2, rows are Variable (ref = Level 1), Level 2, ...; if 3, rows are Level 1 (ref), Level 2, ...; if 4, rows are Level 2 (ref = Level 1), ...; if 5, rows are Level 2, ... |
sep.char |
Character string with separator to place between lower and
upper bound of confidence intervals. Typically |
decimals |
Numeric value specifying number of decimal places for numbers other than p-values. |
formatp.list |
List of arguments to pass to |
# Load in sample dataset and convert to long format tabdata2 <- reshape(data = tabdata, varying = c("bp.1", "bp.2", "bp.3", "highbp.1", "highbp.2", "highbp.3"), timevar = "bp.visit", direction = "long") tabdata2 <- tabdata2[order(tabdata2$id), ] # Blood pressure at 1, 2, and 3 months vs. age, sex, race, and treatment library("gee") fit <- gee(bp ~ Age + Sex + Race + Group, id = id, data = tabdata2, corstr = "unstructured") tabgee(fit) # Can also use piping fit %>% tabgee(data = tabdata2) # Same as previous, but with custom labels for Age and Race and factors # displayed in slightly more compressed format fit %>% tabgee( data = tabdata2, var.labels = list(Age = "Age (years)", Race = "Race/ethnicity"), factor.compression = 2 ) # GEE with some higher-order terms # higher-order terms fit <- gee( highbp ~ poly(Age, 2, raw = TRUE) + Sex + Race + Group + Race*Group, id = id, data = tabdata2, family = "binomial", corstr = "unstructured" ) fit %>% tabgee(data = tabdata2)
# Load in sample dataset and convert to long format tabdata2 <- reshape(data = tabdata, varying = c("bp.1", "bp.2", "bp.3", "highbp.1", "highbp.2", "highbp.3"), timevar = "bp.visit", direction = "long") tabdata2 <- tabdata2[order(tabdata2$id), ] # Blood pressure at 1, 2, and 3 months vs. age, sex, race, and treatment library("gee") fit <- gee(bp ~ Age + Sex + Race + Group, id = id, data = tabdata2, corstr = "unstructured") tabgee(fit) # Can also use piping fit %>% tabgee(data = tabdata2) # Same as previous, but with custom labels for Age and Race and factors # displayed in slightly more compressed format fit %>% tabgee( data = tabdata2, var.labels = list(Age = "Age (years)", Race = "Race/ethnicity"), factor.compression = 2 ) # GEE with some higher-order terms # higher-order terms fit <- gee( highbp ~ poly(Age, 2, raw = TRUE) + Sex + Race + Group + Race*Group, id = id, data = tabdata2, family = "binomial", corstr = "unstructured" ) fit %>% tabgee(data = tabdata2)
Creates a table summarizing a GLM fit using glm
.
tabglm( fit, columns = NULL, xvarlabels = NULL, factor.compression = 1, sep.char = ", ", decimals = 2, formatp.list = NULL )
tabglm( fit, columns = NULL, xvarlabels = NULL, factor.compression = 1, sep.char = ", ", decimals = 2, formatp.list = NULL )
fit |
Fitted |
columns |
Character vector specifying what columns to include. Choices
for each element are |
xvarlabels |
Named list specifying labels to use for certain predictors.
For example, if |
factor.compression |
Integer value from 1 to 5 controlling how much compression is applied to factor predictors (higher value = more compression). If 1, rows are Variable, Level 1 (ref), Level 2, ...; if 2, rows are Variable (ref = Level 1), Level 2, ...; if 3, rows are Level 1 (ref), Level 2, ...; if 4, rows are Level 2 (ref = Level 1), ...; if 5, rows are Level 2, ... |
sep.char |
Character string with separator to place between lower and
upper bound of confidence intervals. Typically |
decimals |
Numeric value specifying number of decimal places for numbers other than p-values. |
formatp.list |
List of arguments to pass to |
# Linear regression: BMI vs. age, sex, race, and treatment fit <- glm(BMI ~ Age + Sex + Race + Group, data = tabdata) tabglm(fit) # Can also use piping fit %>% tabglm() # Logistic regression: 1-year mortality vs. age, sex, race, and treatment fit <- glm( death_1yr ~ Age + Sex + Race + Group, data = tabdata, family = binomial ) fit %>% tabglm() # Same as previous, but with custom labels for Age and Race and factors # displayed in slightly more compressed format fit %>% tabglm( xvarlabels = list(Age = "Age (years)", Race = "Race/ethnicity"), factor.compression = 2 ) # Logistic regression model with some higher-order terms fit <- glm( death_1yr ~ poly(Age, 2, raw = TRUE) + Sex + BMI + Sex * BMI, data = tabdata, family = "binomial" ) fit %>% tabglm()
# Linear regression: BMI vs. age, sex, race, and treatment fit <- glm(BMI ~ Age + Sex + Race + Group, data = tabdata) tabglm(fit) # Can also use piping fit %>% tabglm() # Logistic regression: 1-year mortality vs. age, sex, race, and treatment fit <- glm( death_1yr ~ Age + Sex + Race + Group, data = tabdata, family = binomial ) fit %>% tabglm() # Same as previous, but with custom labels for Age and Race and factors # displayed in slightly more compressed format fit %>% tabglm( xvarlabels = list(Age = "Age (years)", Race = "Race/ethnicity"), factor.compression = 2 ) # Logistic regression model with some higher-order terms fit <- glm( death_1yr ~ poly(Age, 2, raw = TRUE) + Sex + BMI + Sex * BMI, data = tabdata, family = "binomial" ) fit %>% tabglm()
Creates a table comparing the mean of y
across levels of x
.
tabmeans( formula = NULL, data = NULL, x = NULL, y = NULL, columns = c("xgroups", "p"), parenth = "sd", sep.char = ", ", variance = "unequal", xlevels = NULL, yname = NULL, text.label = NULL, quantiles = NULL, quantile.vals = FALSE, decimals = NULL, formatp.list = NULL, n.headings = TRUE, kable = TRUE )
tabmeans( formula = NULL, data = NULL, x = NULL, y = NULL, columns = c("xgroups", "p"), parenth = "sd", sep.char = ", ", variance = "unequal", xlevels = NULL, yname = NULL, text.label = NULL, quantiles = NULL, quantile.vals = FALSE, decimals = NULL, formatp.list = NULL, n.headings = TRUE, kable = TRUE )
formula |
Formula, e.g. |
data |
Data frame containing variables named in |
x |
Vector of values for the categorical |
y |
Vector of values for the continuous |
columns |
Character vector specifying what columns to include. Choices
for each element are |
parenth |
Character string specifying what statistic to display in
parentheses after the means. Choices are |
sep.char |
Character string with separator to place between lower and
upper bound of confidence intervals. Typically |
variance |
Character string specifying which version of the two-sample
t-test to use if |
xlevels |
Character vector with labels for the levels of |
yname |
Character string with a label for the |
text.label |
Character string with text to put after the |
quantiles |
Numeric value. If specified, table compares |
quantile.vals |
Logical value for whether labels for |
decimals |
Numeric value specifying number of decimal places for numbers other than p-values. |
formatp.list |
List of arguments to pass to |
n.headings |
Logical value for whether to display group sample sizes in parentheses in column headings. |
kable |
Logical value for whether to return a
|
A t-test is used to compare means if x
has two levels, and a one-way
analysis of variance is used if x
has more than two levels.
Observations with missing values for x
and/or y
are dropped.
kable
or character matrix.
# Compare mean BMI in control vs. treatment group in sample dataset (meanstable1 <- tabmeans(BMI ~ Group, data = tabdata)) # Compare mean baseline systolic BP across tertiles of BMI (meanstable2 <- tabmeans(bp.1 ~ BMI, data = tabdata, quantiles = 3, yname = "Systolic BP"))
# Compare mean BMI in control vs. treatment group in sample dataset (meanstable1 <- tabmeans(BMI ~ Group, data = tabdata)) # Compare mean baseline systolic BP across tertiles of BMI (meanstable2 <- tabmeans(bp.1 ~ BMI, data = tabdata, quantiles = 3, yname = "Systolic BP"))
Creates a table comparing the mean of y
across levels of x
.
tabmeans.svy( formula, design, columns = c("xgroups", "p"), parenth = "sd", sep.char = ", ", xlevels = NULL, yname = NULL, text.label = NULL, decimals = 1, anova.svyglm.list = NULL, formatp.list = NULL, n.headings = FALSE, N.headings = FALSE, kable = TRUE )
tabmeans.svy( formula, design, columns = c("xgroups", "p"), parenth = "sd", sep.char = ", ", xlevels = NULL, yname = NULL, text.label = NULL, decimals = 1, anova.svyglm.list = NULL, formatp.list = NULL, n.headings = FALSE, N.headings = FALSE, kable = TRUE )
formula |
Formula, e.g. |
design |
Survey design object from |
columns |
Character vector specifying what columns to include. Choices
for each element are |
parenth |
Character string specifying what statistic to display in
parentheses after the means. Choices are |
sep.char |
Character string with separator to place between lower and
upper bound of confidence intervals. Typically |
xlevels |
Character vector with labels for the levels of |
yname |
Character string with a label for the |
text.label |
Character string with text to put after the |
decimals |
Numeric value specifying number of decimal places for numbers other than p-values. |
anova.svyglm.list |
List of arguments to pass to
|
formatp.list |
List of arguments to pass to |
n.headings |
Logical value for whether to display group sample sizes in parentheses in column headings. |
N.headings |
Logical value for whether to display weighted sample sizes in parentheses in column headings. |
kable |
Logical value for whether to return a
|
Basically tabmeans
for complex survey data. Relies heavily on
the survey package.
kable
or character matrix.
# Create survey design object library("survey") design <- svydesign( data = tabsvydata, ids = ~sdmvpsu, strata = ~sdmvstra, weights = ~wtmec2yr, nest = TRUE ) # Compare mean BMI by sex (meanstable <- tabmeans.svy(BMI ~ Sex, design = design))
# Create survey design object library("survey") design <- svydesign( data = tabsvydata, ids = ~sdmvpsu, strata = ~sdmvstra, weights = ~wtmec2yr, nest = TRUE ) # Compare mean BMI by sex (meanstable <- tabmeans.svy(BMI ~ Sex, design = design))
Creates a table comparing the median of y
across levels of x
.
tabmedians( formula = NULL, data = NULL, x = NULL, y = NULL, columns = c("xgroups", "p"), parenth = "iqr", sep.char = ", ", xlevels = NULL, yname = NULL, text.label = NULL, quantiles = NULL, quantile.vals = FALSE, decimals = NULL, formatp.list = NULL, n.headings = TRUE, kable = TRUE )
tabmedians( formula = NULL, data = NULL, x = NULL, y = NULL, columns = c("xgroups", "p"), parenth = "iqr", sep.char = ", ", xlevels = NULL, yname = NULL, text.label = NULL, quantiles = NULL, quantile.vals = FALSE, decimals = NULL, formatp.list = NULL, n.headings = TRUE, kable = TRUE )
formula |
Formula, e.g. |
data |
Data frame containing variables named in |
x |
Vector of values for the categorical |
y |
Vector of values for the continuous |
columns |
Character vector specifying what columns to include. Choices
for each element are |
parenth |
Character string specifying what values are shown in
parentheses after the medians in each cell. Choices are |
sep.char |
Character string with separator to place between lower and
upper bound of confidence intervals. Typically |
xlevels |
Character vector with labels for the levels of |
yname |
Character string with a label for the |
text.label |
Character string with text to put after the |
quantiles |
Numeric value. If specified, table compares |
quantile.vals |
Logical value for whether labels for |
decimals |
Numeric value specifying number of decimal places for numbers other than p-values. |
formatp.list |
List of arguments to pass to |
n.headings |
Logical value for whether to display group sample sizes in parentheses in column headings. |
kable |
Logical value for whether to return a
|
If x
has 2 levels, a Mann-Whitney U (also known as Wilcoxon
rank-sum) test is used to test whether the distribution of y
differs
in the two groups; if x
has more than 2 levels, a Kruskal-Wallis test
is used to test whether the distribution of y
differs across at
least two of the groups. Observations with missing values for x
and/or
y
are dropped.
# Compare median BMI in control group vs. treatment group in sample dataset (medtable1 <- tabmedians(BMI ~ Group, data = tabdata)) # Compare median baseline systolic BP across tertiles of BMI (medtable2 <- tabmedians(bp.1 ~ BMI, data = tabdata, quantiles = 3, yname = "Systolic BP"))
# Compare median BMI in control group vs. treatment group in sample dataset (medtable1 <- tabmedians(BMI ~ Group, data = tabdata)) # Compare median baseline systolic BP across tertiles of BMI (medtable2 <- tabmedians(bp.1 ~ BMI, data = tabdata, quantiles = 3, yname = "Systolic BP"))
Creates a table comparing the median of y
across levels of x
.
tabmedians.svy( formula, design, columns = c("xgroups", "p"), parenth = "iqr", sep.char = ", ", xlevels = NULL, yname = NULL, text.label = NULL, decimals = NULL, svyranktest.list = NULL, formatp.list = NULL, n.headings = FALSE, N.headings = FALSE, kable = TRUE )
tabmedians.svy( formula, design, columns = c("xgroups", "p"), parenth = "iqr", sep.char = ", ", xlevels = NULL, yname = NULL, text.label = NULL, decimals = NULL, svyranktest.list = NULL, formatp.list = NULL, n.headings = FALSE, N.headings = FALSE, kable = TRUE )
formula |
Formula, e.g. |
design |
Survey design object from |
columns |
Character vector specifying what columns to include. Choices
for each element are |
parenth |
Character string specifying what values are shown in
parentheses after the medians in each cell. Choices are |
sep.char |
Character string with separator to place between lower and
upper bound of confidence intervals. Typically |
xlevels |
Character vector with labels for the levels of |
yname |
Character string with a label for the |
text.label |
Character string with text to put after the |
decimals |
Numeric value specifying number of decimal places for numbers other than p-values. |
svyranktest.list |
List of arguments to pass to
|
formatp.list |
List of arguments to pass to |
n.headings |
Logical value for whether to display group sample sizes in parentheses in column headings. |
N.headings |
Logical value for whether to display weighted sample sizes in parentheses in column headings. |
kable |
Logical value for whether to return a
|
Basically tabmedians
for complex survey data. Relies heavily on
the survey package.
kable
or character matrix.
# Create survey design object library("survey") design <- svydesign( data = tabsvydata, ids = ~sdmvpsu, strata = ~sdmvstra, weights = ~wtmec2yr, nest = TRUE ) # Compare median BMI by sex (medtable1 <- tabmedians.svy(BMI ~ Sex, design = design))
# Create survey design object library("survey") design <- svydesign( data = tabsvydata, ids = ~sdmvpsu, strata = ~sdmvstra, weights = ~wtmec2yr, nest = TRUE ) # Compare median BMI by sex (medtable1 <- tabmedians.svy(BMI ~ Sex, design = design))
Creates a table comparing multiple characteristics (e.g. median age, mean
BMI, and race/ethnicity distribution) across levels of x
.
tabmulti( formula = NULL, data, xvarname = NULL, yvarnames = NULL, ymeasures = NULL, columns = c("xgroups", "p"), listwise.deletion = FALSE, sep.char = ", ", xlevels = NULL, yvarlabels = NULL, ylevels = NULL, quantiles = NULL, quantile.vals = FALSE, decimals = NULL, formatp.list = NULL, n.headings = FALSE, tabmeans.list = NULL, tabmedians.list = NULL, tabfreq.list = NULL, kable = TRUE )
tabmulti( formula = NULL, data, xvarname = NULL, yvarnames = NULL, ymeasures = NULL, columns = c("xgroups", "p"), listwise.deletion = FALSE, sep.char = ", ", xlevels = NULL, yvarlabels = NULL, ylevels = NULL, quantiles = NULL, quantile.vals = FALSE, decimals = NULL, formatp.list = NULL, n.headings = FALSE, tabmeans.list = NULL, tabmedians.list = NULL, tabfreq.list = NULL, kable = TRUE )
formula |
Formula, e.g. |
data |
Data frame containing variables named in |
xvarname |
Character string with name of column variable. Should be one
of |
yvarnames |
Character vector with names of row variables. Each element
should be one of |
ymeasures |
Character vector specifying whether each |
columns |
Character vector specifying what columns to include. Choices
for each element are |
listwise.deletion |
Logical value for whether observations with missing
values for any |
sep.char |
Character string with separator to place between lower and
upper bound of confidence intervals. Typically |
xlevels |
Character vector with labels for the levels of |
yvarlabels |
Named list specifying labels for certain |
ylevels |
Character vector (if only 1 frequency comparison) or list of
character vectors with labels for the levels of each categorical |
quantiles |
Numeric value. If specified, function compares |
quantile.vals |
Logical value for whether labels for |
decimals |
Numeric vector specifying number of decimal places for
numbers other than p-values for each |
formatp.list |
List of arguments to pass to |
n.headings |
Logical value for whether to display group sample sizes in parentheses in column headings. |
tabmeans.list |
List of arguments to pass to |
tabmedians.list |
List of arguments to pass to |
tabfreq.list |
List of arguments to pass to |
kable |
Logical value for whether to return a
|
kable
or character matrix.
# Compare age, sex, race, and BMI in control vs. treatment group tabmulti(Age + Sex + Race + BMI ~ Group, data = tabdata) # Same as previous, but compare medians rather than means for BMI tabmulti(Age + Sex + Race + BMI ~ Group, data = tabdata, ymeasures = c("mean", "freq", "freq", "median"))
# Compare age, sex, race, and BMI in control vs. treatment group tabmulti(Age + Sex + Race + BMI ~ Group, data = tabdata) # Same as previous, but compare medians rather than means for BMI tabmulti(Age + Sex + Race + BMI ~ Group, data = tabdata, ymeasures = c("mean", "freq", "freq", "median"))
Creates a table comparing multiple characteristics (e.g. median age, mean
BMI, and race/ethnicity distribution) across levels of x
.
tabmulti.svy( formula = NULL, design, xvarname = NULL, yvarnames = NULL, ymeasures = NULL, columns = c("xgroups", "p"), listwise.deletion = FALSE, sep.char = ", ", xlevels = NULL, yvarlabels = NULL, ylevels = NULL, decimals = NULL, formatp.list = NULL, n.headings = FALSE, N.headings = FALSE, kable = TRUE, tabmeans.svy.list = NULL, tabmedians.svy.list = NULL, tabfreq.svy.list = NULL )
tabmulti.svy( formula = NULL, design, xvarname = NULL, yvarnames = NULL, ymeasures = NULL, columns = c("xgroups", "p"), listwise.deletion = FALSE, sep.char = ", ", xlevels = NULL, yvarlabels = NULL, ylevels = NULL, decimals = NULL, formatp.list = NULL, n.headings = FALSE, N.headings = FALSE, kable = TRUE, tabmeans.svy.list = NULL, tabmedians.svy.list = NULL, tabfreq.svy.list = NULL )
formula |
Formula, e.g. |
design |
Survey design object from |
xvarname |
Character string with name of column variable. Should be one
of |
yvarnames |
Character vector with names of row variables. Each element
should be one of |
ymeasures |
Character vector specifying whether each |
columns |
Character vector specifying what columns to include. Choices
for each element are |
listwise.deletion |
Logical value for whether observations with missing
values for any |
sep.char |
Character string with separator to place between lower and
upper bound of confidence intervals. Typically |
xlevels |
Character vector with labels for the levels of |
yvarlabels |
Named list specifying labels for certain |
ylevels |
Character vector (if only 1 frequency comparison) or list of
character vectors with labels for the levels of each categorical |
decimals |
Numeric vector specifying number of decimal places for
numbers other than p-values for each |
formatp.list |
List of arguments to pass to |
n.headings |
Logical value for whether to display unweighted sample sizes in parentheses in column headings. |
N.headings |
Logical value for whether to display weighted sample sizes in parentheses in column headings. |
kable |
Logical value for whether to return a
|
tabmeans.svy.list |
List of arguments to pass to
|
tabmedians.svy.list |
List of arguments to pass to
|
tabfreq.svy.list |
List of arguments to pass to
|
Basically tabmulti
for complex survey data. Relies heavily on
the survey package.
kable
or character matrix.
# Create survey design object library("survey") design <- svydesign( data = tabsvydata, ids = ~sdmvpsu, strata = ~sdmvstra, weights = ~wtmec2yr, nest = TRUE ) # Compare age, race, and BMI by sex tabmulti.svy(Age + Race + BMI ~ Sex, design)
# Create survey design object library("survey") design <- svydesign( data = tabsvydata, ids = ~sdmvpsu, strata = ~sdmvstra, weights = ~wtmec2yr, nest = TRUE ) # Compare age, race, and BMI by sex tabmulti.svy(Age + Race + BMI ~ Sex, design)
Useful for quickly creating a summary table.
tabreg( betas, ses = NULL, varcov = NULL, columns = c("beta.se", "p"), sep.char = ", ", decimals = NULL, formatp.list = NULL, labels = NULL )
tabreg( betas, ses = NULL, varcov = NULL, columns = c("beta.se", "p"), sep.char = ", ", decimals = NULL, formatp.list = NULL, labels = NULL )
betas |
Numeric vector. |
ses |
Numeric vector. |
varcov |
Numeric matrix. |
columns |
Character vector specifying what columns to include. Choices
are |
sep.char |
Character string with separator to place between lower and
upper bound of confidence intervals. Typically |
decimals |
Numeric value specifying number of decimal places for numbers other than p-values. |
formatp.list |
List of arguments to pass to |
labels |
Character vector. |
# Create summary table for mtcars regression fit <- lm(mpg ~ wt + hp + drat, data = mtcars) tabreg( betas = fit$coef, varcov = vcov(fit), labels = c("Intercept", "Weight", "HP", "Rear axle ratio") )
# Create summary table for mtcars regression fit <- lm(mpg ~ wt + hp + drat, data = mtcars) tabreg( betas = fit$coef, varcov = vcov(fit), labels = c("Intercept", "Weight", "HP", "Rear axle ratio") )
Data frame with with 9 variables, used to illustrate certain functions. Data are derived from the National Health and Nutrition Examination Survey, years 2003-2004, although the variables 'time' and 'event' are simulated (fake).
https://wwwn.cdc.gov/Nchs/Nhanes/2003-2004/DEMO_C.htm
Centers for Disease Control and Prevention (CDC). National Center for Health Statistics (NCHS). National Health and Nutrition Examination Survey Data. Hyattsville, MD: US Department of Health and Human Services, Centers for Disease Control and Prevention, 2003-2004. https://wwwn.cdc.gov/nchs/nhanes/continuousnhanes/default.aspx?BeginYear=2003. Accessed June 8, 2019.
Does some basic formatting and then calls kable
and
kable_styling
to print table to Viewer.
toviewer(x)
toviewer(x)
x |
Character matrix |