jmvtab: Crosstables

View source: R/jmvtab.h.R

jmvtabR Documentation

Crosstables

Description

Crosstables

Usage

jmvtab(
  data,
  row_vars = NULL,
  col_vars = NULL,
  tab_vars = NULL,
  wt = NULL,
  pct = "no",
  color = "no",
  chi2 = TRUE,
  OR = "no",
  na = "keep",
  lvs = "all",
  other_if_less_than = 0,
  cleannames = TRUE,
  ref = "auto",
  ref2 = "first",
  comp = "tab",
  ci = "auto",
  conf_level = 0.95,
  ci_print = "ci",
  totaltab = "line",
  wrap_rows = 35,
  wrap_cols = 15,
  display = "auto",
  add_n = TRUE,
  add_pct = FALSE,
  subtext = "",
  digits = 0
)

Arguments

data

A data.frame.

row_vars

The row variable, which will be printed with one level per line. If numeric, it will be converted to factor.

col_vars

One column is printed for each level of each column variable. For numeric variables means are calculated, in a single column.

tab_vars

One subtable is made for each combination of levels of the tab variables. All tab variables are converted to factor. Leave empty to make a simple table.

wt

A weight variable, of class numeric. Leave empty for unweighted results.

pct

The type of percentages to calculate :

  • "row": row percentages.

  • "col": column percentages.

  • "all": frequencies for each subtable/group, if there is tab_vars.

  • "all_tabs": frequencies for the whole (set of) table(s).

color

The type of colors to print, as a single string. Vectorised over row_vars.

  • "no": by default, no colors are printed.

  • "diff": color percentages and means based on cells differences from totals (or from first cells when ref = "first").

  • "diff_ci": color pct and means based on cells differences from totals or first cells, removing coloring when the confidence interval of this difference is higher than the difference itself.

  • "after_ci": idem, but cut off the confidence interval from the difference first.

  • "contrib": color cells based on their contribution to variance (except mean columns, from numeric variables).

  • "OR": for pct == "col" or pct == "row", color based on odds ratios (or relative risks ratios)

chi2

Set to TRUE to make a Chi2 and add summary stats. Also useful to color cells based on their contribution to variance.

OR

With pct = "row" or pct = "col", calculate and print odds ratios (for binary variables) or relative risks ratios (for variables with 3 levels or more).

  • "no": by default, no OR are calculated.

  • "OR": print OR (instead of percentages).

  • "OR_pct": print OR, with percentages in bracket.

na

The policy to adopt with missing values. It must be a single string.

  • na = "keep": by default, prints NA's as explicit "NA" level.

  • na = "drop": removes NA levels before making each table (tabs made with different column variables may have a different number of observations, and won't exactly have the same total columns).

lvs

The levels of col_vars to keep.

  • "all": by default, all levels are kept.

  • "first": only keep the first level of each col_vars

  • "auto": keep the first level when col_var is only two levels, keep all levels otherwise.

other_if_less_than

When set to a positive integer, levels with less count than that will be merged into an "Others" level.

cleannames

By default, clean levels names, by removing prefix numbers like "1-", and text in parenthesis. Set to FALSE to avoid this behaviour.

ref

The reference cell to calculate differences and ratios (used to print colors) :

  • "auto": by default, cell difference from the corresponding total (rows or cols depending on pct = "row" or pct = "col") is used for diff ; cell ratio from the first line (or col) is use for OR (odds ratio/relative risks ratio).

  • "tot": totals are always used.

  • "first": calculate cell difference or ratio from the first cell of the row or column (useful to color temporal developments).

  • n: when ref is an integer, the nth row (or column) is used for comparison.

  • "regex": when ref is a string, it it used as a regular expression, to match with the names of the rows (or columns). Be precise enough to match only one column or row, otherwise you get a warning message.

  • "no": not use ref and not calculate diffs to gain calculation time.

ref2

A second reference cell is needed to calculate odds ratios (or relative risks ratios). The first cell of the row or column is used by default. See ref for the full list of possible values.

comp

The comparison level : by subtables/groups, or for the whole table.

ci

The type of confidence intervals to calculate, passed to tab_ci.

  • "cell": absolute confidence intervals of cells percentages.

  • "diff": confidence intervals of the difference between a cell and the relative total cell (or relative first cell when ref = "first").

  • "auto": ci = "diff" for means and row/col percentages, ci = "cell" for frequencies ("all", "all_tabs").

By default, for percentages, with ci = "cell" Wilson's method is used, and with ci = "diff" Wald's method along Agresti and Caffo's adjustment. Means use classic method.

conf_level

The confidence level, as a single numeric between 0 and

  1. Default to 0.95 (95\

ci_print

By default confidence interval are printed with the interval display. Set to "moe" to use pct +- moe instead.

totaltab

The total table, if there are subtables/groups (i.e. when tab_vars is provided). Vectorised over row_vars.

  • "line": by default, add a general total line (necessary for calculations with comp = "all")

  • "table": add a complete total table (i.e. row_var by col_vars without tab_vars).

  • "no": not to draw any total table.

wrap_rows

By default, rownames are wrapped when larger than 30 characters.

wrap_cols

By default, colnames are wrapped when larger than 12 characters.

display

The information to display in the table.

add_n

For pct = "row" or pct = "col", set to FALSE not to add another column or row with unweighted counts (n).

add_pct

Set to TRUE to add a column with the frequencies of the row variable (for pct = "row") or a row with the frequencies of the column variable (for pct = "col")

subtext

A character vector to print rows of legend under the table.

digits

The number of digits to print, as a single integer, or an integer vector the same length as col_vars.

Value

A results object containing:

results$html_table a html
results$chi2_table a table
results$plot an image

Tables can be converted to data frames with asDF or as.data.frame. For example:

results$chi2_table$asDF

as.data.frame(results$chi2_table)


BriceNocenti/tablr documentation built on April 12, 2025, 12:56 a.m.