The main components of R code used in this chapter follow with components
to modify in lighter and/or ALL CAPS text where y
is a response variable and x
is a predictor
are easily identified:
TABLENAME <-
tally(~x +
y, data=DATASETNAME)
This function requires that the mosaic
package has been loaded.
This provides a table of the counts in the variable called
TABLENAME
.
margins=T
is used if want to display row, column, and
table totals.
plot(y~ x, data=DATASETNAME)
mosaicplot(TABLENAME)
tableplot(data=DATASETNAME, sortCol=VARIABLENAME,pals=list(“BrBG”))
Makes a tableplot sorted by VARIABLENAME, requires that the tabplot
and RColorBrewer
packages have been loaded.
The pals=list("BrBG")
option provides a color-blind friendly color palette, although other options are possible, such as pals=list("RdBu")
.
chisq.test(TABLENAME)
chisq.test(TABLENAME)$expected
pchisq(X-SQUARED,
df=(R - 1)*
(C - 1), lower.tail=F)
Provides p-value from \(\boldsymbol{\chi^2}\)-distribution with \((R-1)(C-1)\) degrees of freedom for observed test statistic.
See Section 5.5 for code related to finding a permutation-based p-value.
chisq.test(TABLENAME)$residuals^2
chisq.test(TABLENAME)$residuals
mosaicplot(TABLENAME, shade=T)