Contingency Tables
The χ² test of association (not to be confused with the χ² 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.
Example usage
Arguments
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 | TRUE (default) or FALSE, provide χ² |
chiSqCorr | TRUE or FALSE (default), provide χ² with continuity correction |
zProp | TRUE or FALSE (default), provide a z test for differences between two proportions |
likeRat | TRUE or FALSE (default), provide the likelihood ratio |
fisher | TRUE or FALSE (default), provide Fisher's exact test |
contCoef | TRUE or FALSE (default), provide the contingency coefficient |
phiCra | TRUE or FALSE (default), provide Phi and Cramer's V |
diffProp | TRUE or FALSE (default), provide the differences in proportions (only available for 2x2 tables) |
logOdds | TRUE or FALSE (default), provide the log odds ratio (only available for 2x2 tables) |
odds | TRUE or FALSE (default), provide the odds ratio (only available for 2x2 tables) |
relRisk | TRUE or FALSE (default), provide the relative risk (only available for 2x2 tables) |
ci | TRUE or FALSE (default), provide confidence intervals for the comparative measures |
ciWidth | a number between 50 and 99.9 (default: 95), width of the confidence intervals to provide |
compare | columns or rows (default), compare columns/rows in difference of proportions or relative risks (2x2 tables) |
hypothesis | 'different' (default), 'oneGreater' or 'twoGreater', the alternative hypothesis; group 1 different to group 2, group 1 greater than group 2, and group 2 greater than group 1 respectively |
gamma | TRUE or FALSE (default), provide gamma |
taub | TRUE or FALSE (default), provide Kendall's tau-b |
mh | TRUE or FALSE (default), provide Mantel-Haenszel test for trend |
obs | TRUE or FALSE (default), provide the observed counts |
exp | TRUE or FALSE (default), provide the expected counts |
pcRow | TRUE or FALSE (default), provide row percentages |
pcCol | TRUE or FALSE (default), provide column percentages |
pcTot | TRUE or FALSE (default), provide total percentages |
barplot | TRUE or FALSE (default), show barplots |
yaxis | ycounts (default) or ypc. Use respectively counts or percentages for the bar plot y-axis |
yaxisPc | total_pc (default), column_pc, or row_pc. Use respectively percentages of total, within columns, or within rows for the bar plot y-axis. |
xaxis | rows (default), or columns in bar plot X axis |
bartype | stack or side by side (default), barplot type |
Returns
A results object containing:
results$freqs | a table |
results$chiSq | a table |
results$odds | a table |
results$nom | a table |
results$gamma | a table |
results$taub | a table |
results$mh | a table |
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)