top of page

Assignment 4: Win Expectancy Formulas

For assignment four, we worked on understanding various win expectancy formulas. This assignment allowed us to see how the strong correlation between the winning percentage and the Pythagorean Win Estimation was. We were able to see using a linear model and scatter plot to analyze the correlation. This resulted in a very close y=x model.

Since the correlation is very strong, we used logarithms to replace the squares. This new value was then place back into the formula to show an even closer model. 

Download

library(readr)
library(tidyverse)
library(ggpubr)

#Part 1.A: Importing File and Creating a Subset with Needed Variables
Teams <- read_csv("MATH 494/Teams.csv")

teamW <- select(Teams, c(yearID, teamID, W, L, R, RA))

#Part 1.B: Calculate Winning Percentage
winF <- function(df){
                              wPer <- with(df, (W/(W+L))*100)
                              df <- cbind(df, wPer)
                             }

teamW <- winF(teamW)

#Part 1.C: Calculate Pythagorean Winning Percentage
winPythF <- function(df,n){
                                       wPythD <- with(df, (R^n)+(RA^n))
                                       wPythN <- with(df, R^n)
                                       wPyth <- (wPythN/wPythD)*100
                                       df <- cbind(df, wPyth)
                                      } 


##Function will be used for other parts of the assignment

teamW <- winPythF(teamW, 2)

#Part 1.D: Create Win Per vs Pyth. Win Per Scatterplot
WinP_Pyth <- ggplot(teamW, aes(wPyth, wPer)) + 
                     geom_point(color="#ff99ff") +
                     geom_smooth(method = "lm", se=FALSE, color="blue") +
                     labs(title="Win Percentage vs Pythagoean Winning Percentage", 
                            x="Pythagoean Winning Percentage", 
                            y="Winning Percentage") +
                            theme(plot.title = element_text(hjust = 0.5))
WinP_Pyth

#Part 1.E: Create Linear Model
model1 <- lm(wPer ~ wPyth, data=teamW) 
summary(model1)

slope1 <- round(coef(model1)[2], 3)
int1 <- round(coef(model1)[1], 3)
r2_1 <- round(summary(model1)$r.squared, 3)
r_1 <- round(sqrt(r2_1), 3)

##Display Linear Model
glue::glue("y = {int1} + {slope1}x", "
               R = {r_1}", "
               R-Squared = {r2_1}")

Assignment 4 Code and Results

Anastashia Pelletier

©2022 by Anastashia Pelletier. Proudly created with Wix.com

bottom of page