This tutorial shows you:

Note on copying & pasting code from the PDF version of this tutorial: Please note that you may run into trouble if you copy & paste code from the PDF version of this tutorial into your R script. When the PDF is created, some characters (for instance, quotation marks or indentations) are converted into non-text characters that R won’t recognize. To use code from this tutorial, please type it yourself into your R script or you may copy & paste code from the source file for this tutorial which is posted on my website.

Regression: Basic concepts

So far, you have examined relationships between continuous and binary variables (differences of means & differences of proportions). In your textbook sections for today (Applied Regression Analysis sections 5.1 and 5.2, henceforth AR), you first encounter regression analysis as a technique to express a linear relationship between two continuous variables. We typically refer to these variables as outcome variable \(y\) and explanatory or predictor variable \(x\). Linear regression allows you to express the relationship between \(x\) and \(y\) through coefficients in the following equation:

\[ \begin{aligned} y_i = \alpha + \beta x_i + \varepsilon \end{aligned} \]

\(\alpha\) is the so-called intercept; \(\beta\) is the slope coefficient for your explanatory variable \(x\); \(\varepsilon\) is the residual. Other ways to express these are \(\beta_0\) for the intercept and \(\beta_1\) for the slope coefficient, or to use Roman instead of Greek letters. The index \(i\) stands for the individual observations of your data. Also see the summary in AR, top of p. 82 (2nd edition) / p. 87 (3rd edition).

The following picture illustrates these concepts:

Basic workflow

As you just read, linear regression is a useful tool to identify a linear relationship between two continuous variables. To do this, we typically perform the following steps. We’ll expand these steps in the next few weeks and supply the logic behind them, but the underlying routine remains the same.

  1. Based on your research question and theory, operationalize and measure your outcome and explanatory variables.
  2. If your outcome variable is continuous, linear regression may be an appropriate tool to examine variation on this outcome and evaluate your hypothesis.
  3. Specify the implication of your hypothesis for the relationship between explanatory and outcome variables: should they be positively related, negatively related, or not related at all?
  4. Specify your linear regression equation in the form \(y_i = \alpha + \beta x_i + \varepsilon\)

    • \(\beta\) will express the relationship between your explanatory variable \(x\) and your outcome \(y\).
    • As you will see later today, \(\beta\) can be interpreted as: “a one-unit change in \(x\) results in a \(\beta\)-unit change in \(y\).”
    • What does your hypothesis predict for \(\beta\)? Do you expect \(\beta\) be positive or negative? How large do you expect \(\beta\) to be?
  5. Create a scatterplot of your explanatory and predictor variables.
  6. Fit a linear regression model by identifying the straight line that best fits the above scatterplot.
  7. Check how well this model fits your data and whether it violates any of the regression assumptions. Note: not all of these assumptions can be directly checked by inspecting your data.

    • Linearity (A1)
    • Constant error variance (A2)
    • Normality of the errors (A3)
    • Independence of observations (A4)
    • No correlation between \(x\) and \(\varepsilon\) (A5)
  8. Interpret \(\beta\) in a meaningful way and evaluate how it relates to your hypothesized value of \(\beta\).

The remainder of this tutorial explains these concepts with some example data about various performance indicators of baseball teams.

Batter up

The movie “Moneyball” focuses on the “quest for the secret of success in baseball”. It follows a low-budget team, the Oakland Athletics, who believed that underused statistics, such as a player’s ability to get on base, better predict the ability to score runs than typical statistics like home runs, RBIs (runs batted in), and batting average. Obtaining players who excelled in these underused statistics turned out to be much more affordable for the team.

In this tutorial we’ll be looking at data from all 30 Major League Baseball teams and examining the linear relationship between runs scored in a season and a number of other player statistics. Our aim will be to summarize these relationships both graphically and numerically in order to find which variable, if any, helps us best predict a team’s runs scored in a season.

The data

Let’s load up the data for the 2011 season.

mlb11 <- read.csv("")
##                  team runs at_bats hits homeruns bat_avg strikeouts
## 1       Texas Rangers  855    5659 1599      210   0.283        930
## 2      Boston Red Sox  875    5710 1600      203   0.280       1108
## 3      Detroit Tigers  787    5563 1540      169   0.277       1143
## 4  Kansas City Royals  730    5672 1560      129   0.275       1006
## 5 St. Louis Cardinals  762    5532 1513      162   0.273        978
## 6       New York Mets  718    5600 1477      108   0.264       1085
##   stolen_bases wins new_onbase new_slug new_obs
## 1          143   96      0.340    0.460   0.800
## 2          102   90      0.349    0.461   0.810
## 3           49   95      0.340    0.434   0.773
## 4          153   71      0.329    0.415   0.744
## 5           57   90      0.341    0.425   0.766
## 6          130   77      0.335    0.391   0.725

In addition to runs scored, there are seven traditionally used variables in the data set: at-bats, hits, home runs, batting average, strikeouts, stolen bases, and wins. There are also three newer variables: on-base percentage, slugging percentage, and on-base plus slugging. For the first portion of the analysis we’ll consider the seven traditional variables. At the end of the tutorial, you’ll work with the newer variables on your own.

  1. What type of plot would you use to display the relationship between runs and one of the other numerical variables? Plot this relationship using the variable at_bats as the predictor. Does the relationship look linear? If you knew a team’s at_bats, would you be comfortable using a linear model to predict the number of runs?

If the relationship looks linear, we can quantify the strength of the relationship with the correlation coefficient (see AR, bottom of p. 85 (2nd ed.) / p. 90 (3rd ed.), equation 5.4).

cor(mlb11$runs, mlb11$at_bats)
## [1] 0.610627

Sum of squared residuals

Think back to the way that we described the distribution of a single variable. Recall that we discussed characteristics such as center, spread, and shape. It’s also useful to be able to describe the relationship of two numerical variables, such as runs and at_bats above.

  1. Looking at your plot from the previous exercise, describe the relationship between these two variables. Make sure to discuss the form, direction, and strength of the relationship as well as any unusual observations.

Just as we used the mean and standard deviation to summarize a single variable, we can summarize the relationship between these two variables by finding the line that best follows their association. Use the following interactive function to select the line that you think does the best job of going through the cloud of points. Note: you need to read this function into R using the source command. I uploaded a copy of it to my website; the function was originally written by the authors of the OpenIntro book. This function is interactive: it will prompt you to click two points in the RStudio plot window.

plot_ss(x = mlb11$at_bats, y = mlb11$runs)