5.13 Summary of important R commands

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)

    • Makes a stacked bar chart useful for homogeneity test situations.
  • mosaicplot(TABLENAME)

    • Makes a mosaic plot useful for finding patterns in the table in independence test situations.
  • 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)

    • Provides \(X^2\) and p-values based on the \(\boldsymbol{\chi^2}\)-distribution with \((R-1)(C-1)\) degrees of freedom.
  • chisq.test(TABLENAME)$expected

    • Provides expected cell counts.
  • 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

    • Provides \(X^2\) contributions from each cell in table.
  • chisq.test(TABLENAME)$residuals

    • Provides standardized residuals.
  • mosaicplot(TABLENAME, shade=T)

    • Provides a mosaic plot with shading based on standardized residuals.