R/phi.R
, R/xtab_statistics.R
xtab_statistics.Rd
This function calculates various measure of association for contingency tables and returns the statistic and p-value. Supported measures are Cramer's V, Phi, Spearman's rho, Kendall's tau and Pearson's r.
phi(tab) cramer(tab) xtab_statistics(data, x1 = NULL, x2 = NULL, statistics = c("auto", "cramer", "phi", "spearman", "kendall", "pearson"), ...)
tab | A |
---|---|
data | A data frame or a table object. If a table object, |
x1 | Name of first variable that should be used to compute the
contingency table. If |
x2 | Name of second variable that should be used to compute the
contingency table. If |
statistics | Name of measure of association that should be computed. May
be one of |
... | Other arguments, passed down to the statistic functions
|
For phi()
, the table's Phi value. For cramer()
, the
table's Cramer's V.
For xtab_statistics()
, a list with following components:
estimate
the value of the estimated measure of association.
p.value
the p-value for the test.
statistic
the value of the test statistic.
stat.name
the name of the test statistic.
stat.html
if applicable, the name of the test statistic, in HTML-format.
df
the degrees of freedom for the contingency table.
method
character string indicating the name of the measure of association.
method.html
if applicable, the name of the measure of association, in HTML-format.
method.short
the short form of association measure, equals the statistics
-aergument.
fisher
logical, if Fisher's exact test was used to calculate the p-value.
The p-value for Cramer's V and the Phi coefficient are based
on chisq.test()
. If any expected value of a table cell is
smaller than 5, or smaller than 10 and the df is 1, then fisher.test()
is used to compute the p-value. The test statistic is calculated
with cramer()
resp. phi()
.
Both test statistic and p-value for Spearman's rho, Kendall's tau
and Pearson's r are calculated with cor.test()
.
When statistics = "auto"
, only Cramer's V or Phi are calculated,
based on the dimension of the table (i.e. if the table has more than
two rows or columns, Cramer's V is calculated, else Phi).
# Phi coefficient for 2x2 tables tab <- table(sample(1:2, 30, TRUE), sample(1:2, 30, TRUE)) phi(tab)#> [1] 0.008988968# Cramer's V for nominal variables with more than 2 categories tab <- table(sample(1:2, 30, TRUE), sample(1:3, 30, TRUE)) cramer(tab)#> [1] 0.2915476data(efc) # 2x2 table, compute Phi automatically xtab_statistics(efc, e16sex, c161sex)#> #> # Measure of Association for Contingency Tables #> #> Chi-squared: 2.2327 #> Phi: 0.0526 #> p-value: 0.1351# more dimensions than 2x2, compute Cramer's V automatically xtab_statistics(efc, c172code, c161sex)#> #> # Measure of Association for Contingency Tables #> #> Chi-squared: 4.1085 #> Cramer's V: 0.0699 #> p-value: 0.1282# ordinal data, use Kendall's tau xtab_statistics(efc, e42dep, quol_5, statistics = "kendall")#> #> # Measure of Association for Contingency Tables #> #> z: -9.5951 #> Kendall's tau: -0.2496 #> p-value: <0.001# calcilate Spearman's rho, with continuity correction xtab_statistics(efc, e42dep, quol_5, statistics = "spearman", exact = FALSE, continuity = TRUE )#> #> # Measure of Association for Contingency Tables #> #> S: 157974157.4198 #> Spearman's rho: -0.3177 #> p-value: <0.001