View source: R/mosaic_analysis.R
| mosaic_analysis | R Documentation |
Analyses the association between two categorical columns of a data.frame.
Builds the contingency table, drops sparse categories below
min_count, runs a Pearson chi-square test (or Fisher's exact test),
computes Cramer's V with a df-adjusted effect-size label, and draws a flat
ggplot2 mosaic whose tile area encodes counts and whose fill encodes the
standardized Pearson residual (Nestimate diverging palette). All tabular
output is a tidy one-row-per-cell data.frame.
mosaic_analysis(
data,
var1,
var2,
min_count = 10L,
test = c("chisq", "fisher"),
percentage_base = c("total", "row", "column"),
tile_label = c("count", "percent", "residual", "category", "none"),
title = "",
...
)
data |
A data.frame containing the two variables. |
var1 |
Character. Name of the first variable (mosaic columns). |
var2 |
Character. Name of the second variable (stacked within columns). |
min_count |
Integer. Minimum marginal count for a category to be kept. Categories of either variable below this are dropped before testing. Default 10. |
test |
Character. |
percentage_base |
Character. Base for the |
tile_label |
Character. What to print inside each tile: |
title |
Character. Plot title. Default |
... |
Further flat-mosaic styling arguments passed to the renderer
(e.g. |
An object of class "mosaic_analysis": a list with
The flat mosaic ggplot object.
Tidy data.frame, one row per (var1, var2) cell, with
observed, expected, residual (standardized), and
pct (on percentage_base).
One-row data.frame: test, statistic,
df, p_value, cramers_v, effect_size,
n.
The raw htest object.
Effect size value and label.
The filtered contingency table.
List of dropped var1 / var2 categories.
Row counts before/after filtering.
mosaic_plot for the network/table mosaic (which also
accepts style = "flat").
df <- data.frame(
gender = sample(c("F", "M"), 200, replace = TRUE),
level = sample(c("Low", "Mid", "High"), 200, replace = TRUE)
)
res <- mosaic_analysis(df, "gender", "level", min_count = 5)
res$stats
res$counts
plot(res, tile_label = "percent")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.