# 7.9: Summary of important R code

$$\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}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

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 %>% ggplot(mapping = aes(x = x, y = y)) + geom_point() + geom_smooth(method = “lm”)
• Provides a scatter plot with a regression line.
• Add + geom_smooth() to add a smoothing line to help detect nonlinear relationships.
• 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 8 as well).
• par(mfrow = c(2, 2)); plot(MODELNAME)
• Provides four regression diagnostic plots in one plot.
• confint(MODELNAME, level = 0.95)
• Provides 95% confidence intervals for the regression model coefficients.
• Change level if you want other confidence levels.
• plot(allEffects(MODELNAME))
• Requires the effects package.
• Provides a term-plot of the estimated regression line with 95% confidence interval for the mean.
• DATASETNAME <- DATASETNAME %>% mutate(log.y = log(y)
• Creates a transformed variable called log.y – change this to be more specific to your “$$y$$” or “$$x$$”.
• predict(MODELNAME, se.fit = T)
• Provides fitted values for all observed $$x\text{'s}$$ with SEs for the mean.
• predict(MODELNAME, newdata = tibble(x = XNEW), interval = “confidence”)
• Provides fitted value for a specific $$x$$ (XNEW) with CI for the mean. Replace x with name of explanatory variable.
• predict(MODELNAME, newdata = tibble(x = XNEW), interval = “prediction”)
• Provides fitted value for a specific $$x$$ (XNEW) with PI for a new observation. Replace x with name of explanatory variable.
• qt(0.975, df = n - 2)
• Gets the $$t^*$$ multiplier for making a 95% confidence or prediction interval with $$n-2$$ replaced by the sample size – 2.

This page titled 7.9: 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.