contingency_table {epitab}R Documentation

Builds a contingency table

Description

A contingency table provides cross-tabulated frequencies between an outcome of interest and one or more independent variables. This function extends contingency tables to include summary statistics formed both column-wise and row-wise, looking at outcomes and covariates respectively in isolation. This allows for a large amount of flexibility and tables can be drawn for a variety of situations. By default, the print method fits these tables to standard R console output, but publication quality tables can be produced using the neat_table function. See the vignette for further guidance.

Usage

contingency_table(independents, data, outcomes = NULL,
  crosstab_funcs = NULL, row_funcs = NULL, col_funcs = NULL,
  marginal = TRUE)

Arguments

independents

A named list of independent variables, which will be distributed down the table's rows. The variables must be specified by strings, with the item name used as the column header.

data

The data set that contains the columns specified in cat_vars and outcome.

outcomes

The variables to cross-tabulate by. These will be distributed across the table's columns. Specified as a named list of strings. Must correspond to factor or character variables.

crosstab_funcs

A list of functions that are applied to every cross-tabulation permutation of independents and outcomes. The most common function, the frequency, is provided with the package in function freq. See the vignette for further guidance.

row_funcs

A list of functions that are applied row-wise to the table, one independent variable at a time, providing a value for each level of the factors specified in independents. Two functions: odds_ratio and hazard_ratio come provided with the package. See the vignette for further guidance.

col_funcs

A list of functions that are applied column-wise to the table, to every outcome separate from the independent variables. Examples provided with the package included summary_mean and summary_median, which calculate the mean and median value of a specified continuous variable for each level of the outcome. See the vignette for further guidance.

marginal

Whether to include the counts of each level of cat_vars, the marginal frequency.

Value

An S3 object of class contintab, that provides the cell contents as a matrix of strings.

Examples


# This example uses a dummy data set of whether an individual was treated or not
treat <- data.frame(age=abs(rnorm(100, 60, 20)),
                    sex=factor(sample(c("M", "F"), 100, replace=TRUE)),
                    variant=factor(sample(c("A", "B"), 100, replace=TRUE)),
                    treated=factor(sample(c("Yes", "No"), 100, replace=TRUE),
                                   levels=c("Yes", "No")))
treat$agebin <- cut(treat$age, breaks=c(0, 40, 60, 80, 9999),
                    labels=c("0-40", "41-60", "61-80", "80+"))

# Displays a standard contingency table
contingency_table(list("Age"='agebin', "Sex"='sex'),
                  outcomes=list('Treated'='treated'),
                  crosstab_funcs=list(freq()),
                  data=treat)

# Continuous variables can be summarised with respect to the outcome
# by using col_funcs
contingency_table(list("Age"='agebin', "Sex"='sex'),
                  outcomes=list('Treated'='treated'),
                  crosstab_funcs=list(freq()),
                  col_funcs=list("Mean age"=summary_mean('age')),
                  data=treat)

# Regression coefficients can be added using row_funcs
contingency_table(list("Age"='agebin', "Sex"='sex'),
                   treat,
                   outcomes=list('Treated'='treated'),
                   crosstab_funcs=list(freq()),
                   row_funcs=list("Odds ratio"=odds_ratio('treated'),
                                  "Adjusted odds ratio"=odds_ratio('treated', adjusted=TRUE)))


[Package epitab version 0.2.2 Index]