STAT 3341 Homework #1

Author

Victoria Romanczyk, 48202600

Code
library(knitr)
library(formatR)
library(DT)

Instructions (Level 1)

Your goal is to reproduce every aspect of this document (including this!). The only additional thing that should be included in your document is your name and student ID. The point break down is as follows:

  • 15 points: Submitting both a .qmd and .html file that include your name and student ID as the “Author” in the YAML header
  • 15 points: YAML header (including embed-resources, spacelab theme, code folding, and table of contents)
  • 10 points: R Chunks (including chunk options)
  • 10 points: Displaying Pythagorean win percentage equation (including the ^ above wins)
  • 30 points: All text formatting issues (including headers, hyperlinks, bulleted lists, tabs, and font in bold/italics)
  • 10 points: Datatable formatting
  • 10 points: In-line code used to display the max Pythagorean win percentage in the “Viewing the Raw Data” section

Pythagorean Winning Percentage (Level 2)

The Pythagorean winning percentage is an advanced metric first proposed by Bill James. The idea is that game-to-game variability can sometimes mask overall strength… that is, the number of games you actually won may not match with how many games you should have won.

The original formula for Pythagorean winning percentage is: \[\hat{wins}=\frac{R^2}{R^2+RA^2}\]

where R = runs scored and RA = runs allowed. More information about Pythagorean winning percentage can be found at:

https://www.mlb.com/glossary/advanced-stats/pythagorean-winning-percentage

Application to Lahman Data (Level 2)

Let’s explore Pythagorean winning percentage using the Lahman package. Specifically, you need to load the library and access the “Teams” data set.

Code
##You wouldn't want to display the entire data set because it is 2,955 rows
##If we wanted to do this, we would just type the name of the data set
##Instead, we set eval: false to prevent the long output!

library(Lahman)
dim(Teams)
Teams

Remember, you can use the datatable() function from the DT package to create an interactive table. We will work with all seasons since 1995, which is when the Wild Card was introduced. First, let’s subset the data and add both actual winning percentage and Pythagorean winning percentage.

Code
library(Lahman)
teams.short <- subset(Teams[,c(1:2, 4, 9:10, 15, 27, 14)], yearID >= 1995)

teams.short$WinPercent <- with(teams.short, W/(W+L))
teams.short$PythagPercent <- with(teams.short, R^2/(R^2 + RA^2))

Below is a nicely formatted table that displays the teams.short data set defined above. Note that the chunk of code that creates this table has intentionally not been displayed (e.g. echo: false).

Using in-line code with the max() and round() functions (and rounding to three digits), we find that the max Pythagorean winning percentage was 0.732. This can be confirmed by sorting the table above.

Next, let’s explore the correlation between actual winning percentage and Pythagorean winning percentage.

Code
with(teams.short, plot(PythagPercent, WinPercent, xlab="Pythagorean Winning Percentage", ylab="Actual Winning Percentage"))

Last, let’s see whether actual winning percentage or Pythagorean winning percentage are better predictors of winning the world series.

Code
teams.short$WSChamp <- ifelse(teams.short$WSWin=='Y', 1, 0)

summary(glm(WSChamp ~ WinPercent, data=teams.short, family='binomial'))

Call:
glm(formula = WSChamp ~ WinPercent, family = "binomial", data = teams.short)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-1.40081  -0.25678  -0.13470  -0.07035   2.89977  

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)  -16.274      2.204  -7.384 1.54e-13 ***
WinPercent    23.442      3.733   6.279 3.41e-10 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 245.11  on 833  degrees of freedom
Residual deviance: 188.63  on 832  degrees of freedom
AIC: 192.63

Number of Fisher Scoring iterations: 7
Code
summary(glm(WSChamp ~ PythagPercent, data=teams.short, family='binomial'))

Call:
glm(formula = WSChamp ~ PythagPercent, family = "binomial", data = teams.short)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-1.34537  -0.26138  -0.14679  -0.08042   2.87928  

Coefficients:
              Estimate Std. Error z value Pr(>|z|)    
(Intercept)    -14.678      1.966  -7.468 8.17e-14 ***
PythagPercent   20.591      3.325   6.193 5.89e-10 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 245.11  on 833  degrees of freedom
Residual deviance: 194.27  on 832  degrees of freedom
AIC: 198.27

Number of Fisher Scoring iterations: 7

The AIC value for the model using actual winning percentage is slightly lower, indicating that it is a better predictor of winning the world series (but perhaps not meaningfully better).