Batter up

The data

download.file("http://www.openintro.org/stat/data/mlb11.RData", destfile = "mlb11.RData")
load("mlb11.RData")

Exercise 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?

boxplot(mlb11$runs ~ mlb11$bat_avg)

plot(mlb11$at_bats, mlb11$hits,  # plot the variables 
    xlab="At Bats",              # x−axis label 
    ylab="Hits")                # y−axis label
reg1 <- lm(at_bats~hits,data=mlb11)
abline(reg1)

Answer: Yes, the relationship looks linear for at bats and hits.
plot(mlb11$at_bats, mlb11$runs,  # plot the variables 
    xlab="At Bats",              # x−axis label 
    ylab="Runs")                # y−axis label

Answer: The relationship is linear, but less tightly between at_bats and runs. I would be more confident using a linear model to predict the number of hits with at_bats than I would using a linear model to predict the number of runs with at_bats.
cor(mlb11$runs, mlb11$at_bats)
## [1] 0.610627

Sum of squared residuals

Exercise 2

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.
Answer: Using the At Bats compared to Runs plot, the data seems to follow a general linear pattern in an upward direction, however there is great deviation from a normal line. The relationship seems to be positively correlated but there are many variances among the scatter points in both directions.
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
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

Exercise 3

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?
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
Answer: I did not receive the “select two lines” prompt and could not select different lines, hence the only sum of squares was 123721.9.

The linear model

m1 <- lm(runs ~ at_bats, data = mlb11)
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

Exercise 4

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?
m2 <- lm(runs ~ homeruns, data = mlb11)
summary(m2)
## 
## Call:
## lm(formula = runs ~ homeruns, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -91.615 -33.410   3.231  24.292 104.631 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 415.2389    41.6779   9.963 1.04e-10 ***
## homeruns      1.8345     0.2677   6.854 1.90e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 51.29 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
Answer: According to the summary, the linear relationship between homeruns and runs is runs = 415.2389 + 1.8345 x homeruns
The R-squared value for the model is 0.6266 which means 62.66% of the data can be explained by this linear model.

Prediction and prediction errors

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

Exercise 5

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?
Answer:
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
-2789.2429 + (0.6305*5578)
## [1] 727.6861
The linear relationship between run and at_bats is described by runs = -2789.2429 + 0.6305 x at_bats. For a at_bats value of 5578, predicted runs should be 728. If a team manager saw the least squares regression line and not the actual data, he or she would predict approximately 728 runs for a team with 5,578 at-bats.

Model diagnostics

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

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

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

Exercise 6

Is there any apparent pattern in the residuals plot? What does this indicate about the linearity of the relationship between runs and at-bats?
Answer: The points are generally dispersed along the horizontal line, with differing degrees of scatterpoint distance and at least one apparent outlier. However the linear model does appear appropriate to analyze run and at_bats.
hist(m1$residuals)

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

Exercise 7

Based on the histogram and the normal probability plot, does the nearly normal residuals condition appear to be met?
Answer: Based on the histogram and the normal probability plot the nearly normal residuals condition does appear to have been met. The histogram indicates a slight right skew, however with the exception of the 0-50 range, the rest of the histogram is roughly normal. The QQ plot also indicates a fairly normal distribution with the lower outliers of the 0-50 range apparent as well as slightly higher figures than expected in the -100 to 0 range and slightly lower numbers than expected in the 100-200 range.

Exercise 8

Based on the plot in (1), does the constant variability condition appear to be met?
Answer: based on the plot in (1) the constant variability condition does appear to be met, since the points are randomly dispersed and variable in amount.

On Your Own

1. Choose another traditional variable from mlb11 that you think might be a good predictor of runs. Produce a scatterplot of the two variables and fit a linear model. At a glance, does there seem to be a linear relationship?
m3 <- lm(runs ~ bat_avg, data = mlb11)
plot(mlb11$runs ~ mlb11$bat_avg)
abline(m3)

m4 <- lm(runs ~ stolen_bases, data = mlb11)
plot(mlb11$runs ~ mlb11$stolen_bases)
abline(m4)

Answer: The scatterplot of batting averages and runs with a linear regression illustrates a fit to the linear model more so than the runs and stolen bases scatterplot. While the runs and stolen bases plot has greater variation from the linear regression line, the scatterpoints do follow a linear pattern as well.
*2.How does this relationship compare to the relationship between runs and at_bats? Use the R2 values from the two model summaries to compare. Does your variable seem to predict runs better than at_bats? How can you tell?
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
summary(m3)
## 
## Call:
## lm(formula = runs ~ bat_avg, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -94.676 -26.303  -5.496  28.482 131.113 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   -642.8      183.1  -3.511  0.00153 ** 
## bat_avg       5242.2      717.3   7.308 5.88e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 49.23 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
summary(m4)
## 
## Call:
## lm(formula = runs ~ stolen_bases, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -139.94  -62.87   10.01   38.54  182.49 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  677.3074    58.9751  11.485 4.17e-12 ***
## stolen_bases   0.1491     0.5211   0.286    0.777    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 83.82 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
Answer: The R-squared value for at_bats and runs is 0.3729. The R2 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.
The R-squared value for bat_avg and runs is 0.6561. For this model 65.61% of the variability in runs is explained by batting average. Hence, batting average is a better predictor than at_bats.
The R-squared value for stolen_bases and runs is 0.002914. For this model 0.29% of the variability in runs is explained by stolen bases. Hence, stolen bases is the least effective predictor of runs among the three.
3. Now that you can summarize the linear relationship between two variables, investigate the relationships between runs and each of the other five traditional variables. Which variable best predicts runs? Support your conclusion using the graphical and numerical methods we’ve discussed (for the sake of conciseness, only include output for the best variable, not all five).
m1 <- lm(runs ~ at_bats, data = mlb11)
m2 <- lm(runs ~ homeruns, data = mlb11)
m3 <- lm(runs ~ bat_avg, data = mlb11)
m4 <- lm(runs ~ stolen_bases, data = mlb11)
m5 <- lm(runs ~ strikeouts, data = mlb11)
m6 <- lm(runs ~ hits, data = mlb11)
m7 <- lm(runs ~ wins, data = mlb11)
summary(m3)
## 
## Call:
## lm(formula = runs ~ bat_avg, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -94.676 -26.303  -5.496  28.482 131.113 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   -642.8      183.1  -3.511  0.00153 ** 
## bat_avg       5242.2      717.3   7.308 5.88e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 49.23 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
Answer: The variable that best predicts runs is batting average with an R-squared value of 65.61%. Second is hits with R2 = 64.19%. Third is homeruns with R2 = 62.66%. Fourth is wins with R2 = 36.1%. Fifth is at_bats with R2 = 37.29%. Sixth is strikeouts with R2 = 16.94%. Seventh is stolen bases with R2 = 0.2914%. Refer to table below in question 4.
4. Now examine the three newer variables. These are the statistics used by the author of Moneyball to predict a teams success. In general, are they more or less effective at predicting runs that the old variables? Explain using appropriate graphical and numerical evidence. Of all ten variables we’ve analyzed, which seems to be the best predictor of runs? Using the limited (or not so limited) information you know about these baseball statistics, does your result make sense?
m8 <- lm(runs ~ new_onbase, data = mlb11)
m9 <- lm(runs ~ new_slug, data = mlb11)
m10 <- lm(runs ~ new_obs, data = mlb11)
summary(m8)
## 
## Call:
## lm(formula = runs ~ new_onbase, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -58.270 -18.335   3.249  19.520  69.002 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -1118.4      144.5  -7.741 1.97e-08 ***
## new_onbase    5654.3      450.5  12.552 5.12e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 32.61 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
summary(m9)
## 
## Call:
## lm(formula = runs ~ new_slug, data = mlb11)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -45.41 -18.66  -0.91  16.29  52.29 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -375.80      68.71   -5.47 7.70e-06 ***
## new_slug     2681.33     171.83   15.61 2.42e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 26.96 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
summary(m10)
## 
## Call:
## lm(formula = runs ~ new_obs, data = mlb11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -43.456 -13.690   1.165  13.935  41.156 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -686.61      68.93  -9.962 1.05e-10 ***
## new_obs      1919.36      95.70  20.057  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 21.41 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
Answer: All three of the newer variables are better at predicting runs than the old variables. The best predictor is on-base plus slugging with an R2 of 93.49%. The second best predictor is the slugging percentage with an R2 value of 89.69%. The third best predictor is the on-base percentage with an R2 value of 84.91%. The linearity of these new variables with runs is shown in the plots below.
plot(mlb11$runs ~ mlb11$new_obs)
abline(m10)

plot(mlb11$runs ~ mlb11$new_slug)
abline(m9)

plot(mlb11$runs ~ mlb11$new_onbase)
abline(m8)

I have an extremely limited knowledge of baseball and only know what slugging means because I just looked it up. That said, the on base portion of it makes a lot of sense. Here is the final table:
library(xtable)
## Warning: package 'xtable' was built under R version 3.6.3
d <- data.frame (rank=c(1,2,3,4,5,6,7,8,9,10), mset_var=c(10,9,8,3,6,2,7,1,5,4), Rsquared=c(0.9349, 0.8969, 0.8491, 0.6561, 0.6419, 0.6266, 0.361, 0.3729, 0.1694, 0.0029))

d
##    rank mset_var Rsquared
## 1     1       10   0.9349
## 2     2        9   0.8969
## 3     3        8   0.8491
## 4     4        3   0.6561
## 5     5        6   0.6419
## 6     6        2   0.6266
## 7     7        7   0.3610
## 8     8        1   0.3729
## 9     9        5   0.1694
## 10   10        4   0.0029
# mset_var 10 = new onbase with slugging
# mset_var 9 = slugging
# mset_var 8 = onbase
# mset_var 3 = bat_avg
# mset_var 6 = hits
# mset_var 2 = homeruns
# mset_var 7 = wins
# mset_var 1 = at_bat
# mset_var 5 = strikeouts
# mset_var 4 = stolenbases
xtable(d)
## % latex table generated in R 3.6.2 by xtable 1.8-4 package
## % Sun Apr 12 09:57:23 2020
## \begin{table}[ht]
## \centering
## \begin{tabular}{rrrr}
##   \hline
##  & rank & mset\_var & Rsquared \\ 
##   \hline
## 1 & 1.00 & 10.00 & 0.93 \\ 
##   2 & 2.00 & 9.00 & 0.90 \\ 
##   3 & 3.00 & 8.00 & 0.85 \\ 
##   4 & 4.00 & 3.00 & 0.66 \\ 
##   5 & 5.00 & 6.00 & 0.64 \\ 
##   6 & 6.00 & 2.00 & 0.63 \\ 
##   7 & 7.00 & 7.00 & 0.36 \\ 
##   8 & 8.00 & 1.00 & 0.37 \\ 
##   9 & 9.00 & 5.00 & 0.17 \\ 
##   10 & 10.00 & 4.00 & 0.00 \\ 
##    \hline
## \end{tabular}
## \end{table}
**R-Squared table**

R-Squared table

Above table I was trying to create in R but have not yet figured it out

5. Check the model diagnostics for the regression model with the variable you decided was the best predictor for runs.
qqnorm(m10$residuals)
qqline(m10$residuals)  # adds diagonal line to the normal prob plot

Answer: The model diagnostics for the regression model with the new variable of on-base plus slugging is visually consistent with the decision that it is the best predictor for runs.