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.
Usage
contTables(
data,
rows,
cols,
counts = NULL,
layers = NULL,
chiSq = TRUE,
chiSqCorr = FALSE,
likeRat = FALSE,
fisher = FALSE,
contCoef = FALSE,
phiCra = FALSE,
logOdds = FALSE,
odds = FALSE,
relRisk = FALSE,
riskDiff = FALSE,
nnt = FALSE,
ci = FALSE,
ciWidth = 95,
gamma = FALSE,
taub = FALSE,
trendTest = FALSE,
trendDirection = "twosided",
obs = TRUE,
exp = FALSE,
pcRow = FALSE,
pcCol = FALSE,
pcTot = FALSE,
formula
)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) orFALSE, provide X²- chiSqCorr
TRUEorFALSE(default), provide X² with continuity correction- likeRat
TRUEorFALSE(default), provide the likelihood ratio- fisher
TRUEorFALSE(default), provide Fisher's exact test- contCoef
TRUEorFALSE(default), provide the contingency coefficient- phiCra
TRUEorFALSE(default), provide Phi and Cramer's V- logOdds
TRUEorFALSE(default), provide the log odds ratio (only available for 2x2 tables)- odds
TRUEorFALSE(default), provide the odds ratio (only available for 2x2 tables)- relRisk
TRUEorFALSE(default), provide the relative risk (only available for 2x2 tables)- riskDiff
TRUEorFALSE(default), provide the risk difference (only available for 2x2 tables)- nnt
TRUEorFALSE(default), provide the number needed to treat (only available for 2x2 tables)- ci
TRUEorFALSE(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
- gamma
TRUEorFALSE(default), provide gamma- taub
TRUEorFALSE(default), provide Kendall's tau-b- trendTest
TRUEorFALSE(default), provide Cochran-Armitage test for trend for ordered categorical variables- trendDirection
Direction of trend test: two-sided (default), increasing, or decreasing
- obs
TRUEorFALSE(default), provide the observed counts- exp
TRUEorFALSE(default), provide the expected counts- pcRow
TRUEorFALSE(default), provide row percentages- pcCol
TRUEorFALSE(default), provide column percentages- pcTot
TRUEorFALSE(default), provide total percentages- formula
(optional) the formula to use, see the examples
Value
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$trendTest | Cochran-Armitage trend test results |
Tables can be converted to data frames with asDF or as.data.frame. For example:
results$freqs$asDF
as.data.frame(results$freqs)