Movatterモバイル変換


[0]ホーム

URL:


Type:Package
Title:Create Summary Tables for Statistical Reports
Version:5.1.1
Date:2021-08-01
Author:Dane R. Van Domelen
Maintainer:Dane R. Van Domelen <vandomed@gmail.com>
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.
License:GPL (≥ 3)
LazyData:true
Encoding:UTF-8
Depends:dplyr, knitr
Imports:kableExtra, MASS, stats, survey (≥ 4.1)
RoxygenNote:7.1.1
Suggests:gee, rmarkdown, survival
VignetteBuilder:knitr
NeedsCompilation:no
Packaged:2021-08-02 00:29:41 UTC; vando
Repository:CRAN
Date/Publication:2021-08-02 04:30:02 UTC

Format P-values for Functions in thetab Package

Description

Formats p-values for tables generated by the functions in thetabpackage. Handles rounding and presentation of p-values.

Usage

formatp(  p,  decimals = c(2, 3),  cuts = 0.01,  lowerbound = 0.001,  leading0 = TRUE,  avoid1 = FALSE)

Arguments

p

Numeric vector of p-values.

decimals

Number of decimal places for p-values. If a vector isprovided rather than a single value, number of decimal places will depend onwhat range the p-value lies in. Seecuts input.

cuts

Cut-point(s) to control number of decimal places used forp-values. For example, by defaultcuts = 0.1 anddecimals = c(2, 3). This means that p-values in the range [0.1, 1]will be printed to two decimal places, while p-values in the range [0, 0.1)will be printed to three decimal places.

lowerbound

Controls cut-point at which p-values are no longer printedas their value, but rather <lowerbound. For example, by defaultlowerbound = 0.001. Under this setting, p-values less than 0.001 areprinted as<0.001.

leading0

IfTRUE, p-values are printed with 0 before decimalplace; ifFALSE, the leading 0 is omitted.

avoid1

IfTRUE, p-values rounded to 1 are not printed as 1, butas>0.99 (or similarly depending ondecimals andcuts).

Value

Character vector.

Examples

# Generate vector of numeric p-valuesset.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.01pvals <- formatp(p = p)# Use 2 decimal places, a lower bound of 0.01, and omit the leading 0pvals <- formatp(p = p, decimals = 2, lowerbound = 0.01, leading0 = FALSE)

Print a GLM Summary Table to the RStudio Viewer

Description

You can call this function as you wouldglm or pass apreviously fittedglm object. Either way, the result isa summary table printed to the Viewer.

Usage

glm_v(...)

Arguments

...

Arguments to pass to glm.

Value

kable

Examples

# Fit and viewglm_v(death_1yr ~ Age + Sex + Race, data = tabdata, family = "binomial")# Fit then viewfit <- glm(death_1yr ~ Age + Sex + Race, data = tabdata, family = "binomial")glm_v(fit)# Piping is OMG so cool Hashtag HexStickerzfit %>% glm_v()

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 andsummarizing fitted generalized linear models, generalized estimatingequations, and Cox proportional hazards models. Functions are available tohandle data from simple random samples as well as complex surveys.

Details

Package: tab
Type: Package
Version: 5.1.1
Date: 2021-08-01
License: GPL-3

SeeCRAN documentation forfull list of functions.

Author(s)

Dane R. Van Domelen
vandomed@gmail.com

References

Acknowledgment: This material is based upon work supported by theNational Science Foundation Graduate Research Fellowship under Grant No.DGE-0940903.


Create Summary Table for Fitted Cox Proportional Hazards Model

Description

Creates a table summarizing a GEE fit using thecoxphfunction.

Usage

tabcoxph(  fit,  columns = c("beta.se", "hr.ci", "p"),  var.labels = NULL,  factor.compression = 1,  sep.char = ", ",  decimals = 2,  formatp.list = NULL)

Arguments

fit

Fittedcoxph object.

columns

Character vector specifying what columns to include. Choiesfor each element are"events","beta","se","beta.se","beta.betaci","betaci","hr","hr.hrci","hrci","z", and"p".

var.labels

Named list specifying labels to use for certain predictors.For example, iffit includes a predictor named "race"that you want to label "Race/ethnicity" and a predictor named "age_yrs" thatyou want to label "Age (years)", usevar.labels = list(race = "Race/ethnicity", age_yrs = "Age (years)".

factor.compression

Integer value from 1 to 5 controlling how muchcompression is applied to factor predictors (higher value = morecompression). 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, rowsare Level 2, ...

sep.char

Character string with separator to place between lower andupper bound of confidence intervals. Typically"-" or", ".

decimals

Numeric value specifying number of decimal places for numbersother than p-values.

formatp.list

List of arguments to pass toformatp.

Value

kable.

References

1. Therneau, T. (2015). A Package for Survival Analysis in S. R packageversion 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.

Examples

# Cox PH model with age, sex, race, and treatmentlibrary("survival")fit <- coxph(  Surv(time = time, event = delta) ~ Age + Sex + Race + Group,  data = tabdata)tabcoxph(fit)# Can also use pipingfit %>% tabcoxph()# Same as previous, but with custom labels for Age and Race and factors# displayed in slightly more compressed formatfit %>%  tabcoxph(    var.labels = list(Age = "Age (years)", Race = "Race/ethnicity"),    factor.compression = 2  )# Cox PH model with some higher-order termsfit <- coxph(  Surv(time = time, event = delta) ~  poly(Age, 2, raw = TRUE) + Sex + Race + Group + Race*Group,  data = tabdata)fit %>% tabcoxph()

Sample Dataset fortab Package

Description

Data frame with 15 variables, used to illustrate certain functions.

Source

Simulated data in R


Create Frequency Table

Description

Creates an I-by-J frequency table comparing the distribution ofyacross levels ofx.

Usage

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)

Arguments

formula

Formula, e.g.Sex ~ Group.

data

Data frame containing variables named informula.

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. Choicesfor each element are"n" for total sample size,"overall" foroverall distribution ofy,"xgroups" for distributions ofy for eachx group,"test" for test statistic, and"p" for p-value.

cell

Character string specifying what statistic to display in cells.Choices are"counts","tot.percent","col.percent",and"row.percent".

parenth

Character string specifying what statistic to display inparentheses. Choices are"none","se","ci","counts","tot.percent","col.percent", and"row.percent".

sep.char

Character string with separator to place between lower andupper bound of confidence intervals. Typically"-" or", ".

test

Character string specifying which test for association betweenx andy should be used. Choices are"chi.fisher" forPearson's chi-squared test if its assumptions are met, otherwise Fisher'sexact test;"chi";"fisher";"z" for z test withoutcontinuity correction; and"z.continuity" for z test with continuitycorrection. The last two only work if bothx andy are binary.

xlevels

Character vector with labels for the levels ofx, usedin column headings.

yname

Character string with a label for they variable.

ylevels

Character vector with labels for the levels ofy. Notethat levels ofy are listed in the order that they appear when you runtable(y, x).

compress.binary

Logical value for whether to compress binaryyvariable to a single row, excluding the first level rather than showing both.

yname.row

Logical value for whether to include a row displaying thename of they variable and indent the factor levels.

text.label

Character string with text to put after theyvariable name, identifying what cell values and parentheses represent.

quantiles

Numeric value. If specified, table comparesy acrossquantiles ofx created on the fly.

quantile.vals

Logical value for whether labels forx quantilesshould show quantile number and corresponding range, e.g. Q1 [0.00, 0.25),rather than just the quantile number.

decimals

Numeric value specifying number of decimal places for numbersother than p-values.

formatp.list

List of arguments to pass toformatp.

n.headings

Logical value for whether to display group sample sizes inparentheses in column headings.

kable

Logical value for whether to return akable.

Value

kable.

Examples

# 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))

Create Frequency Table (for Complex Survey Data)

Description

Creates an I-by-J frequency table comparing the distribution ofyacross levels ofx.

Usage

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)

Arguments

formula

Formula, e.g.Race ~ Sex.

design

Survey design object fromsvydesign.

columns

Character vector specifying what columns to include. Choicesfor each element are"n" for total unweighted sample size,"N"for total weighted sample size,"overall" for overall distribution ofy,"xgroups" for distributions ofy for eachxgroup, and"p" for Chi-square p-value.

cell

Character string specifying what statistic to display in cells.Choices are"n","N", and"col.percent".

parenth

Character string specifying what statistic to display inparentheses. Choices are"none","n","N","col.percent","se", and"ci".

sep.char

Character string with separator to place between lower andupper bound of confidence intervals. Typically"-" or", ".

xlevels

Character vector with labels for the levels ofx, usedin column headings.

yname

Character string with a label for they variable.

ylevels

Character vector with labels for the levels ofy. Notethat levels ofy are listed in the order that they appear when you runtable(y, x).

compress.binary

Logical value for whether to compress binaryyvariable to a single row, excluding the first level rather than showing both.

yname.row

Logical value for whether to include a row displaying thename of they variable.

text.label

Character string with text to put after theyvariable name, identifying what cell values and parentheses represent.

decimals

Numeric value specifying number of decimal places for numbersother than p-values.

svychisq.list

List of arguments to pass tosvychisq.

formatp.list

List of arguments to pass toformatp.

n.headings

Logical value for whether to display unweighted samplesizes in parentheses in column headings.

N.headings

Logical value for whether to display weighted sample sizesin parentheses in column headings.

kable

Logical value for whether to return akable.

Details

Basicallytabmedians for complex survey data. Relies heavily onthesurvey package.

Value

kable or character matrix.

Examples

# Create survey design objectlibrary("survey")design <- svydesign(  data = tabsvydata,  ids = ~sdmvpsu,  strata = ~sdmvstra,  weights = ~wtmec2yr,  nest = TRUE)# Compare race distribution by sextabfreq.svy(Race ~ Sex, design)

Create Summary Table for Fitted Generalized Estimating Equation Model

Description

Creates a table summarizing a GEE fit using thegeefunction.

Usage

tabgee(  fit,  data = NULL,  columns = NULL,  robust = TRUE,  var.labels = NULL,  factor.compression = 1,  sep.char = ", ",  decimals = 2,  formatp.list = NULL)

Arguments

fit

Fittedgee object.

data

Data frame that served as 'data' in function call togee. Only needs to be specified if one or more of thepredictors is a factor andfactor.compression is 1, 2, 3, or 4.

columns

Character vector specifying what columns to include. Choicesfor each element are"beta","se","betaci" for 95% CIfor Beta,"beta.se" for Beta (SE),"beta.ci" for Beta(95% CI),"or","orci" for 95% CI for OR,"or.ci" forOR (95% CI),"hr","hrci" for 95% CI for HR,"hr.ci"for HR (95% CI),"z" for z statistic, and"p". If OR's or HR'sare requested, the function will trust that exponentiated betas correspond tothese quantities.

robust

Logical value for whether to use robust standard errors.

var.labels

Named list specifying labels to use for certain predictors.For example, iffit includes a predictor named "race"that you want to label "Race/ethnicity" and a predictor named "age_yrs" thatyou want to label "Age (years)", usevar.labels = list(race = "Race/ethnicity", age_yrs = "Age (years)".

factor.compression

Integer value from 1 to 5 controlling how muchcompression is applied to factor predictors (higher value = morecompression). 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, rowsare Level 2, ...

sep.char

Character string with separator to place between lower andupper bound of confidence intervals. Typically"-" or", ".

decimals

Numeric value specifying number of decimal places for numbersother than p-values.

formatp.list

List of arguments to pass toformatp.

Value

kable.

Examples

# Load in sample dataset and convert to long formattabdata2 <- 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 treatmentlibrary("gee")fit <- gee(bp ~ Age + Sex + Race + Group, id = id, data = tabdata2,           corstr = "unstructured")tabgee(fit)# Can also use pipingfit %>% tabgee(data = tabdata2)# Same as previous, but with custom labels for Age and Race and factors# displayed in slightly more compressed formatfit %>%  tabgee(    data = tabdata2,    var.labels = list(Age = "Age (years)", Race = "Race/ethnicity"),    factor.compression = 2  )# GEE with some higher-order terms# higher-order termsfit <- gee(  highbp ~ poly(Age, 2, raw = TRUE) + Sex + Race + Group + Race*Group,  id = id,  data = tabdata2,  family = "binomial",  corstr = "unstructured")fit %>% tabgee(data = tabdata2)

Create Summary Table for Fitted Generalized Linear Model

Description

Creates a table summarizing a GLM fit usingglm.

Usage

tabglm(  fit,  columns = NULL,  xvarlabels = NULL,  factor.compression = 1,  sep.char = ", ",  decimals = 2,  formatp.list = NULL)

Arguments

fit

Fittedglm object.

columns

Character vector specifying what columns to include. Choicesfor each element are"beta","se","betaci" for 95% CIfor Beta,"beta.se" for Beta (SE),"beta.ci" for Beta(95% CI),"or","orci" for 95% CI for OR,"or.ci" forOR (95% CI),"hr","hrci" for 95% CI for HR,"hr.ci"for HR (95% CI),"test" for z/t statistic, and"p". If OR's orHR's are requested, the function will trust that exponentiated betascorrespond to these quantities.

xvarlabels

Named list specifying labels to use for certain predictors.For example, iffit includes a predictor named "race"that you want to label "Race/ethnicity" and a predictor named "age_yrs" thatyou want to label "Age (years)", usexvarlabels = list(race = "Race/ethnicity", age_yrs = "Age (years)".

factor.compression

Integer value from 1 to 5 controlling how muchcompression is applied to factor predictors (higher value = morecompression). 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, rowsare Level 2, ...

sep.char

Character string with separator to place between lower andupper bound of confidence intervals. Typically"-" or", ".kable.

decimals

Numeric value specifying number of decimal places for numbersother than p-values.

formatp.list

List of arguments to pass toformatp.

Value

kable.

Examples

# Linear regression: BMI vs. age, sex, race, and treatmentfit <- glm(BMI ~ Age + Sex + Race + Group, data = tabdata)tabglm(fit)# Can also use pipingfit %>% tabglm()# Logistic regression: 1-year mortality vs. age, sex, race, and treatmentfit <- 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 formatfit %>%  tabglm(    xvarlabels = list(Age = "Age (years)", Race = "Race/ethnicity"),    factor.compression = 2  )# Logistic regression model with some higher-order termsfit <- glm(  death_1yr ~ poly(Age, 2, raw = TRUE) + Sex + BMI + Sex * BMI,  data = tabdata,  family = "binomial")fit %>% tabglm()

Create Table Comparing Group Means

Description

Creates a table comparing the mean ofy across levels ofx.

Usage

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)

Arguments

formula

Formula, e.g.BMI ~ Group.

data

Data frame containing variables named informula.

x

Vector of values for the categoricalx variable.

y

Vector of values for the continuousy variable.

columns

Character vector specifying what columns to include. Choicesfor each element are"n" for total sample size,"overall" foroverall mean,"xgroups" forx group means,"diff" fordifference inx group means (this one and the next two are onlyavailable for binaryx),"diffci" for 95x group means,"diff.ci" for difference in group means and 95confidence interval,"test" for test statistic, and"p" forp-value.

parenth

Character string specifying what statistic to display inparentheses after the means. Choices are"none","sd","se","t.ci","z.ci","range", and"minmax".

sep.char

Character string with separator to place between lower andupper bound of confidence intervals. Typically"-" or", ".

variance

Character string specifying which version of the two-samplet-test to use ifx has 2 levels. Choices are"equal" for equalvariance t-test,"unequal" for unequal variance t-test, and"f"for F test to determine which to use.

xlevels

Character vector with labels for the levels ofx, usedin column headings.

yname

Character string with a label for they variable.

text.label

Character string with text to put after theyvariable name, identifying what cell values and parentheses represent.

quantiles

Numeric value. If specified, table comparesy acrossquantiles ofx created on the fly.

quantile.vals

Logical value for whether labels forx quantilesshould show quantile number and corresponding range, e.g. Q1 [0.00, 0.25),rather than just the quantile number.

decimals

Numeric value specifying number of decimal places for numbersother than p-values.

formatp.list

List of arguments to pass toformatp.

n.headings

Logical value for whether to display group sample sizes inparentheses in column headings.

kable

Logical value for whether to return akable.

Details

A t-test is used to compare means ifx has two levels, and a one-wayanalysis of variance is used ifx has more than two levels.Observations with missing values forx and/ory are dropped.

Value

kable or character matrix.

Examples

# 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"))

Create Table Comparing Group Means (for Complex Survey Data)

Description

Creates a table comparing the mean ofy across levels ofx.

Usage

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)

Arguments

formula

Formula, e.g.BMI ~ Sex.

design

Survey design object fromsvydesign.

columns

Character vector specifying what columns to include. Choicesfor each element are"n" for total sample size,"overall" foroverall mean,"xgroups" forx group means,"diff" fordifference inx group means (this one and the next two are onlyavailable for binaryx),"diffci" for 95x group means,"diff.ci" for difference in group means and 95confidence interval, and"p" for p-value.

parenth

Character string specifying what statistic to display inparentheses after the means. Choices are"none","sd","se","t.ci","z.ci","range", and"minmax".

sep.char

Character string with separator to place between lower andupper bound of confidence intervals. Typically"-" or", ".

xlevels

Character vector with labels for the levels ofx, usedin column headings.

yname

Character string with a label for they variable.

text.label

Character string with text to put after theyvariable name, identifying what cell values and parentheses represent

decimals

Numeric value specifying number of decimal places for numbersother than p-values.

anova.svyglm.list

List of arguments to pass toanova.svyglm. Only used ifx has three or morelevels.

formatp.list

List of arguments to pass toformatp.

n.headings

Logical value for whether to display group sample sizes inparentheses in column headings.

N.headings

Logical value for whether to display weighted sample sizesin parentheses in column headings.

kable

Logical value for whether to return akable.

Details

Basicallytabmeans for complex survey data. Relies heavily onthesurvey package.

Value

kable or character matrix.

Examples

# Create survey design objectlibrary("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 Table Comparing Group Medians

Description

Creates a table comparing the median ofy across levels ofx.

Usage

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)

Arguments

formula

Formula, e.g.BMI ~ Group.

data

Data frame containing variables named informula.

x

Vector of values for the categoricalx variable.

y

Vector of values for the continuousy variable.

columns

Character vector specifying what columns to include. Choicesfor each element are"n" for total sample size,"overall" foroverall median,"xgroups" forx group medians,"diff"for difference inx group medians (only available for binaryx),"test" for test statistic, and"p" for p-value.

parenth

Character string specifying what values are shown inparentheses after the medians in each cell. Choices are"none","iqr","q1q3" for first and third quartiles,"range","minmax", and"ci" for 95% confidence interval for the mediansbased on normal approximation to binomial.

sep.char

Character string with separator to place between lower andupper bound of confidence intervals. Typically"-" or", ".

xlevels

Character vector with labels for the levels ofx, usedin column headings.

yname

Character string with a label for they variable.

text.label

Character string with text to put after theyvariable name, identifying what cell values and parentheses represent.

quantiles

Numeric value. If specified, table comparesy acrossquantiles ofx created on the fly.

quantile.vals

Logical value for whether labels forx quantilesshould show quantile number and corresponding range, e.g. Q1 [0.00, 0.25),rather than just the quantile number.

decimals

Numeric value specifying number of decimal places for numbersother than p-values.

formatp.list

List of arguments to pass toformatp.

n.headings

Logical value for whether to display group sample sizes inparentheses in column headings.

kable

Logical value for whether to return akable.

Details

Ifx has 2 levels, a Mann-Whitney U (also known as Wilcoxonrank-sum) test is used to test whether the distribution ofy differsin the two groups; ifx has more than 2 levels, a Kruskal-Wallis testis used to test whether the distribution ofy differs across atleast two of the groups. Observations with missing values forx and/ory are dropped.

Value

kable.

Examples

# 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"))

Create Table Comparing Group Medians (for Complex Survey Data)

Description

Creates a table comparing the median ofy across levels ofx.

Usage

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)

Arguments

formula

Formula, e.g.BMI ~ Sex.

design

Survey design object fromsvydesign.

columns

Character vector specifying what columns to include. Choicesfor each element are"n" for total sample size,"overall" foroverall median,"xgroups" forx group medians,"diff"for difference inx group medians (only available for binaryx), and"p" for p-value.

parenth

Character string specifying what values are shown inparentheses after the medians in each cell. Choices are"none","iqr","q1q3" for first and third quartiles,"range","minmax", and"ci" for 95% confidence interval for the median.

sep.char

Character string with separator to place between lower andupper bound of confidence intervals. Typically"-" or", ".

xlevels

Character vector with labels for the levels ofx, usedin column headings.

yname

Character string with a label for they variable.

text.label

Character string with text to put after theyvariable name, identifying what cell values and parentheses represent.

decimals

Numeric value specifying number of decimal places for numbersother than p-values.

svyranktest.list

List of arguments to pass tosvyranktest.

formatp.list

List of arguments to pass toformatp.

n.headings

Logical value for whether to display group sample sizes inparentheses in column headings.

N.headings

Logical value for whether to display weighted sample sizesin parentheses in column headings.

kable

Logical value for whether to return akable.

Details

Basicallytabmedians for complex survey data. Relies heavily onthesurvey package.

Value

kable or character matrix.

Examples

# Create survey design objectlibrary("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 Table Comparing Characteristics Across Levels of a CategoricalVariable

Description

Creates a table comparing multiple characteristics (e.g. median age, meanBMI, and race/ethnicity distribution) across levels ofx.

Usage

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)

Arguments

formula

Formula, e.g.Age + Sex + Race + BMI ~ Group.

data

Data frame containing variables named informula.

xvarname

Character string with name of column variable. Should be oneofnames(data).

yvarnames

Character vector with names of row variables. Each elementshould be one ofnames(data).

ymeasures

Character vector specifying whether eachy variableshould be summarized by mean, median, or frequency. For example, if you wantto compare frequencies for the first variable, means for the second, andmedians for the third, you would setymeasures = c("freq", "mean", "median"). If unspecified, functioncompares means for numeric variables and frequencies for factor and charactervariables.

columns

Character vector specifying what columns to include. Choicesfor each element are"n" for total sample size,"overall" foroverall statistics,"xgroups" forx group statistics,"test" for test statistic, and"p" for p-value.

listwise.deletion

Logical value for whether observations with missingvalues for anyy variable should be excluded entirely (as opposed tousing all available data for each comparison).

sep.char

Character string with separator to place between lower andupper bound of confidence intervals. Typically"-" or", ".

xlevels

Character vector with labels for the levels ofx, usedin column headings.

yvarlabels

Named list specifying labels for certainyvariables. For example, if you want variables named "race" and "age_yrs" toprint as "Race/ethnicity" and "Age (years)", use\codeyvarlabels = list(race = "Race/ethnicity", age_yrs = "Age (years)").

ylevels

Character vector (if only 1 frequency comparison) or list ofcharacter vectors with labels for the levels of each categoricalyvariable.

quantiles

Numeric value. If specified, function comparesyvariables across quantiles ofx. For example, ifx contains BMIvalues andyvarnames includes HDL and race, settingquantiles = 3 compares mean BMI and distribution of race across BMItertiles.

quantile.vals

Logical value for whether labels forx quantilesshould show quantile number and corresponding range, e.g. Q1 [0.00, 0.25),rather than just the quantile number.

decimals

Numeric vector specifying number of decimal places fornumbers other than p-values for eachy variable. Can be a single valueto use for ally variables.

formatp.list

List of arguments to pass toformatp.

n.headings

Logical value for whether to display group sample sizes inparentheses in column headings.

tabmeans.list

List of arguments to pass totabmeans.

tabmedians.list

List of arguments to pass totabmedians.

tabfreq.list

List of arguments to pass totabfreq.

kable

Logical value for whether to return akable.

Value

kable or character matrix.

Examples

# Compare age, sex, race, and BMI in control vs. treatment grouptabmulti(Age + Sex + Race + BMI ~ Group, data = tabdata)# Same as previous, but compare medians rather than means for BMItabmulti(Age + Sex + Race + BMI ~ Group, data = tabdata,         ymeasures = c("mean", "freq", "freq", "median"))

Create Table Comparing Characteristics Across Levels of a CategoricalVariable (for Complex Survey Data)

Description

Creates a table comparing multiple characteristics (e.g. median age, meanBMI, and race/ethnicity distribution) across levels ofx.

Usage

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)

Arguments

formula

Formula, e.g.Age + Race + BMI ~ Sex.

design

Survey design object fromsvydesign.

xvarname

Character string with name of column variable. Should be oneofnames(design$variables).

yvarnames

Character vector with names of row variables. Each elementshould be one ofnames(design$variables).

ymeasures

Character vector specifying whether eachy variableshould be summarized by mean, median, or frequency. For example, if you wantto compare frequencies for the first variable, means for the second, andmedians for the third, you would setymeasures = c("freq", "mean", "median"). If unspecified, functioncompares means for numeric variables and frequencies for factor and charactervariables.

columns

Character vector specifying what columns to include. Choicesfor each element are"n" for unweighted sample size,"N" forweighted sample size,"overall" for overall statistics,"xgroups" forx group statistics, and"p" for p-value.

listwise.deletion

Logical value for whether observations with missingvalues for anyy variable should be excluded entirely (as opposed tousing all available data for each comparison).

sep.char

Character string with separator to place between lower andupper bound of confidence intervals. Typically"-" or", ".

xlevels

Character vector with labels for the levels ofx, usedin column headings.

yvarlabels

Named list specifying labels for certainyvariables. For example, if you want variables named "race" and "age_yrs" toprint as "Race/ethnicity" and "Age (years)", use\codeyvarlabels = list(race = "Race/ethnicity", age_yrs = "Age (years)").

ylevels

Character vector (if only 1 frequency comparison) or list ofcharacter vectors with labels for the levels of each categoricalyvariable.

decimals

Numeric vector specifying number of decimal places fornumbers other than p-values for eachy variable. Can be a single valueto use for ally variables.

formatp.list

List of arguments to pass toformatp.

n.headings

Logical value for whether to display unweighted samplesizes in parentheses in column headings.

N.headings

Logical value for whether to display weighted sample sizesin parentheses in column headings.

kable

Logical value for whether to return akable.

tabmeans.svy.list

List of arguments to pass totabmeans.svy.

tabmedians.svy.list

List of arguments to pass totabmedians.svy.

tabfreq.svy.list

List of arguments to pass totabfreq.svy.

Details

Basicallytabmulti for complex survey data. Relies heavily onthesurvey package.

Value

kable or character matrix.

Examples

# Create survey design objectlibrary("survey")design <- svydesign(  data = tabsvydata,  ids = ~sdmvpsu,  strata = ~sdmvstra,  weights = ~wtmec2yr,  nest = TRUE)# Compare age, race, and BMI by sextabmulti.svy(Age + Race + BMI ~ Sex, design)

Create Regression Table from Betas and Standard Errors

Description

Useful for quickly creating a summary table.

Usage

tabreg(  betas,  ses = NULL,  varcov = NULL,  columns = c("beta.se", "p"),  sep.char = ", ",  decimals = NULL,  formatp.list = NULL,  labels = NULL)

Arguments

betas

Numeric vector.

ses

Numeric vector.

varcov

Numeric matrix.

columns

Character vector specifying what columns to include. Choicesare"beta","se","betaci","beta.se","beta.ci","or","orci","or.ci", and"p".

sep.char

Character string with separator to place between lower andupper bound of confidence intervals. Typically"-" or", ".

decimals

Numeric value specifying number of decimal places for numbersother than p-values.

formatp.list

List of arguments to pass toformatp.

labels

Character vector.

Value

kable.

Examples

# Create summary table for mtcars regressionfit <- lm(mpg ~ wt + hp + drat, data = mtcars)tabreg(  betas = fit$coef,  varcov = vcov(fit),  labels = c("Intercept", "Weight", "HP", "Rear axle ratio"))

Sample Survey Dataset fortab Package

Description

Data frame with with 9 variables, used to illustrate certain functions. Dataare derived from the National Health and Nutrition Examination Survey, years2003-2004, although the variables 'time' and 'event' are simulated (fake).

Source

https://wwwn.cdc.gov/Nchs/Nhanes/2003-2004/DEMO_C.htm

References

Centers for Disease Control and Prevention (CDC). National Center for HealthStatistics (NCHS). National Health and Nutrition Examination Survey Data.Hyattsville, MD: US Department of Health and Human Services, Centers forDisease Control and Prevention, 2003-2004.https://wwwn.cdc.gov/nchs/nhanes/continuousnhanes/default.aspx?BeginYear=2003.Accessed June 8, 2019.


Output a Table to the RStudio Viewer

Description

Does some basic formatting and then callskable andkable_styling to print table to Viewer.

Usage

toviewer(x)

Arguments

x

Character matrix


[8]ページ先頭

©2009-2025 Movatter.jp