Making expression for contingency table and goodness of fit tests

expr_contingency_tab(
data,
x,
y = NULL,
counts = NULL,
paired = FALSE,
ratio = NULL,
k = 2L,
conf.level = 0.95,
output = "expression",
...
)

## Arguments

data A dataframe (or a tibble) from which variables specified are to be taken. A matrix or tables will not be accepted. The variable to use as the rows in the contingency table. The variable to use as the columns in the contingency table. Default is NULL. If NULL, one-sample proportion test (a goodness of fit test) will be run for the x variable. Otherwise association test will be carried out. A string naming a variable in data containing counts, or NULL if each row represents a single observation. Logical indicating whether data came from a within-subjects or repeated measures design study (Default: FALSE). If TRUE, McNemar's test subtitle will be returned. If FALSE, Pearson's chi-square test will be returned. A vector of proportions: the expected proportions for the proportion test (should sum to 1). Default is NULL, which means the null is equal theoretical proportions across the levels of the nominal variable. This means if there are two levels this will be ratio = c(0.5,0.5) or if there are four levels this will be ratio = c(0.25,0.25,0.25,0.25), etc. Number of digits after decimal point (should be an integer) (Default: k = 2L). Scalar between 0 and 1. If unspecified, the defaults return 95% lower and upper confidence intervals (0.95). If "expression", will return expression with statistical details, while "dataframe" will return a dataframe containing the results. Additional arguments (currently ignored).

## Value

Expression for contingency analysis (Pearson's chi-square test for independence for between-subjects design or McNemar's test for within-subjects design) or goodness of fit test for a single categorical variable.

## Details

For more details about how the effect sizes and their confidence intervals were computed, see documentation in ?effectsize::cramers_v and ?effectsize::cohens_g.

## References

For more details, see- https://indrajeetpatil.github.io/statsExpressions/articles/stats_details.html

## Examples

# for reproducibility
set.seed(123)
library(statsExpressions)

# ------------------------ association tests -----------------------------

# without counts data
expr_contingency_tab(
data = mtcars,
x = am,
y = cyl,
paired = FALSE
)
#> Warning: Chi-squared approximation may be incorrect#> paste(chi["Pearson"]^2, "(", "2", ") = ", "8.74", ", ", italic("p"),
#>     " = ", "0.013", ", ", widehat(italic("V"))["Cramer"], " = ",
#>     "0.46", ", CI"["95%"], " [", "0.00", ", ", "0.78", "]", ", ",
#>     italic("n")["obs"], " = ", 32L)
# ------------------------ goodness of fit tests ---------------------------

# with counts
expr_contingency_tab(
data = as.data.frame(HairEyeColor),
x = Eye,
counts = Freq,
ratio = c(0.2, 0.2, 0.3, 0.3)
)
#> paste(chi["gof"]^2, "(", "3", ") = ", "278.96", ", ", italic("p"),
#>     " = ", "3.56e-60", ", ", widehat(italic("V"))["Cramer"],
#>     " = ", "0.27", ", CI"["95%"], " [", "0.22", ", ", "0.32",
#>     "]", ", ", italic("n")["obs"], " = ", 592L)