Skip to content
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
Config/testthat/parallel: true
Config/Needs/website: easystats/easystatstemplate
Config/roxygen2/version: 8.1.0
Config/roxygen2/version: 8.1.1
10 changes: 9 additions & 1 deletion R/data_tabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
#' any combination of `N` (frequencies including `NA`), `"raw"` (percentage
#' including `NA` values), `"valid"` (percentage excluding `NA` values) and
#' `"cumulative"` (percentage excluding `NA` values).
#' @param chisq include chi square for two dimensional tables. This is always
#' calculated with the `chisq.test()` `simulate.p.value = TRUE`.
#' @param ... not used.
#' @inheritParams extract_column_names
#'
Expand Down Expand Up @@ -163,6 +165,7 @@ data_tabulate.default <- function(
name = NULL,
verbose = TRUE,
metrics = c("N", "raw", "valid", "cumulative"),
chisq = FALSE,
...
) {
# save label attribute, before it gets lost...
Expand Down Expand Up @@ -211,7 +214,8 @@ data_tabulate.default <- function(
remove_na = remove_na,
proportions = proportions,
obj_name = obj_name,
group_variable = group_variable
group_variable = group_variable,
chisq = chisq
))
}
# frequency table
Expand Down Expand Up @@ -334,6 +338,7 @@ data_tabulate.default <- function(
attr(out, "by") <- NULL
} else {
attr(out, "by") <- by_name
attr(out, "chi_square") <- chisq
}

class(out) <- c("datawizard_table", "data.frame")
Expand All @@ -357,6 +362,7 @@ data_tabulate.data.frame <- function(
proportions = NULL,
collapse = FALSE,
verbose = TRUE,
chisq = FALSE,
...
) {
if (!is.null(by)) {
Expand Down Expand Up @@ -391,6 +397,7 @@ data_tabulate.data.frame <- function(
remove_na = remove_na,
name = i,
verbose = verbose,
chisq = chisq,
...
)
})
Expand Down Expand Up @@ -422,6 +429,7 @@ data_tabulate.grouped_df <- function(
remove_na = FALSE,
collapse = FALSE,
verbose = TRUE,
chisq = FALSE,
...
) {
grps <- attr(x, "groups", exact = TRUE)
Expand Down
42 changes: 40 additions & 2 deletions R/data_xtabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@
remove_na = FALSE,
proportions = NULL,
obj_name = NULL,
group_variable = NULL
group_variable = NULL,
chisq = FALSE
) {
if (is.null(group_variable) && is.null(attr(x, "groups"))) {
chi_square <- chisq.test(x, by, simulate.p.value = TRUE)
} else if (nlevels(x) > 1 && nlevels(x) > 1) {
chi_square <- chisq.test(x, by, simulate.p.value = TRUE)
} else {
chi_square <-
"Both 'by' and 'x' must have at least 2 unique levels\n
in order to calculate chi square."
}

if (!is.null(proportions)) {
proportions <- match.arg(proportions, c("row", "column", "full"))
}
Expand Down Expand Up @@ -97,7 +108,24 @@
if (!is.null(by_name)) {
attr(out, "by") <- by_name
}

attr(out, "chi_square") <- chi_square
if (chisq) {
if (is.list(chi_square)) {
attr(out, "chisq_caption") <- paste0(
chi_square$method,
"\n ",
"X-squared = ",
round(chi_square[["statistic"]], 3),
", ",
"df = ",
chi_square[["parameter"]][["df"]],
", p = ",
round(chi_square$p.value, 3)
)
} else if (is.character(chi_square)) {
attr(out, "chisq_caption") <- chi_square
}
}
attr(out, "grouped_df") <- !is.null(group_variable)
attr(out, "prop_table") <- .prop_table(out)

Expand Down Expand Up @@ -312,6 +340,16 @@ print_html.datawizard_crosstab <- function(x, big_mark = NULL, ...) {
caption <- paste0("Grouped by ", x[["Group"]][1])
x$Group <- NULL
}
if (!is.null(attr(x, "chisq_caption"))) {
caption <- paste0(caption, attr(x, "chisq_caption"))
}
if (!is.null(attr(x, "chi_square_warning"))) {
paste0(
caption,
"\n",
insight::format_warning(attr(x, "chi_square_warning"))
)
}

# this function is used by all four supported format, markdown, text, html
# and tt (tinytable). For tt, we sometimes have format "html" and backend = "tt",
Expand Down
5 changes: 5 additions & 0 deletions man/data_tabulate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions tests/testthat/_snaps/data_tabulate.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,31 @@
5 | 2002107 | 20.02 | 20.02 | 100.00
<NA> | 0 | 0.00 | <NA> | <NA>

# data_tabulate default by with chisq

Code
data_tabulate(x = efc$e16sex, by = efc$c172code, chisq = TRUE)
Output
Pearson's Chi-squared test with simulated p-value
(based on 2000 replicates)
X-squared = 3.88, df = NA, p = 0.163

efc$e16sex | low level of education | intermediate level of education
-----------+------------------------+--------------------------------
1 | 5 | 32
2 | 3 | 34
<NA> | 0 | 0
-----------+------------------------+--------------------------------
Total | 8 | 66

efc$e16sex | high level of education | <NA> | Total
-----------+-------------------------+------+------
1 | 4 | 5 | 46
2 | 12 | 5 | 54
<NA> | 0 | 0 | 0
-----------+-------------------------+------+------
Total | 16 | 10 | 100

# data_tabulate print multiple, collapse

Code
Expand Down Expand Up @@ -861,6 +886,26 @@
|e42dep | 4| 32 (50.8%)| 28 (44.4%)| 3 (4.8%) | 63|
|e42dep | (NA)| 1 (33.3%)| 2 (66.7%)| 0 (0.0%) | 3|

---

Code
display(data_tabulate(efc, "c172code", by = "e16sex", proportions = "column",
remove_na = TRUE, weights = "weights", chisq = TRUE))
Output


Table: Pearson's Chi-squared test with simulated p-value
(based on 2000 replicates)
X-squared = 4.217, df = NA, p = 0.117

|c172code | male| female| Total|
|:--------|----------:|----------:|-----:|
|1 | 5 (12.5%)| 3 (6.5%)| 8|
|2 | 32 (80.0%)| 32 (69.6%)| 64|
|3 | 3 (7.5%)| 11 (23.9%)| 14|
| | | | |
|Total | 40| 46| 86|

# data_tabulate, correct 0% for proportions

Code
Expand Down
Loading
Loading