Performs chi-squared tests, including goodness-of-fit, homogeneity and independence tests.
chisq_test(
x,
y = NULL,
correct = TRUE,
p = rep(1/length(x), length(x)),
rescale.p = FALSE,
simulate.p.value = FALSE,
B = 2000
)
pairwise_chisq_gof_test(x, p.adjust.method = "holm", ...)
pairwise_chisq_test_against_p(
x,
p = rep(1/length(x), length(x)),
p.adjust.method = "holm",
...
)
chisq_descriptives(res.chisq)
expected_freq(res.chisq)
observed_freq(res.chisq)
pearson_residuals(res.chisq)
std_residuals(res.chisq)
a numeric vector or matrix. x
and y
can also
both be factors.
a numeric vector; ignored if x
is a matrix. If
x
is a factor, y
should be a factor of the same length.
a logical indicating whether to apply continuity
correction when computing the test statistic for 2 by 2 tables: one
half is subtracted from all \(|O - E|\) differences; however, the
correction will not be bigger than the differences themselves. No correction
is done if simulate.p.value = TRUE
.
a vector of probabilities of the same length of x
.
An error is given if any entry of p
is negative.
a logical scalar; if TRUE then p
is rescaled
(if necessary) to sum to 1. If rescale.p
is FALSE, and
p
does not sum to 1, an error is given.
a logical indicating whether to compute p-values by Monte Carlo simulation.
an integer specifying the number of replicates used in the Monte Carlo test.
method to adjust p values for multiple comparisons. Used when pairwise comparisons are performed. Allowed values include "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none". If you don't want to adjust the p value (not recommended), use p.adjust.method = "none".
other arguments passed to the function {chisq_test}()
.
an object of class chisq_test
.
return a data frame with some the following columns:
n
: the number of participants.
group, group1, group2
:
the categories or groups being compared.
statistic
: the value
of Pearson's chi-squared test statistic.
df
: the degrees of
freedom of the approximate chi-squared distribution of the test statistic.
NA if the p-value is computed by Monte Carlo simulation.
p
:
p-value.
p.adj
: the adjusted p-value.
method
: the
used statistical test.
p.signif, p.adj.signif
: the significance
level of p-values and adjusted p-values, respectively.
observed
: observed counts.
expected
: the expected counts under the null hypothesis.
The returned object has an attribute called args, which is a list holding the test arguments.
chisq_test()
: performs chi-square tests including goodness-of-fit,
homogeneity and independence tests.
pairwise_chisq_gof_test()
: perform pairwise comparisons between groups following a global
chi-square goodness of fit test.
pairwise_chisq_test_against_p()
: perform pairwise comparisons after a global
chi-squared test for given probabilities. For each group, the observed and
the expected proportions are shown. Each group is compared to the sum of
all others.
chisq_descriptives()
: returns the descriptive statistics of the chi-square
test. These include, observed and expected frequencies, proportions,
residuals and standardized residuals.
expected_freq()
: returns the expected counts from the chi-square test result.
observed_freq()
: returns the observed counts from the chi-square test result.
pearson_residuals()
: returns the Pearson residuals, (observed - expected) / sqrt(expected)
.
std_residuals()
: returns the standardized residuals
# Chi-square goodness of fit test
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tulip <- c(red = 81, yellow = 50, white = 27)
# Q1: Are the colors equally common?
chisq_test(tulip)
#> # A tibble: 1 × 6
#> n statistic p df method p.signif
#> * <int> <dbl> <dbl> <dbl> <chr> <chr>
#> 1 3 27.9 0.00000088 2 Chi-square test ****
pairwise_chisq_gof_test(tulip)
#> # A tibble: 3 × 8
#> n group1 group2 statistic p df p.adj p.adj.signif
#> * <int> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr>
#> 1 2 red yellow 7.34 0.00676 1 0.0135 *
#> 2 2 red white 27 0.000000203 1 0.000000609 ****
#> 3 2 yellow white 6.87 0.00876 1 0.0135 *
# Q2: comparing observed to expected proportions
chisq_test(tulip, p = c(1/2, 1/3, 1/6))
#> # A tibble: 1 × 6
#> n statistic p df method p.signif
#> * <int> <dbl> <dbl> <dbl> <chr> <chr>
#> 1 3 0.203 0.904 2 Chi-square test ns
pairwise_chisq_test_against_p(tulip, p = c(0.5, 0.33, 0.17))
#> # A tibble: 3 × 9
#> group observed expected n statistic p df p.adj p.adj.signif
#> * <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <chr>
#> 1 red 81 79 2 0.101 0.75 1 1 ns
#> 2 yellow 50 52.1 2 0.131 0.717 1 1 ns
#> 3 white 27 26.9 2 0.000879 0.976 1 1 ns
# Homogeneity of proportions between groups
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Data: Titanic
xtab <- as.table(rbind(
c(203, 118, 178, 212),
c(122, 167, 528, 673)
))
dimnames(xtab) <- list(
Survived = c("Yes", "No"),
Class = c("1st", "2nd", "3rd", "Crew")
)
xtab
#> Class
#> Survived 1st 2nd 3rd Crew
#> Yes 203 118 178 212
#> No 122 167 528 673
# Chi-square test
chisq_test(xtab)
#> # A tibble: 1 × 6
#> n statistic p df method p.signif
#> * <dbl> <dbl> <dbl> <int> <chr> <chr>
#> 1 2201 190. 5e-41 3 Chi-square test ****
# Compare the proportion of survived between groups
pairwise_prop_test(xtab)
#> # A tibble: 6 × 5
#> group1 group2 p p.adj p.adj.signif
#> * <chr> <chr> <dbl> <dbl> <chr>
#> 1 1st 2nd 3.13e- 7 9.38e- 7 ****
#> 2 1st 3rd 2.55e-30 1.27e-29 ****
#> 3 2nd 3rd 6.9 e- 7 1.38e- 6 ****
#> 4 1st Crew 1.62e-35 9.73e-35 ****
#> 5 2nd Crew 1.94e- 8 7.75e- 8 ****
#> 6 3rd Crew 6.03e- 1 6.03e- 1 ns