coindep_test.RdPerforms a test of (conditional) independence of 2 margins in a contingency table by simulation from the marginal distribution of the input table under (conditional) independence.
a contingency table.
margin index(es) or corresponding name(s) of the conditioning variables. Each resulting conditional table has to be a 2-way table.
number of (conditional) independence tables to be drawn.
aggregation function capturing independence in (each conditional) 2-way table.
aggregation function aggregating the test statistics
computed by indepfun.
a character string specifying the alternative
hypothesis; must be either "greater" (default) or
"less" (and may be abbreviated.)
logical. Should the table of Pearson residuals under
independence be computed and passed to indepfun (default)
or the raw table of observed frequencies?
If margin is NULL this computes a simple independence
statistic in a 2-way table. Alternatively, margin can give
several conditioning variables and then conditional independence in
the resulting conditional table is tested.
By default, this uses a (double) maximum statistic of Pearson residuals.
By changing indepfun or aggfun a (maximum of) Pearson Chi-squared
statistic(s) can be computed or just the usual Pearson Chi-squared statistics
and so on. Other statistics can be computed by changing pearson
to FALSE.
The function uses r2dtable to simulate the distribution
of the test statistic under the null.
A list of class "coindep_test" inheriting from "htest"
with following components:
the value of the test statistic.
the \(p\) value for the test.
a character string indicating the type of the test.
a character string giving the name(s) of the data.
observed table of frequencies
expected table of frequencies
corresponding Pearson residuals
the margin used
a vector of size n with simulated values of the
distribution of the statistic under the null.
the corresponding quantile function (for computing critical values).
the corresponding distribution function (for computing \(p\) values).
library(MASS)
TeaTasting <- matrix(c(3, 1, 1, 3), nrow = 2,
dimnames = list(Guess = c("Milk", "Tea"),
Truth = c("Milk", "Tea"))
)
## compute maximum statistic
coindep_test(TeaTasting)
#>
#> Permutation test for conditional independence
#>
#> data: TeaTasting
#> f(x) = 0.70711, p-value = 0.48
#>
## compute Chi-squared statistic
coindep_test(TeaTasting, indepfun = function(x) sum(x^2))
#>
#> Permutation test for conditional independence
#>
#> data: TeaTasting
#> f(x) = 2, p-value = 0.488
#>
## use unconditional asymptotic distribution
chisq.test(TeaTasting, correct = FALSE)
#> Warning: Chi-squared approximation may be incorrect
#>
#> Pearson's Chi-squared test
#>
#> data: TeaTasting
#> X-squared = 2, df = 1, p-value = 0.1573
#>
chisq.test(TeaTasting)
#> Warning: Chi-squared approximation may be incorrect
#>
#> Pearson's Chi-squared test with Yates' continuity correction
#>
#> data: TeaTasting
#> X-squared = 0.5, df = 1, p-value = 0.4795
#>
data("UCBAdmissions")
## double maximum statistic
coindep_test(UCBAdmissions, margin = "Dept")
#>
#> Permutation test for conditional independence
#>
#> data: UCBAdmissions
#> f(x) = 3.1344, p-value = 0.001
#>
## maximum of Chi-squared statistics
coindep_test(UCBAdmissions, margin = "Dept", indepfun = function(x) sum(x^2))
#>
#> Permutation test for conditional independence
#>
#> data: UCBAdmissions
#> f(x) = 17.248, p-value < 2.2e-16
#>
## Pearson Chi-squared statistic
coindep_test(UCBAdmissions, margin = "Dept", indepfun = function(x) sum(x^2), aggfun = sum)
#>
#> Permutation test for conditional independence
#>
#> data: UCBAdmissions
#> f(x) = 19.938, p-value = 0.001
#>
## use unconditional asymptotic distribution
loglm(~ Dept * (Gender + Admit), data = UCBAdmissions)
#> Call:
#> loglm(formula = ~Dept * (Gender + Admit), data = UCBAdmissions)
#>
#> Statistics:
#> X^2 df P(> X^2)
#> Likelihood Ratio 21.73551 6 0.001351993
#> Pearson 19.93841 6 0.002840164