Skip to main content
Statistics LibreTexts

2.12: Summary of important R code

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

    2.12 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, remembering that any R packages mentioned need to be installed and loaded for this code to have a chance of working:

    • summary(DATASETNAME)
      • Provides numerical summaries of all variables in the data set.
    • summary(lm(Y ~ X, data = DATASETNAME))
      • Provides estimate, SE, test statistic, and p-value for difference in second row of coefficient table.
    • confint(lm(Y ~ X, data = DATASETNAME), level = 0.95)
      • Provides 95% confidence interval for difference in second row of output.
    • 2*pt(abs(Tobs), df = DF, lower.tail = F)
      • Finds the two-sided test p-value for an observed 2-sample t-test statistic of Tobs.
    • hist(DATASETNAME$Y)
      • Makes a histogram of a variable named Y from the data set of interest.
    • boxplot(Y ~ X, data = DATASETNAME)
      • Makes a boxplot of a variable named Y for groups in X from the data set.
    • pirateplot(Y ~ X, data = DATASETNAME, inf.method = “ci”, inf.disp = “line”)
      • Requires the yarrr package is loaded.
      • Makes a pirate-plot of a variable named Y for groups in X from the data set with estimated means and 95% confidence intervals for each group.
      • Add theme = 2 if the confidence intervals extend outside the density curves and you can’t see how far they extend.
    • mean(Y ~ X, data = DATASETNAME); sd(Y ~ X, data = DATASETNAME)
      • This usage of mean and sd requires the mosaic package.
      • Provides the mean and sd of responses of Y for each group described in X.
    • favstats(Y ~ X, data = DATASETNAME)
      • Provides numerical summaries of Y by groups described in X.
    • Tobs <- coef(lm(Y ~ X, data = DATASETNAME))[2]; Tobs
      B <- 1000
      Tstar <- matrix(NA, nrow = B)
      for (b in (1:B)){
      lmP <- lm(Y ~ shuffle(X), data = DATASETNAME)
      Tstar[b] <- coef(lmP)[2]
      }

      • Code to run a for loop to generate 1000 permuted versions of the test statistic using the shuffle function and keep track of the results in Tstar
    • pdata(Tstar, abs(Tobs), lower.tail = F)
      • Finds the proportion of the permuted test statistics in Tstar that are less than -|Tobs| or greater than |Tobs|, useful for finding the two-sided test p-value.
    • Tobs <- coef(lm(Y ~ X, data = DATASETNAME))[2]; Tobs
      B <- 1000
      Tstar <- matrix(NA, nrow = B)
      for (b in (1:B)){
      lmP <- lm(Y ~ X, data = resample(DATASETNAME))
      Tstar[b] <- coef(lmP)[2]
      }

      • Code to run a for loop to generate 1000 bootstrapped versions of the data set using the resample function and keep track of the results of the statistic in Tstar.
    • qdata(Tstar, c(0.025, 0.975))
      • Provides the values that delineate the middle 95% of the results in the bootstrap distribution (Tstar).

    This page titled 2.12: 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?