This is a continuation of my first project of Chess tournament, the chunks below are essential for the next part of this project where the data is loaded, cleaned and tidying to move for the ELO calculations and make a comparison between the rates of chess players.
# List of packages to install
packages <- c("RCurl", "knitr", "kableExtra", "tidyverse", "stringr")
# Check and install packages if not already installed
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if (length(new_packages) > 0) {
install.packages(new_packages)
}
# Load required libraries
library(RCurl)
library(knitr)
library(kableExtra)
library(tidyverse)
library(stringr)
##PART 1: Cleaning data and generating CSV file
# Step 1: Read the text file from github
tournament_data <- readLines("https://cdn.rawgit.com/Jomifum/rawtournamentinfo/main/tournamentinfo.txt", warn = FALSE)
# Step 2: Extract the player data by using specified indices
data1 <- c(seq(5, length(tournament_data), 3)) # This for player names and total points
data2 <- c(seq(6, length(tournament_data), 3)) # By state and pre-rating
# Extracting the player names from data1
name <- str_replace_all(str_extract(tournament_data[data1], "([|]).+?\\1"), "[|]", "")
# Extracting state from data2
state <- str_extract(tournament_data[data2], "[A-Z]{2}")
# Extracting the total points from data1 as a float
total_points <- as.numeric(str_extract(tournament_data[data1], "\\d+\\.\\d+"))
# Extracting pre-rating from data2
pre_rating <- as.integer(str_replace_all(str_extract(tournament_data[data2], "R: \\s?\\d{3,4}"), "R:\\s", ""))
#Creating an initial data frame
df1 <- data.frame(name, state, total_points, pre_rating)
# Display the first 20 rows for the initial data frame
kable(head(df1, 20), "html", escape = FALSE) %>%
kable_styling("striped", full_width = FALSE, font_size = 15) %>%
column_spec(1:2, bold = TRUE)
name | state | total_points | pre_rating |
---|---|---|---|
GARY HUA | ON | 6.0 | 1794 |
DAKSHESH DARURI | MI | 6.0 | 1553 |
ADITYA BAJAJ | MI | 6.0 | 1384 |
PATRICK H SCHILLING | MI | 5.5 | 1716 |
HANSHI ZUO | MI | 5.5 | 1655 |
HANSEN SONG | OH | 5.0 | 1686 |
GARY DEE SWATHELL | MI | 5.0 | 1649 |
EZEKIEL HOUGHTON | MI | 5.0 | 1641 |
STEFANO LEE | ON | 5.0 | 1411 |
ANVIT RAO | MI | 5.0 | 1365 |
CAMERON WILLIAM MC LEMAN | MI | 4.5 | 1712 |
KENNETH J TACK | MI | 4.5 | 1663 |
TORRANCE HENRY JR | MI | 4.5 | 1666 |
BRADLEY SHAW | MI | 4.5 | 1610 |
ZACHARY JAMES HOUGHTON | MI | 4.5 | 1220 |
MIKE NIKITIN | MI | 4.0 | 1604 |
RONALD GRZEGORCZYK | MI | 4.0 | 1629 |
DAVID SUNDEEN | MI | 4.0 | 1600 |
DIPANKAR ROY | MI | 4.0 | 1564 |
JASON ZHENG | MI | 4.0 | 1595 |
# Step 3: Extract the opponent numbers
opponent1 <- str_extract_all(tournament_data[data1], "\\d+\\|")
opponent <- str_extract_all(opponent1, "\\d+")
## Warning in stri_extract_all_regex(string, pattern, simplify = simplify, :
## argument is not an atomic vector; coercing
# set up a vector to store opponents' pre-ratings
opponent_pre_rating <- numeric(length(data1))
# Calculate opponents' pre-ratings
for (i in 1:length(data1)) {
opponent_pre_rating[i] <- mean(pre_rating[as.numeric(unlist(opponent[i]))], na.rm = TRUE)
}
# Round up the opponent pre-ratings
opponent_pre_rating <- round(opponent_pre_rating, 0)
# Step 4: Create a final data frame without player_num
df2 <- data.frame(name, state, total_points, pre_rating, opponent_pre_rating)
# Display the final data frame
kable(df2, "html", escape = FALSE) %>%
kable_styling("striped", full_width = FALSE, font_size = 15) %>%
column_spec(1:2, bold = TRUE)
name | state | total_points | pre_rating | opponent_pre_rating |
---|---|---|---|---|
GARY HUA | ON | 6.0 | 1794 | 1605 |
DAKSHESH DARURI | MI | 6.0 | 1553 | 1469 |
ADITYA BAJAJ | MI | 6.0 | 1384 | 1564 |
PATRICK H SCHILLING | MI | 5.5 | 1716 | 1574 |
HANSHI ZUO | MI | 5.5 | 1655 | 1501 |
HANSEN SONG | OH | 5.0 | 1686 | 1519 |
GARY DEE SWATHELL | MI | 5.0 | 1649 | 1372 |
EZEKIEL HOUGHTON | MI | 5.0 | 1641 | 1468 |
STEFANO LEE | ON | 5.0 | 1411 | 1523 |
ANVIT RAO | MI | 5.0 | 1365 | 1554 |
CAMERON WILLIAM MC LEMAN | MI | 4.5 | 1712 | 1468 |
KENNETH J TACK | MI | 4.5 | 1663 | 1506 |
TORRANCE HENRY JR | MI | 4.5 | 1666 | 1498 |
BRADLEY SHAW | MI | 4.5 | 1610 | 1515 |
ZACHARY JAMES HOUGHTON | MI | 4.5 | 1220 | 1484 |
MIKE NIKITIN | MI | 4.0 | 1604 | 1386 |
RONALD GRZEGORCZYK | MI | 4.0 | 1629 | 1499 |
DAVID SUNDEEN | MI | 4.0 | 1600 | 1480 |
DIPANKAR ROY | MI | 4.0 | 1564 | 1426 |
JASON ZHENG | MI | 4.0 | 1595 | 1411 |
DINH DANG BUI | ON | 4.0 | 1563 | 1470 |
EUGENE L MCCLURE | MI | 4.0 | 1555 | 1300 |
ALAN BUI | ON | 4.0 | 1363 | 1214 |
MICHAEL R ALDRICH | MI | 4.0 | 1229 | 1357 |
LOREN SCHWIEBERT | MI | 3.5 | 1745 | 1363 |
MAX ZHU | ON | 3.5 | 1579 | 1507 |
GAURAV GIDWANI | MI | 3.5 | 1552 | 1222 |
SOFIA ADINA STANESCU-BELLU | MI | 3.5 | 1507 | 1522 |
CHIEDOZIE OKORIE | MI | 3.5 | 1602 | 1314 |
GEORGE AVERY JONES | ON | 3.5 | 1522 | 1144 |
RISHI SHETTY | MI | 3.5 | 1494 | 1260 |
JOSHUA PHILIP MATHEWS | ON | 3.5 | 1441 | 1379 |
JADE GE | MI | 3.5 | 1449 | 1277 |
MICHAEL JEFFERY THOMAS | MI | 3.5 | 1399 | 1375 |
JOSHUA DAVID LEE | MI | 3.5 | 1438 | 1150 |
SIDDHARTH JHA | MI | 3.5 | 1355 | 1388 |
AMIYATOSH PWNANANDAM | MI | 3.5 | 980 | 1385 |
BRIAN LIU | MI | 3.0 | 1423 | 1539 |
JOEL R HENDON | MI | 3.0 | 1436 | 1430 |
FOREST ZHANG | MI | 3.0 | 1348 | 1391 |
KYLE WILLIAM MURPHY | MI | 3.0 | 1403 | 1248 |
JARED GE | MI | 3.0 | 1332 | 1150 |
ROBERT GLEN VASEY | MI | 3.0 | 1283 | 1107 |
JUSTIN D SCHILLING | MI | 3.0 | 1199 | 1327 |
DEREK YAN | MI | 3.0 | 1242 | 1152 |
JACOB ALEXANDER LAVALLEY | MI | 3.0 | 377 | 1358 |
ERIC WRIGHT | MI | 2.5 | 1362 | 1392 |
DANIEL KHAIN | MI | 2.5 | 1382 | 1356 |
MICHAEL J MARTIN | MI | 2.5 | 1291 | 1286 |
SHIVAM JHA | MI | 2.5 | 1056 | 1296 |
TEJAS AYYAGARI | MI | 2.5 | 1011 | 1356 |
ETHAN GUO | MI | 2.5 | 935 | 1495 |
JOSE C YBARRA | MI | 2.0 | 1393 | 1345 |
LARRY HODGE | MI | 2.0 | 1270 | 1206 |
ALEX KONG | MI | 2.0 | 1186 | 1406 |
MARISA RICCI | MI | 2.0 | 1153 | 1414 |
MICHAEL LU | MI | 2.0 | 1092 | 1363 |
VIRAJ MOHILE | MI | 2.0 | 917 | 1391 |
SEAN M MC CORMICK | MI | 2.0 | 853 | 1319 |
JULIA SHEN | MI | 1.5 | 967 | 1330 |
JEZZEL FARKAS | ON | 1.5 | 955 | 1327 |
ASHWIN BALAJI | MI | 1.0 | 1530 | 1186 |
THOMAS JOSEPH HOSMER | MI | 1.0 | 1175 | 1350 |
BEN LI | MI | 1.0 | 1163 | 1263 |
# Step 5: Save to a CSV file
write.table(df2, file = "tournament_results.csv", sep = ",", col.names = TRUE, row.names = FALSE)
#ELO calculation
ELO Expected Score Formula The expected score for a player š“ against an opponent B in the ELO system is calculated using the formula:
EA=1/(1+10^((RBāRA)/400))
Where:
EA is the expected score for player A. ratingA is the ELO rating of player A. ratingB is the ELO rating of player B.
This formula essentially computes the probability of player A winning against player B, based on the difference in their ratings, code for the ELO calculus is below.
The code implements a function to calculate the expected score between two players based on their ELO ratings. This function utilizes a mathematical formula to estimate the probability of one player winning against another. The code then iterates through each player in the dataset, calculates their expected score based on their opponentsā ratings, and determines their score difference by comparing their expected score to their actual points scored. Players with positive score differences are considered overperformers, while those with negative differences are considered underperformers.
Finally, the code identifies the top 5 overperformers and the top 5 underperformers based on their score differences and displays their results. This analysis provides valuable insights into player performance, highlighting instances where players exceeded or fell short of expectations based on their ELO ratings. This information can be used to identify areas for improvement and to gain a deeper understanding of player strengths and weaknesses.
# Define the function to calculate the expected score between two players
expected_score <- function(player_rating, opponent_rating) {
return(1 / (1 + 10^((opponent_rating - player_rating) / 400)))
}
# Initialize new columns for expected score and score difference
df2$expected_score <- numeric(length(df2$name))
df2$score_difference <- numeric(length(df2$name))
# Calculate expected scores and the score difference for each player
for (i in 1:length(df2$name)) {
expected_total <- 0
for (j in 1:length(opponent[[i]])) { # Loop over each opponent for player i
opponent_index <- as.numeric(opponent[[i]][j]) # Convert to numeric to access opponent's rating
if (!is.na(opponent_index) && opponent_index <= length(df2$name)) { # Check if opponent exists
# Calculate the expected score against each opponent and sum it
expected_total <- expected_total + expected_score(df2$pre_rating[i], df2$pre_rating[opponent_index])
}
}
# Assign the total expected score to the player
df2$expected_score[i] <- expected_total
# Calculate the difference between actual and expected score
df2$score_difference[i] <- df2$total_points[i] - df2$expected_score[i]
}
# Print all players with their respective calculations
print(df2[, c("name", "total_points", "expected_score", "score_difference")])
## name total_points expected_score
## 1 GARY HUA 6.0 5.16157364
## 2 DAKSHESH DARURI 6.0 3.77882483
## 3 ADITYA BAJAJ 6.0 1.94508791
## 4 PATRICK H SCHILLING 5.5 4.74176432
## 5 HANSHI ZUO 5.5 4.38248398
## 6 HANSEN SONG 5.0 4.94459645
## 7 GARY DEE SWATHELL 5.0 4.58106788
## 8 EZEKIEL HOUGHTON 5.0 5.03428087
## 9 STEFANO LEE 5.0 2.28654888
## 10 ANVIT RAO 5.0 1.94485405
## 11 CAMERON WILLIAM MC LEMAN 4.5 5.33873083
## 12 KENNETH J TACK 4.5 4.10819113
## 13 TORRANCE HENRY JR 4.5 4.95307678
## 14 BRADLEY SHAW 4.5 4.18268993
## 15 ZACHARY JAMES HOUGHTON 4.5 1.37330887
## 16 MIKE NIKITIN 4.0 3.80433625
## 17 RONALD GRZEGORCZYK 4.0 4.66049867
## 18 DAVID SUNDEEN 4.0 4.58704123
## 19 DIPANKAR ROY 4.0 4.32620016
## 20 JASON ZHENG 4.0 5.12725313
## 21 DINH DANG BUI 4.0 4.32087927
## 22 EUGENE L MCCLURE 4.0 4.48162862
## 23 ALAN BUI 4.0 3.94107372
## 24 MICHAEL R ALDRICH 4.0 2.55189635
## 25 LOREN SCHWIEBERT 3.5 6.27565045
## 26 MAX ZHU 3.5 4.10248886
## 27 GAURAV GIDWANI 3.5 3.99556782
## 28 SOFIA ADINA STANESCU-BELLU 3.5 3.31100433
## 29 CHIEDOZIE OKORIE 3.5 4.59900797
## 30 GEORGE AVERY JONES 3.5 6.01822002
## 31 RISHI SHETTY 3.5 5.09246477
## 32 JOSHUA PHILIP MATHEWS 3.5 3.71839352
## 33 JADE GE 3.5 4.64227364
## 34 MICHAEL JEFFERY THOMAS 3.5 3.43588721
## 35 JOSHUA DAVID LEE 3.5 4.95789036
## 36 SIDDHARTH JHA 3.5 2.69611607
## 37 AMIYATOSH PWNANANDAM 3.5 0.77345290
## 38 BRIAN LIU 3.0 2.12959676
## 39 JOEL R HENDON 3.0 3.62310174
## 40 FOREST ZHANG 3.0 2.93713890
## 41 KYLE WILLIAM MURPHY 3.0 2.36484749
## 42 JARED GE 3.0 5.01041608
## 43 ROBERT GLEN VASEY 3.0 4.33396207
## 44 JUSTIN D SCHILLING 3.0 2.07174822
## 45 DEREK YAN 3.0 4.37239265
## 46 JACOB ALEXANDER LAVALLEY 3.0 0.04324981
## 47 ERIC WRIGHT 2.5 3.18615351
## 48 DANIEL KHAIN 2.5 2.53048057
## 49 MICHAEL J MARTIN 2.5 2.54134277
## 50 SHIVAM JHA 2.5 1.42393306
## 51 TEJAS AYYAGARI 2.5 1.02757017
## 52 ETHAN GUO 2.5 0.29545213
## 53 JOSE C YBARRA 2.0 1.71961464
## 54 LARRY HODGE 2.0 3.39891396
## 55 ALEX KONG 2.0 1.43978403
## 56 MARISA RICCI 2.0 1.08408337
## 57 MICHAEL LU 2.0 1.30485948
## 58 VIRAJ MOHILE 2.0 0.42612292
## 59 SEAN M MC CORMICK 2.0 0.41470738
## 60 JULIA SHEN 1.5 0.59606374
## 61 JEZZEL FARKAS 1.5 0.96672325
## 62 ASHWIN BALAJI 1.0 0.87870495
## 63 THOMAS JOSEPH HOSMER 1.0 1.42806004
## 64 BEN LI 1.0 2.27467058
## score_difference
## 1 0.83842636
## 2 2.22117517
## 3 4.05491209
## 4 0.75823568
## 5 1.11751602
## 6 0.05540355
## 7 0.41893212
## 8 -0.03428087
## 9 2.71345112
## 10 3.05514595
## 11 -0.83873083
## 12 0.39180887
## 13 -0.45307678
## 14 0.31731007
## 15 3.12669113
## 16 0.19566375
## 17 -0.66049867
## 18 -0.58704123
## 19 -0.32620016
## 20 -1.12725313
## 21 -0.32087927
## 22 -0.48162862
## 23 0.05892628
## 24 1.44810365
## 25 -2.77565045
## 26 -0.60248886
## 27 -0.49556782
## 28 0.18899567
## 29 -1.09900797
## 30 -2.51822002
## 31 -1.59246477
## 32 -0.21839352
## 33 -1.14227364
## 34 0.06411279
## 35 -1.45789036
## 36 0.80388393
## 37 2.72654710
## 38 0.87040324
## 39 -0.62310174
## 40 0.06286110
## 41 0.63515251
## 42 -2.01041608
## 43 -1.33396207
## 44 0.92825178
## 45 -1.37239265
## 46 2.95675019
## 47 -0.68615351
## 48 -0.03048057
## 49 -0.04134277
## 50 1.07606694
## 51 1.47242983
## 52 2.20454787
## 53 0.28038536
## 54 -1.39891396
## 55 0.56021597
## 56 0.91591663
## 57 0.69514052
## 58 1.57387708
## 59 1.58529262
## 60 0.90393626
## 61 0.53327675
## 62 0.12129505
## 63 -0.42806004
## 64 -1.27467058
# Find the top 5 players who overperformed and underperformed based on score difference
overperformers <- df2[order(df2$score_difference, decreasing = TRUE),][1:5, ]
underperformers <- df2[order(df2$score_difference),][1:5, ]
# Display the results for overperformers and underperformers
cat("Top 5 Overperformers:\n")
## Top 5 Overperformers:
print(overperformers[, c("name", "total_points", "expected_score", "score_difference")])
## name total_points expected_score
## 3 ADITYA BAJAJ 6.0 1.94508791
## 15 ZACHARY JAMES HOUGHTON 4.5 1.37330887
## 10 ANVIT RAO 5.0 1.94485405
## 46 JACOB ALEXANDER LAVALLEY 3.0 0.04324981
## 37 AMIYATOSH PWNANANDAM 3.5 0.77345290
## score_difference
## 3 4.054912
## 15 3.126691
## 10 3.055146
## 46 2.956750
## 37 2.726547
cat("\nTop 5 Underperformers:\n")
##
## Top 5 Underperformers:
print(underperformers[, c("name", "total_points", "expected_score", "score_difference")])
## name total_points expected_score
## 25 LOREN SCHWIEBERT 3.5 6.275650
## 30 GEORGE AVERY JONES 3.5 6.018220
## 42 JARED GE 3.0 5.010416
## 31 RISHI SHETTY 3.5 5.092465
## 35 JOSHUA DAVID LEE 3.5 4.957890
## score_difference
## 25 -2.775650
## 30 -2.518220
## 42 -2.010416
## 31 -1.592465
## 35 -1.457890
# Combine results for saving
results <- rbind(overperformers, underperformers)
# Save results to CSV
write.csv(results, "player_performance.csv")
#Results
The analysis examines the difference between playersā expected scores, calculated based on their ELO ratings, and their actual scores in a tournament. It identifies players who significantly overperformed, such as Aditya Bajaj and Zachary James Houghton, by achieving scores far higher than predicted by their ratings. Conversely, players like Loren Schwiebert and George Avery Jones, despite having higher pre-ratings, underperformed relative to their expected scores.
This analysis reveals that while ELO ratings provide a valuable estimate, they do not always accurately predict individual performance. Players with lower pre-ratings can outperform expectations, while those with higher ratings may underperform. This highlights the importance of considering factors beyond just ELO ratings when evaluating player performance, such as individual form, tournament conditions, and psychological factors.
##Plot
library(ggplot2)
# Create the plot
ggplot(df2, aes(x = total_points, y = expected_score)) + # Ensure 'total_points' is used for actual scores
geom_point(color = "#2c3e50", size = 2) + # Scatter plot with customized color and size
geom_smooth(method = "lm", formula = "y ~ x",
color = "#e74c3c", linetype = "dashed", size = 1) + # Linear model line (dashed)
geom_smooth(method = "loess", formula = "y ~ x",
color = "#3498db", se = FALSE) + # LOESS smooth line without confidence intervals
labs(
x = "Total Points",
y = "Total Expected Score",
title = "Actual vs. Expected Score"
) +
theme_light() # Light theme for the plot
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ā¹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
#Interpretation
Overall Trend represents the plot shows a general upward trend, indicating that as the total points scored increase, the total expected score also tends to increase. This is expected as higher-rated players are likely to score more points.
Regarding the scatter there is a considerable amount of scatter around the trend lines. This suggests that while the ELO rating system provides a general prediction, thereās significant variability in actual performance compared to the expected scores.
The Trend Lines such as the blue line likely a loess curve captures the non-linear relationship between actual and expected scores better than the red line likely a linear regression suggesting that the relationship might not be perfectly linear and the gray shaded area around the loess curve represents the confidence interval, showing the range within which the actual scores are likely to fall.
##Conclusion:
The tournament data reveals significant variability in player performance compared to their expected scores based on ELO ratings. Some players outperformed expectations, suggesting exceptional gameplay or underestimated ratings, while others underperformed, indicating that factors beyond their ratings, such as pressure or overestimation of their abilities, influenced their results.
Furthermore, the ELO rating system, while valuable, exhibits limitations. The scatter and variability observed in the plot demonstrate that ELO predictions are not always accurate. Individual performances are influenced by multiple factors, such as tournament conditions, psychological factors, and individual consistency, which are not fully captured by ELO ratings.
The plot reveals a non-linear relationship between actual points and expected scores. The loess curve provides a more accurate representation of this relationship than the linear regression. The scatter and confidence interval around the loess curve highlight the inherent uncertainty in predicting actual scores based on ELO ratings alone. These insights can be valuable for tournament organizers, analysts, players, and coaches to improve performance analysis and strategies beyond solely focusing on ELO ratings.
Note that the echo = FALSE
parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.