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, betterpredict 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 lab 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.

load("more/mlb11.RData")

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 lab, 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?
fit1 <- lm(formula = runs ~ at_bats, data = mlb11)
plot(runs ~ at_bats, data = mlb11, xlab = "At Bats", ylab = "Runs")
abline(fit1)

I would use a scatterplot, with runs as the response variable and at_bats as the explanatory varaible. To this I would add a regression line. Yes, I would be comfortable.

If the relationship looks linear, we can quantify the strength of the relationship with the correlation coefficient.

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.
paste0 ("Correlation between at_bats and runs is ", round(cor(mlb11$runs, mlb11$at_bats), 4))
## [1] "Correlation between at_bats and runs is 0.6106"
summary(fit1)
## 
## Call:
## lm(formula = runs ~ at_bats, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -125.58  -47.05  -16.59   54.40  176.87 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -2789.2429   853.6957  -3.267 0.002871 ** 
## at_bats         0.6305     0.1545   4.080 0.000339 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 66.47 on 28 degrees of freedom
## Multiple R-squared:  0.3729, Adjusted R-squared:  0.3505 
## F-statistic: 16.65 on 1 and 28 DF,  p-value: 0.0003388

At-bats show a positive correlation with runs of .6106. The regression line has a slope of .6305, which means that for every 100 at bats, runs increase by about 63. The R^2 statistic of .3729 means that 37% of the variation in linear model is explained by the relationship between at-bats and runs.

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.

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

## Click two points to make a line.
                                
## Call:
## lm(formula = y ~ x, data = pts)
## 
## Coefficients:
## (Intercept)            x  
##  -2789.2429       0.6305  
## 
## Sum of Squares:  123721.9

This function doesn’t seem to be interactive - syntax error or user error?

After running this command, you’ll be prompted to click two points on the plot to define a line. Once you’ve done that, the line you specified will be shown in black and the residuals in blue. Note that there are 30 residuals, one for each of the 30 observations. Recall that the residuals are the difference between the observed values and the values predicted by the line:

\[ e_i = y_i - \hat{y}_i \]

The most common way to do linear regression is to select the line that minimizes the sum of squared residuals. To visualize the squared residuals, you can rerun the plot command and add the argument showSquares = TRUE.

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

## Click two points to make a line.
                                
## Call:
## lm(formula = y ~ x, data = pts)
## 
## Coefficients:
## (Intercept)            x  
##  -2789.2429       0.6305  
## 
## Sum of Squares:  123721.9

Note that the output from the plot_ss function provides you with the slope and intercept of your line as well as the sum of squares.

  1. Using plot_ss, choose a line that does a good job of minimizing the sum of squares. Run the function several times. What was the smallest sum of squares that you got? How does it compare to your neighbors?

This feature does not seem to be working. Jumping to the next number.

The linear model

It is rather cumbersome to try to get the correct least squares line, i.e. the line that minimizes the sum of squared residuals, through trial and error. Instead we can use the lm function in R to fit the linear model (a.k.a. regression line).

m1 <- lm(runs ~ at_bats, data = mlb11)

The first argument in the function lm is a formula that takes the form y ~ x. Here it can be read that we want to make a linear model of runs as a function of at_bats. The second argument specifies that R should look in the mlb11 data frame to find the runs and at_bats variables.

The output of lm is an object that contains all of the information we need about the linear model that was just fit. We can access this information using the summary function.

summary(m1)
## 
## Call:
## lm(formula = runs ~ at_bats, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -125.58  -47.05  -16.59   54.40  176.87 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -2789.2429   853.6957  -3.267 0.002871 ** 
## at_bats         0.6305     0.1545   4.080 0.000339 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 66.47 on 28 degrees of freedom
## Multiple R-squared:  0.3729, Adjusted R-squared:  0.3505 
## F-statistic: 16.65 on 1 and 28 DF,  p-value: 0.0003388

Let’s consider this output piece by piece. First, the formula used to describe the model is shown at the top. After the formula you find the five-number summary of the residuals. The “Coefficients” table shown next is key; its first column displays the linear model’s y-intercept and the coefficient of at_bats. With this table, we can write down the least squares regression line for the linear model:

\[ \hat{y} = -2789.2429 + 0.6305 * atbats \]

One last piece of information we will discuss from the summary output is the Multiple R-squared, or more simply, \(R^2\). The \(R^2\) value represents the proportion of variability in the response variable that is explained by the explanatory variable. For this model, 37.3% of the variability in runs is explained by at-bats.

  1. Fit a new model that uses homeruns to predict runs. Using the estimates from the R output, write the equation of the regression line. What does the slope tell us in the context of the relationship between success of a team and its home runs?
fit2 <- lm(formula = homeruns ~ runs, data = mlb11)
plot(homeruns ~ runs, data = mlb11, xlab = "Homeruns", ylab = "Runs")
abline(fit2)

summary(fit2)
## 
## Call:
## lm(formula = homeruns ~ runs, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -52.067 -15.794   3.702  15.766  39.232 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -85.15663   34.79698  -2.447   0.0209 *  
## runs          0.34154    0.04983   6.854  1.9e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 22.13 on 28 degrees of freedom
## Multiple R-squared:  0.6266, Adjusted R-squared:  0.6132 
## F-statistic: 46.98 on 1 and 28 DF,  p-value: 1.9e-07

homeruns = .34154 * runs - 85.15663

Prediction and prediction errors

Let’s create a scatterplot with the least squares line laid on top.

plot(mlb11$runs ~ mlb11$at_bats)
abline(m1)

The function abline plots a line based on its slope and intercept. Here, we used a shortcut by providing the model m1, which contains both parameter estimates. This line can be used to predict \(y\) at any value of \(x\). When predictions are made for values of \(x\) that are beyond the range of the observed data, it is referred to as extrapolation and is not usually recommended. However, predictions made within the range of the data are more reliable. They’re also used to compute the residuals.

  1. If a team manager saw the least squares regression line and not the actual data, how many runs would he or she predict for a team with 5,578 at-bats? Is this an overestimate or an underestimate, and by how much? In other words, what is the residual for this prediction?
# Calculate the predicted value based on the regression line.

atbat <- 5578
y.hat <- 0.6305 * atbat - 2789.2429

# Look up the actual values

head(mlb11[order(-mlb11$at_bats),],9)
##                     team runs at_bats hits homeruns bat_avg strikeouts
## 2         Boston Red Sox  875    5710 1600      203   0.280       1108
## 4     Kansas City Royals  730    5672 1560      129   0.275       1006
## 1          Texas Rangers  855    5659 1599      210   0.283        930
## 14       Cincinnati Reds  735    5612 1438      183   0.256       1250
## 6          New York Mets  718    5600 1477      108   0.264       1085
## 10        Houston Astros  615    5598 1442       95   0.258       1164
## 11     Baltimore Orioles  708    5585 1434      191   0.257       1120
## 16 Philadelphia Phillies  713    5579 1409      153   0.253       1024
## 3         Detroit Tigers  787    5563 1540      169   0.277       1143
##    stolen_bases wins new_onbase new_slug new_obs
## 2           102   90      0.349    0.461   0.810
## 4           153   71      0.329    0.415   0.744
## 1           143   96      0.340    0.460   0.800
## 14           97   79      0.326    0.408   0.734
## 6           130   77      0.335    0.391   0.725
## 10          118   56      0.311    0.374   0.684
## 11           81   69      0.316    0.413   0.729
## 16           96  102      0.323    0.395   0.717
## 3            49   95      0.340    0.434   0.773
# Reference the actual values to find actual runs for the team closest to 5578 at-bats.

y.actual <- mlb11$runs[16]
y.delta <- y.actual - y.hat

paste0("The manager would predict ", round(y.hat), " runs.  The actual number of runs for a team with 5579 at-bats is ", round(y.actual), ".  The manager prediction would overestimate by about ", abs(round(y.delta)), " runs.")
## [1] "The manager would predict 728 runs.  The actual number of runs for a team with 5579 at-bats is 713.  The manager prediction would overestimate by about 15 runs."

Model diagnostics

To assess whether the linear model is reliable, we need to check for (1) linearity, (2) nearly normal residuals, and (3) constant variability.

Linearity: You already checked if the relationship between runs and at-bats is linear using a scatterplot. We should also verify this condition with a plot of the residuals vs. at-bats. Recall that any code following a # is intended to be a comment that helps understand the code but is ignored by R.

plot(m1$residuals ~ mlb11$at_bats)
abline(h = 0, lty = 3)  # adds a horizontal dashed line at y = 0

  1. Is there any apparent pattern in the residuals plot? What does this indicate about the linearity of the relationship between runs and at-bats?

There does not appear to be any sort of pattern in the residuals, which indicates that the disparity between predicted and observed values does not vary meaningfully on the regression line.

Nearly normal residuals: To check this condition, we can look at a histogram

hist(m1$residuals)

or a normal probability plot of the residuals.

qqnorm(m1$residuals)
qqline(m1$residuals)  # adds diagonal line to the normal prob plot

  1. Based on the histogram and the normal probability plot, does the nearly normal residuals condition appear to be met?

The histogram appear to be a slightly rightward-skewed normal distribution. Points in the normal probability plot stay close to the line (though there does seem to be a slight waveform). From these two observations, we can probably assume that the conditions are met for nearly normal residuals.

Constant variability:

  1. Based on the plot in (1), does the constant variability condition appear to be met?

As there does not seem to be any sort of pattern in the residuals, yes, it appears the conditions for constant variability are met.


On Your Own

str(mlb11)
## 'data.frame':    30 obs. of  12 variables:
##  $ team        : Factor w/ 30 levels "Arizona Diamondbacks",..: 28 4 10 13 26 18 19 16 9 12 ...
##  $ runs        : int  855 875 787 730 762 718 867 721 735 615 ...
##  $ at_bats     : int  5659 5710 5563 5672 5532 5600 5518 5447 5544 5598 ...
##  $ hits        : int  1599 1600 1540 1560 1513 1477 1452 1422 1429 1442 ...
##  $ homeruns    : int  210 203 169 129 162 108 222 185 163 95 ...
##  $ bat_avg     : num  0.283 0.28 0.277 0.275 0.273 0.264 0.263 0.261 0.258 0.258 ...
##  $ strikeouts  : int  930 1108 1143 1006 978 1085 1138 1083 1201 1164 ...
##  $ stolen_bases: int  143 102 49 153 57 130 147 94 118 118 ...
##  $ wins        : int  96 90 95 71 90 77 97 96 73 56 ...
##  $ new_onbase  : num  0.34 0.349 0.34 0.329 0.341 0.335 0.343 0.325 0.329 0.311 ...
##  $ new_slug    : num  0.46 0.461 0.434 0.415 0.425 0.391 0.444 0.425 0.41 0.374 ...
##  $ new_obs     : num  0.8 0.81 0.773 0.744 0.766 0.725 0.788 0.75 0.739 0.684 ...

The traditional variables are at-bats, hits, home runs, batting average, strikeouts, and stolen bases. Hits is likely a good candidate - let’s use that.

fit3 <- lm(formula = hits ~ runs, data = mlb11)
plot(hits ~ runs, data = mlb11, xlab = "Hits", ylab = "Runs")
abline(fit3)

summary(fit3)
## 
## Call:
## lm(formula = hits ~ runs, data = mlb11)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -103.583  -30.643    0.605   35.252  120.308 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 822.1675    83.3760   9.861 1.31e-10 ***
## runs          0.8459     0.1194   7.085 1.04e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 53.03 on 28 degrees of freedom
## Multiple R-squared:  0.6419, Adjusted R-squared:  0.6292 
## F-statistic:  50.2 on 1 and 28 DF,  p-value: 1.043e-07

The multiple R^2 between runs and at_bats is .6266. The multiple R^2 between hits and at_bats is .6419. Marginally more variability is explained in the second model than in the first model.

# The traditional variables are at-bats, hits, home runs, batting average, strikeouts, and stolen bases.

summary(m1) # 1) runs and at_bats, R^2 = .3729
## 
## Call:
## lm(formula = runs ~ at_bats, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -125.58  -47.05  -16.59   54.40  176.87 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -2789.2429   853.6957  -3.267 0.002871 ** 
## at_bats         0.6305     0.1545   4.080 0.000339 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 66.47 on 28 degrees of freedom
## Multiple R-squared:  0.3729, Adjusted R-squared:  0.3505 
## F-statistic: 16.65 on 1 and 28 DF,  p-value: 0.0003388
summary(fit3) # runs and hits, R^2 = .6419
## 
## Call:
## lm(formula = hits ~ runs, data = mlb11)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -103.583  -30.643    0.605   35.252  120.308 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 822.1675    83.3760   9.861 1.31e-10 ***
## runs          0.8459     0.1194   7.085 1.04e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 53.03 on 28 degrees of freedom
## Multiple R-squared:  0.6419, Adjusted R-squared:  0.6292 
## F-statistic:  50.2 on 1 and 28 DF,  p-value: 1.043e-07
summary(fit2) # 2) runs and homeruns, R^2 = .6266
## 
## Call:
## lm(formula = homeruns ~ runs, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -52.067 -15.794   3.702  15.766  39.232 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -85.15663   34.79698  -2.447   0.0209 *  
## runs          0.34154    0.04983   6.854  1.9e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 22.13 on 28 degrees of freedom
## Multiple R-squared:  0.6266, Adjusted R-squared:  0.6132 
## F-statistic: 46.98 on 1 and 28 DF,  p-value: 1.9e-07
fit4 <- lm(formula = bat_avg ~ runs, data = mlb11) # runs and batting average
summary(fit4) # runs and batting average, R^2 = .6561
## 
## Call:
## lm(formula = bat_avg ~ runs, data = mlb11)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.0136347 -0.0048163  0.0004583  0.0051691  0.0155111 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1.681e-01  1.196e-02  14.059 3.26e-14 ***
## runs        1.252e-04  1.712e-05   7.308 5.88e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.007606 on 28 degrees of freedom
## Multiple R-squared:  0.6561, Adjusted R-squared:  0.6438 
## F-statistic: 53.41 on 1 and 28 DF,  p-value: 5.877e-08
fit5 <- lm(formula = strikeouts ~ runs, data = mlb11) # runs and strikeouts
summary(fit5) # runs and strikeouts, R^2 = .1694
## 
## Call:
## lm(formula = strikeouts ~ runs, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -181.95  -87.45   37.40   79.84  135.88 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1523.5321   157.5673   9.669 2.01e-10 ***
## runs          -0.5391     0.2256  -2.389   0.0239 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 100.2 on 28 degrees of freedom
## Multiple R-squared:  0.1694, Adjusted R-squared:  0.1397 
## F-statistic: 5.709 on 1 and 28 DF,  p-value: 0.02386
fit6 <- lm(formula = stolen_bases ~ runs, data = mlb11) # runs and stolen bases
summary(fit6) # runs and stolen bases, R^2 = .0029
## 
## Call:
## lm(formula = stolen_bases ~ runs, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -62.126 -19.338  -0.803  20.606  62.667 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 95.74099   47.72206   2.006   0.0546 .
## runs         0.01955    0.06834   0.286   0.7769  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 30.35 on 28 degrees of freedom
## Multiple R-squared:  0.002914,   Adjusted R-squared:  -0.0327 
## F-statistic: 0.08183 on 1 and 28 DF,  p-value: 0.7769
# The strongest R^2 is found between strikeouts and runs
plot(bat_avg ~ runs, data = mlb11, xlab = "Strikeouts", ylab = "Runs")
abline(fit4)

# The newer variables are on-base, slug, and on-base plus slugs.

fit7 <- lm(formula = new_onbase ~ runs, data = mlb11) # runs and on-base
summary(fit7) # runs and on-base, R^2 = .5314
## 
## Call:
## lm(formula = new_onbase ~ runs, data = mlb11)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.0108850 -0.0034976 -0.0004294  0.0023316  0.0108692 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.163e-01  8.354e-03   25.89  < 2e-16 ***
## runs        1.502e-04  1.196e-05   12.55 5.12e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.005314 on 28 degrees of freedom
## Multiple R-squared:  0.8491, Adjusted R-squared:  0.8437 
## F-statistic: 157.6 on 1 and 28 DF,  p-value: 5.116e-13
plot(new_onbase ~ runs, data = mlb11, xlab = "On-base", ylab = "Runs")
abline(fit7)

fit8 <- lm(formula = new_slug ~ runs, data = mlb11) # runs and slugs
summary(fit8) # runs and slugs, R^2 = .8969
## 
## Call:
## lm(formula = new_slug ~ runs, data = mlb11)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.016184 -0.005936  0.001474  0.006825  0.017002 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1.668e-01  1.497e-02   11.14 8.34e-12 ***
## runs        3.345e-04  2.144e-05   15.61 2.42e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.009521 on 28 degrees of freedom
## Multiple R-squared:  0.8969, Adjusted R-squared:  0.8932 
## F-statistic: 243.5 on 1 and 28 DF,  p-value: 2.42e-15
plot(new_slug ~ runs, data = mlb11, xlab = "Slugs", ylab = "Runs")
abline(fit8)

fit9 <- lm(formula = new_obs ~ runs, data = mlb11) # runs and on-base plus slugs
summary(fit9) # wuns and obs, R^2 = .9349
## 
## Call:
## lm(formula = new_obs ~ runs, data = mlb11)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.017098 -0.009121  0.001791  0.006924  0.020315 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 3.812e-01  1.696e-02   22.48   <2e-16 ***
## runs        4.871e-04  2.429e-05   20.06   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.01079 on 28 degrees of freedom
## Multiple R-squared:  0.9349, Adjusted R-squared:  0.9326 
## F-statistic: 402.3 on 1 and 28 DF,  p-value: < 2.2e-16
plot(new_obs ~ runs, data = mlb11, xlab = "Obs", ylab = "Runs")
abline(fit9)

On-base plus slugs percentage seems to be the best predictor of runs. Its R^2 statistic of .9349 means that about 93% of the variation in the linear model is explained by the relationship between obs and runs. This next best contender is also a new variable, slugs, which has an R^2 of .8969. A table will help with comparing the R^2 statistics for each variable.

# Compose a tibble ranking variables by highest R^2 statistic.

variable.fits <- tibble("Variable" = c("at-bats", "hits", "homeruns", "batting average", "strikeouts", "stolen bases", "on-base", "slugs", "on-base plus slugs"), "R2" = c(.3729, .6419, .6266, .6561, .1694, .0029, .5314, .8969, .9349))
variable.fits <- variable.fits[order(-variable.fits$R2),]
variable.fits
## # A tibble: 9 x 2
##             Variable     R2
##                <chr>  <dbl>
## 1 on-base plus slugs 0.9349
## 2              slugs 0.8969
## 3    batting average 0.6561
## 4               hits 0.6419
## 5           homeruns 0.6266
## 6            on-base 0.5314
## 7            at-bats 0.3729
## 8         strikeouts 0.1694
## 9       stolen bases 0.0029
plot(new_obs ~ runs, data = mlb11, xlab = "Obs", ylab = "Runs")
abline(fit9)

# Check the residuals for new_obs (fit9). 

plot(fit9$residuals ~ mlb11$new_obs, main = "Obs-Runs Residuals Plot", xlab = "Obs", ylab = "Residuals")
abline(h = 0, lty = 3)  

hist(fit9$residuals, main = "Histogram of Residuals", xlab = "Residuals")

qqnorm(fit9$residuals, main = "Obs-Runs Residuals QQPlot")
qqline(fit9$residuals)

Linearity: There does not appear to be any sort of pattern in the residuals plot which would suggest a need for curvature to fit a model.

Nearly normal residuals: While a historgram of residuals does reveal a unimodal distribution centered on 0, there are high frequencies in the left tail (also apparent in the QQ plot) which lead me to question whether the distribution is normal.

Constant variability: There does not appear to be any sort of pattern in the residuals plot which would suggest variability is constant or random.

This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported. This lab was adapted for OpenIntro by Andrew Bray and Mine Çetinkaya-Rundel from a lab written by the faculty and TAs of UCLA Statistics.