Skip to contents

Computes the Phi coefficient as a measure of association between two categorical variables.

Usage

phi(x, y = NULL, ...)

Arguments

x

a vector of categorical data or a contingency table (matrix or table)

y

optional second categorical vector. If provided, a contingency table is constructed from x and y.

...

additional arguments passed to base::table()

Value

a numeric scalar containing the Phi coefficient

Details

If y is provided, a contingency table is created using table(x, y, ...). Otherwise, x is assumed to already be a contingency table.

Note: Yates' continuity correction is not applied when computing the chi-squared statistic.

The Phi coefficient is defined as: $$ \phi = \sqrt{ \frac{\chi^2}{n} } $$ where \(\chi^2\) is the chi-squared test statistic and \(n\) is the total sample size.

This definition is unsigned. For a 2x2 table the signed coefficient \((n_{11} n_{22} - n_{12} n_{21}) / \sqrt{n_{1\cdot} n_{2\cdot} n_{\cdot 1} n_{\cdot 2}}\) equals the Pearson correlation of the two 0/1 indicators and lies in \([-1, 1]\); the value returned here is its absolute value, so the direction of the association is not reported. See pearsonCor() if the sign is needed.

For contingency tables larger than 2x2, Phi is not bounded by 1 and may exceed 1. In such cases, cramerV() is usually preferred.

Examples

# Example with vectors
x <- c("A", "A", "B", "B")
y <- c("yes", "no", "yes", "no")
phi(x, y)
#> [1] 0

# Example with contingency table. Note that the signed coefficient is
# -0.0891 here: phi() reports the magnitude only.
tab <- matrix(c(10, 20, 30, 40), nrow = 2)
phi(tab)
#> [1] 0.08908708