Take a moderndive into introductory linear regression with R

We present the moderndive R package of datasets and functions for tidyverse-friendly introductory linear regression (Wickham, Averick, et al. 2019). These tools leverage the well-developed tidyverse and broom packages to facilitate 1) working with regression tables that include confidence intervals, 2) accessing regression outputs on an observation level (e.g. fitted/predicted values and residuals), 3) inspecting scalar summaries of regression fit (e.g. R, R adj , and mean squared error), and 4) visualizing parallel slopes regression models using ggplot2-like syntax (Wickham, Chang, et al. 2019; Robinson and Hayes 2019). This R package is designed to supplement the book “Statistical Inference via Data Science: A ModernDive into R and the Tidyverse” (Ismay and Kim 2019). Note that the book is also available online at https://moderndive.com and is referred to as “ModernDive” for short.


Introduction
Let's load all the R packages we are going to need.

library(moderndive) library(ggplot2) library(dplyr) library(knitr) library(broom)
Let's consider data gathered from end of semester student evaluations for a sample of 463 courses taught by 94 professors from the University of Texas at Austin (Diez, Barr, and Çetinkaya-Rundel 2015). This data is included in the evals data frame from the moderndive package.
In the following table, we present a subset of 9 of the 14 variables included for a random sample of 5 courses 1 : 1. ID uniquely identifies the course whereas prof_ID identifies the professor who taught this course. This distinction is important since many professors taught more than one course. 2. score is the outcome variable of interest: average professor evaluation score out of 5 as given by the students in this course. 3. The remaining variables are demographic variables describing that course's instructor, including bty_avg (average "beauty" score) for that professor as given by a panel of 6 students.

Regression analysis the "good old-fashioned" way
Let's fit a simple linear regression model of teaching score as a function of instructor age using the lm() function.
score_model <-lm(score~age, data = evals) Let's now study the output of the fitted model score_model "the good old-fashioned way": using summary() which calls summary.lm() behind the scenes (we'll refer to them interchangeably throughout this paper).

Regression analysis using moderndive
As an improvement to base R's regression functions, we've included three functions in the moderndive package that take a fitted model object as input and return the same information as summary.lm(), but output them in tidyverse-friendly format (Wickham, Averick, et al. 2019). As we'll see later, while these three functions are thin wrappers to existing functions in the broom package for converting statistical objects into tidy tibbles, we modified them with the introductory statistics student in mind (Robinson and Hayes 2019). Furthermore, say you would like to create a visualization of the relationship between two numerical variables and a third categorical variable with k levels. Let's create this using a colored scatterplot via the ggplot2 package for data visualization . Using geom_smooth(method = "lm", se = FALSE) yields a visualization of an interaction model where each of the k regression lines has their own intercept and slope. For example in Figure 1, we extend our previous regression model by now mapping the categorical variable ethnicity to the color aesthetic.
Evgeni Chasnovski thus wrote a custom geom_ extension to ggplot2 called geom_parallel_slopes(); this extension is included in the moderndive package. Much like geom_smooth() from the ggplot2 package, you add geom_parallel_slopes() as a layer to the code, resulting in Figure 2.

Repository README
In the GitHub repository README, we present an in-depth discussion of six features of the moderndive package:

Produce metrics on the quality of regression model fits
Furthermore, we discuss the inner-workings of the moderndive package: 1. It leverages the broom package in its wrappers 2. It builds a custom ggplot2 geometry for the geom_parallel_slopes() function that allows for quick visualization of parallel slopes models in regression.

Author contributions
Albert Y. Kim and Chester Ismay contributed equally to the development of the moderndive package. Albert Y. Kim wrote a majority of the initial version of this manuscript with Chester Ismay writing the rest. Max Kuhn provided guidance and feedback at various stages of the package development and manuscript writing.