Skip to main content
Statistics LibreTexts

5.13: Summary of important R code

  • Page ID
    33261
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    5.13 Summary of important R code

    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 you 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.

    This page titled 5.13: Summary of important R code is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?