contTables | R Documentation |
The X² test of association (not to be confused with the X² goodness of fit) is used to test whether two categorical variables are independent or associated. If the p-value is low, it suggests the variables are not independent, and that there is a relationship between the two variables.
contTables(data, rows, cols, counts = NULL, layers = NULL,
chiSq = TRUE, chiSqCorr = FALSE, zProp = FALSE, likeRat = FALSE,
fisher = FALSE, contCoef = FALSE, phiCra = FALSE,
diffProp = FALSE, logOdds = FALSE, odds = FALSE, relRisk = FALSE,
ci = TRUE, ciWidth = 95, compare = "rows",
hypothesis = "different", gamma = FALSE, taub = FALSE,
mh = FALSE, obs = TRUE, exp = FALSE, pcRow = FALSE,
pcCol = FALSE, pcTot = FALSE, barplot = FALSE, yaxis = "ycounts",
yaxisPc = "total_pc", xaxis = "xrows", bartype = "dodge", formula)
data |
the data as a data frame |
rows |
the variable to use as the rows in the contingency table (not necessary when providing a formula, see the examples) |
cols |
the variable to use as the columns in the contingency table (not necessary when providing a formula, see the examples) |
counts |
the variable to use as the counts in the contingency table (not necessary when providing a formula, see the examples) |
layers |
the variables to use to split the contingency table (not necessary when providing a formula, see the examples) |
chiSq |
|
chiSqCorr |
|
zProp |
|
likeRat |
|
fisher |
|
contCoef |
|
phiCra |
|
diffProp |
|
logOdds |
|
odds |
|
relRisk |
|
ci |
|
ciWidth |
a number between 50 and 99.9 (default: 95), width of the confidence intervals to provide |
compare |
|
hypothesis |
|
gamma |
|
taub |
|
mh |
|
obs |
|
exp |
|
pcRow |
|
pcCol |
|
pcTot |
|
barplot |
|
yaxis |
ycounts (default) or ypc. Use respectively |
yaxisPc |
total_pc (default), column_pc, or row_pc. Use respectively
percentages |
xaxis |
rows (default), or columns in bar plot X axis |
bartype |
stack or side by side (default), barplot type |
formula |
(optional) the formula to use, see the examples |
A results object containing:
results$freqs | a table of proportions | ||||
results$chiSq | a table of X² test results | ||||
results$odds | a table of comparative measures | ||||
results$nom | a table of the 'nominal' test results | ||||
results$gamma | a table of the gamma test results | ||||
results$taub | a table of the Kendall's tau-b test results | ||||
results$mh | a table of the Mantel-Haenszel test for trend | ||||
results$barplot | an image | ||||
Tables can be converted to data frames with asDF
or as.data.frame
. For example:
results$freqs$asDF
as.data.frame(results$freqs)
data('HairEyeColor')
dat <- as.data.frame(HairEyeColor)
contTables(formula = Freq ~ Hair:Eye, dat)
#
# CONTINGENCY TABLES
#
# Contingency Tables
# -----------------------------------------------------
# Hair Brown Blue Hazel Green Total
# -----------------------------------------------------
# Black 68 20 15 5 108
# Brown 119 84 54 29 286
# Red 26 17 14 14 71
# Blond 7 94 10 16 127
# Total 220 215 93 64 592
# -----------------------------------------------------
#
#
# X² Tests
# -------------------------------
# Value df p
# -------------------------------
# X² 138 9 < .001
# N 592
# -------------------------------
#
# Alternatively, omit the left of the formula (`Freq`) if each row
# represents a single observation:
contTables(formula = ~ Hair:Eye, dat)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.