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”)

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

  • 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)[[1]]

    • 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).