Appendix C: R Reference MATH 4210 -- setup and every R function used, by chapter
This appendix is a lookup table, not a tutorial. Use it to install R and the course
packages once, to find the one-line syntax for reading a course dataset, and to look up any
R function you saw in a chapter but forgot how it works. Functions are grouped by the
chapter that introduces them; a function used again in a later chapter is not repeated,
so if you cannot find something under “your” chapter, check an earlier one.
Installing R for this course ¶ The course was built and checked against R 4.6.0 . Download it from
CRAN for your operating system, then install
RStudio Desktop as your editor; it is not
required, but it makes running the chapter code much easier.
Every dataset in this book loads with base R alone (read.csv), but several chapters use
add-on packages for diagnostics, model selection, and shrinkage. Install all of them once,
before you start Chapter 2:
install.packages(c(
"faraway", "car", "leaps", "lmtest", "ggplot2", "broom",
"glmnet", "ellipse", "dplyr", "MASS", "boot", "carData", "HistData"
))MASS and boot ship with R itself, so install.packages will report they are already
present; the line above is safe to run regardless. Load a package in any R session with
library(packagename) before using its functions; the chapter tables below note which
package each function comes from when it is not base R.
Reading course data ¶ Every dataset used in this book lives in data/ as a plain CSV, one row per observation,
with column headers already set to the names used in the chapters. Read any of them with
base R’s read.csv, giving the path relative to the book’s root folder:
toluca <- read.csv("data/toluca.csv")
head(toluca) lotsize hours
1 80 399
2 30 121
3 50 221
4 90 376
5 70 361
6 60 224All chapter R code assumes your working directory is the book’s root folder (the one that
contains data/), so every read.csv call in the book uses a path like "data/toluca.csv",
never a full file-system path. In RStudio, open the project at the book root, or set it
by hand with setwd("path/to/book-myst") before running any chapter’s code. See
Appendix E: The Datasets of This Book for the story, provenance, and variable list of every dataset.
How to read the tables below ¶ Each row gives the function, the package it comes from (blank means base R, already loaded
in any session), a one-line description of what it does, and a minimal example. Functions
are listed once, in the chapter where the book first uses them; later chapters build on
these without repeating the entry. library(...) itself is not tabulated; load whichever
package a function’s row names before calling it.
Chapter 1: Regression as a way of thinking ¶ Function Package Purpose Example read.csvbase Read a CSV file into a data frame. d <- read.csv("data/toluca.csv")lmbase Fit a linear model by least squares. fit <- lm(hours ~ lotsize, data = d)coefbase Extract fitted coefficients from a model. coef(fit)predictbase Compute fitted or predicted values from a model. predict(fit)meanbase Arithmetic mean of a vector. mean(d$hours)minbase Smallest value in a vector. min(d$lotsize)sumbase Sum of a vector’s entries. sum(d$hours)nrowbase Number of rows in a data frame. nrow(d)corbase Pearson correlation between two vectors. cor(d$lotsize, d$hours)varbase Sample variance of a vector. var(d$hours)sapplybase Apply a function to each element of a list or vector, simplifying the result. sapply(d, class)roundbase Round a number to a given number of decimal places. round(3.14159, 2)plotbase Draw a scatterplot or other base-R plot. plot(d$lotsize, d$hours)ablinebase Add a straight line to an existing plot. abline(fit)parbase Get or set plotting parameters (layout, margins). par(mfrow = c(1, 2))data.framebase Build a data frame from named vectors. data.frame(x = 1:3, y = 4:6)
Chapter 2: Simple linear regression ¶ Function Package Purpose Example residualsbase Extract residuals e i = Y i − Y ^ i e_i = Y_i - \hat{Y}_i e i = Y i − Y ^ i from a fitted model. residuals(fit)fittedbase Extract fitted values Y ^ i \hat{Y}_i Y ^ i from a fitted model. fitted(fit)summarybase Print a model’s coefficients, standard errors, t t t tests, and R 2 R^2 R 2 . summary(fit)sqrtbase Square root. sqrt(25)set.seedbase Fix the random-number generator’s starting point for reproducibility. set.seed(4210)replicatebase Repeat an expression a fixed number of times, collecting the results. replicate(1000, mean(rnorm(10)))rnormbase Draw random values from a Normal distribution. rnorm(10, mean = 0, sd = 1)sdbase Sample standard deviation of a vector. sd(d$hours)
Chapter 3: Inference for simple linear regression ¶ Function Package Purpose Example confintbase Confidence intervals for a model’s coefficients. confint(fit, level = 0.95)anovabase Analysis-of-variance table for a fitted model. anova(fit)qtbase Quantile (inverse CDF) of the t t t distribution. qt(0.975, df = 23)qfbase Quantile (inverse CDF) of the F F F distribution. qf(0.95, df1 = 1, df2 = 23)
Chapter 4: Correlation ¶ Function Package Purpose Example cor.testbase Hypothesis test and confidence interval for a correlation. cor.test(d$lotsize, d$hours)atanhbase Inverse hyperbolic tangent; the Fisher z z z transform of a correlation. atanh(0.86)tanhbase Hyperbolic tangent; inverts the Fisher z z z transform. tanh(1.29)qnormbase Quantile (inverse CDF) of the standard Normal distribution. qnorm(0.975)aggregatebase Compute a summary statistic for each group in a data frame. aggregate(hours ~ lotsize, data = d, FUN = mean)which.maxbase Index of the largest value in a vector. which.max(d$hours)
Chapter 5: Randomization and bootstrap inference for regression ¶ Function Package Purpose Example samplebase Draw a random sample (with or without replacement) from a vector. sample(d$hours, size = 25, replace = TRUE)numericbase Create a numeric vector of a given length, filled with zeros. numeric(1000)quantilebase Sample quantiles of a vector (used for bootstrap percentile intervals). quantile(boot_slopes, c(0.025, 0.975))absbase Absolute value. abs(-3.5)maxbase Largest value in a vector. max(d$hours)lengthbase Number of elements in a vector. length(d$hours)cbase Combine values into a vector. c(1, 2, 3)
Chapter 6: Matrix algebra for regression ¶ Function Package Purpose Example dimbase Dimensions (rows, columns) of a matrix or data frame. dim(X)headbase First few rows of a data frame or matrix. head(d)ncolbase Number of columns in a matrix or data frame. ncol(X)cbindbase Bind vectors or matrices together as columns. X <- cbind(1, d$lotsize)colnamesbase Get or set a matrix’s column names. colnames(X) <- c("intercept", "lotsize")matrixbase Build a matrix from a vector, given its dimensions. matrix(1:6, nrow = 2)tbase Transpose a matrix. t(X)%*%base Matrix multiplication. t(X) %*% XisSymmetricbase Test whether a matrix equals its own transpose. isSymmetric(t(X) %*% X)qrbase QR decomposition of a matrix (used internally by lm, and for rank checks). qr(X)$rankdetbase Determinant of a square matrix. det(t(X) %*% X)solvebase Matrix inverse, or solve a linear system A x = b \mathbf{A}\mathbf{x} = \mathbf{b} Ax = b . solve(t(X) %*% X)diagbase Extract or construct a diagonal matrix. diag(3)as.numericbase Coerce an object to a plain numeric vector. as.numeric(coef(fit))cholbase Cholesky decomposition of a symmetric positive-definite matrix. chol(t(X) %*% X)covbase Sample covariance matrix of a data frame or matrix. cov(d)
Chapter 7: The general linear model ¶ Function Package Purpose Example all.equalbase Test near-equality of two numeric objects, up to rounding error. all.equal(coef(fit), b_hand)
Chapter 8: Multiple regression in practice ¶ Function Package Purpose Example residbase Extract residuals from a fitted model (identical to residuals). resid(fit)scalebase Center and/or scale the columns of a matrix or data frame. scale(d[, c("triceps", "thigh")])as.data.framebase Coerce an object (e.g. a matrix) to a data frame. as.data.frame(scale(d))deviancebase Residual sum of squares of a fitted model. deviance(fit)
Chapter 9: Model diagnostics ¶ Function Package Purpose Example hatvaluesbase Leverage values h i i h_{ii} h ii , the diagonal of the hat matrix. hatvalues(fit)rstandardbase Standardized (internally studentized) residuals. rstandard(fit)rstudentbase Studentized deleted (externally studentized) residuals. rstudent(fit)cooks.distancebase Cook’s distance, an overall influence measure for each observation. cooks.distance(fit)dffitsbase DFFITS, the standardized change in a fitted value when one case is deleted. dffits(fit)dfbetasbase DFBETAS, the standardized change in each coefficient when one case is deleted. dfbetas(fit)factorbase Convert a variable to a categorical factor. d$grp <- factor(d$grp)bptestlmtest Breusch-Pagan test for nonconstant error variance. bptest(fit)shapiro.testbase Shapiro-Wilk test for Normality of a sample. shapiro.test(residuals(fit))dwtestlmtest Durbin-Watson test for autocorrelated residuals. dwtest(fit)librarybase Load an installed package into the current session. library(lmtest)sortbase Sort a vector in increasing (or decreasing) order. sort(cooks.distance(fit))whichbase Indices of the TRUE entries of a logical vector. which(hatvalues(fit) > 0.5)rangebase Minimum and maximum of a vector, as a length-2 vector. range(d$hours)tapplybase Apply a function to a vector, grouped by a factor. tapply(d$hours, d$grp, mean)pfbase Cumulative distribution function of the F F F distribution. pf(4.2, df1 = 1, df2 = 23, lower.tail = FALSE)
Function Package Purpose Example logbase Natural logarithm (base e e e ). log(d$body)boxcoxMASS Profile log-likelihood plot for the Box-Cox family of power transformations. MASS::boxcox(fit)qchisqbase Quantile (inverse CDF) of the chi-squared distribution. qchisq(0.95, df = 1)rlmMASS Fit a robust regression by iteratively reweighted least squares, down-weighting outliers. MASS::rlm(y ~ x, data = d)rbindbase Bind vectors or data frames together as rows. rbind(row1, row2)
Chapter 11: Categorical predictors and interactions ¶ Function Package Purpose Example relevelbase Change which level of a factor is the reference (baseline) level. d$rank <- relevel(d$rank, ref = "AsstProf")model.matrixbase Build the design matrix X \mathbf{X} X a model uses, including dummy columns. model.matrix(fit)Ibase Protect an expression from formula notation, so it is used as-is. lm(y ~ x + I(x^2), data = d)pmaxbase Elementwise maximum across two or more vectors. pmax(0, x - 5)expand.gridbase Build a data frame of every combination of the given values. expand.grid(x = 1:3, grp = c("a", "b"))tablebase Cross-tabulate one or more factors. table(d$rank, d$sex)
Chapter 12: Multicollinearity, variable selection, and validation ¶ Function Package Purpose Example vifcar Variance inflation factors, a multicollinearity diagnostic. car::vif(fit)regsubsetsleaps Best-subsets search across candidate predictors. leaps::regsubsets(y ~ ., data = d)AICbase Akaike information criterion of a fitted model. AIC(fit)BICbase Bayesian (Schwarz) information criterion of a fitted model. BIC(fit)orderbase Indices that would sort a vector. order(d$hours)formulabase Extract or build a model formula object. formula(fit)glmnetglmnet Fit a ridge, lasso, or elastic-net penalized regression path. glmnet::glmnet(as.matrix(X), y, alpha = 1)cv.glmnetglmnet Cross-validated glmnet fit, chooses the penalty by held-out error. glmnet::cv.glmnet(as.matrix(X), y, alpha = 1)as.matrixbase Coerce a data frame to a plain numeric matrix. as.matrix(d[, c("x1", "x2")])
Chapter 13: Logistic regression ¶ Function Package Purpose Example glmbase Fit a generalized linear model (here, with family = binomial). glm(damage_bin ~ temp, data = d, family = binomial)confint.defaultbase Wald (asymptotic Normal) confidence intervals for a glm’s coefficients. confint.default(fit)drop1base Compare a model to each of its one-term-simpler versions (likelihood-ratio tests). drop1(fit, test = "Chisq")expbase Exponential function; converts a log-odds coefficient to an odds ratio. exp(coef(fit))pchisqbase Cumulative distribution function of the chi-squared distribution. pchisq(5.2, df = 1, lower.tail = FALSE)outerbase Outer product: apply a function to every pair from two vectors. outer(1:3, 1:3, "+")cutbase Cut a numeric vector into labeled bins (used to build calibration groups). cut(fitted(fit), breaks = 5)
Chapter 14: Poisson regression and the GLM idea ¶ Function Package Purpose Example poissonbase Family object for Poisson glm fits (log link by default). glm(count ~ x, data = d, family = poisson)quasipoissonbase Family object for overdispersed count data; same mean model, inflated variance. glm(count ~ x, data = d, family = quasipoisson)gaussianbase Family object for glm fits with a Normal response (recovers ordinary lm). glm(y ~ x, data = d, family = gaussian)df.residualbase Residual degrees of freedom of a fitted model. df.residual(fit)glm.nbMASS Negative-binomial regression, for overdispersed counts. MASS::glm.nb(count ~ x, data = d)
Chapter 15: Regression with time: autocorrelation and forecasting ¶ Function Package Purpose Example seq_lenbase Generate the sequence 1, 2, ..., n. seq_len(nrow(d))diffbase Lagged differences of a vector. diff(d$passengers)subsetbase Filter rows of a data frame by a logical condition. subset(d, year >= 1955)
Chapter 16: Path analysis and a look ahead ¶ No new functions beyond those already tabulated in earlier chapters; Chapter 16 reuses
lm, coef, cor, scale, cooks.distance, factor, and relevel to build a path
model from a sequence of ordinary regressions.
A double colon, as in MASS::boxcox(fit), calls a function from a specific package
without first running library(MASS). Both styles work once the package is installed;
the book uses package::function the first time a function appears in a chapter, so you
can see at a glance which package it needs, and the bare function name afterward.
See Appendix D: Python Reference for the equivalent lookup table in Python, and Appendix E: The Datasets of This Book
for full documentation of every dataset named in the examples above.