Skip to main content
Statistics LibreTexts

6.13: Summary of important R code

  • Page ID
    33276
  • \( \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}}\)

    The main components of the R code used in this chapter follow with the components to modify in lighter and/or ALL CAPS text where y is a response variable, x is an explanatory variable, and the data are in DATASETNAME.

    • DATASETNAME %>% ggpairs()
      • Requires the GGally package.
      • Makes a scatterplot matrix that also displays the correlation coefficients.
    • cor(y ~ x, data = DATASETNAME)
      • Provides the estimated correlation coefficient between \(x\) and \(y\).
    • plot(y ~ x, data = DATASETNAME)
      • Provides a base R scatter plot.
    • DATASETNAME %>% ggplot(mapping = aes(x = x, y = y) +
      geom_point() +
      geom_smooth(method = “lm”)

      • Provides a scatter plot with a regression line.
      • Add color = groupfactor to the aes() to color points and get regression lines based on a grouping (categorical) variable.
      • Add + geom_smooth(se = F, lty = 2) to add a smoothing line to the scatterplot as a dashed line.
    • MODELNAME <- lm(y ~ x, data = DATASETNAME)
      • Estimates a regression model using least squares.
    • summary(MODELNAME)
      • Provides parameter estimates and R-squared (used heavily in Chapter 7 and 8 as well).
    • par(mfrow = c(2, 2)); plot(MODELNAME)
      • Provides four regression diagnostic plots in one plot.

    This page titled 6.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?