For this final project, the plan is to use a database of chess games, collected from Lichess.org, to reach the ultimate goal of becoming the best chess player in the world. The data set consists of over 6,000 classical chess games played by grand master players in high level tournaments, including the candidates, world championship and world cup going back to 1990. A classical game is any game where each player is given at least 2 hours to complete their moves, this ensures that the games in the data set are of the highest level of game play.
Obviously, it would be impossible to study all of the chess games ever played so it was decided to take a sample of the best games from the past couple decades. There should be no sampling bias other than the fact that the moves played during these games will be of decidedly higher level than a typical chess game. The data for this data set shows exactly what has happened in each of the games meaning that the data was collected by simply recording each move that was made by a player. This also means that there will be no bias in the way the data was collected.
This project is important to us because we both are novice chess players who are very interested in the subtle complexities of the game. Chess has extremely simple rules but it is said that there are more possible chess games than there are atoms in the observable universe leading. The best chess players in the world play moves based on “theory” rather than simply reacting to a particular move made by the opponent which is typical of a novice player. This means that they will often play a move which does not make sense at first glance but is actually the strongest move. We believe that if we can pick out some of the unique tendencies of expert chess players, then we may be able to learn how to become better players ourselves or teach others how to become better.
To begin this analysis, we first must load in the necessary data sets and packages. These are loaded in the following code chunk. The data set is called “chess_games.csv” and we will use the bigchess, tidyverse, dplyr, and maps packages. The chess games data set includes a game ID (GameID), the event the game was played at (Event) ,the city the game was played in (Site), the date of the game (Date), the round of the event (Round), the players names (Black and White), the result (Result), every move played (Movetext), and the number of moves (NMoves). Furthermore, an opening data set is used to compare the opening of the different games being analyzed.
knitr::opts_chunk$set(echo = F,
fig.align = "center")
# load in nessecary packages
pacman::p_load(bigchess, tidyverse, dplyr, maps, class, caret, rpart, rpart.plot)
# load in all games from pgn files into one data frame and save as csv for future use
#path_to_games <- dir("C:\\Users\\dfhei\\Desktop\\School\\Data Science\\Final Project\\Chess games pgn", full.names = TRUE)
#chess_games <- do.call(rbind, lapply(path_to_games, read.pgn, quiet = TRUE))|>
# filter(!is.na(Event), !is.na(Site), !is.na(Date), !is.na(Round), !is.na(White), !is.na(Black), !is.na(Result),!is.na(Movetext), !is.na(NMoves))
#write.csv(chess_games, "chess_games.csv")
# load chess games from file
chess_games <- read.csv("chess_games.csv")[,1:10] |>
rename(GameID = X)
# load data from bigchess package which holds chess opening data
data(eco)
# display chess games
tibble(chess_games)
## # A tibble: 6,006 × 10
## GameID Event Site Date Round White Black Result Movetext NMoves
## <int> <chr> <chr> <chr> <dbl> <chr> <chr> <chr> <chr> <int>
## 1 1 Candidats final Kuala … 1990… 1 Timm… Karp… 0-1 1.e4 e5… 36
## 2 2 Candidats final Kuala … 1990… 2 Karp… Timm… 1/2-1… 1.d4 Nf… 42
## 3 3 Candidats final Kuala … 1990… 3 Timm… Karp… 1/2-1… 1.d4 Nf… 61
## 4 4 Candidats final Kuala … 1990… 4 Karp… Timm… 1-0 1.d4 Nf… 75
## 5 5 Candidats final Kuala … 1990… 5 Timm… Karp… 1/2-1… 1.e4 e5… 38
## 6 6 Candidats final Kuala … 1990… 6 Karp… Timm… 1/2-1… 1.d4 Nf… 62
## 7 7 Candidats final Kuala … 1990… 7 Timm… Karp… 1/2-1… 1.e4 e5… 43
## 8 8 Candidats final Kuala … 1990… 8 Karp… Timm… 1-0 1.d4 Nf… 38
## 9 9 Candidats final Kuala … 1990… 9 Timm… Karp… 0-1 1.e4 e5… 56
## 10 10 Candidats qf3 Brusse… 1991… 1 Ivan… Juss… 1/2-1… 1.e4 e6… 50
## # ℹ 5,996 more rows
# display chess openings
tibble(eco)
## # A tibble: 2,014 × 6
## ECO Opening Variation Movetext NMoves LAN
## <chr> <chr> <chr> <chr> <dbl> <chr>
## 1 A00 Polish (Sokolsky) opening <NA> 1. b4 1 b2b4
## 2 A00 Polish Tuebingen variation 1. b4 Nh6 1 b2b4…
## 3 A00 Polish Outflank variation 1. b4 c6 1 b2b4…
## 4 A00 Benko's opening <NA> 1. g3 1 g2g3
## 5 A00 Lasker simul special <NA> 1. g3 h5 1 g2g3…
## 6 A00 Benko's opening reversed Alekhine 1. g3 e5… 2 g2g3…
## 7 A00 Grob's attack <NA> 1. g4 1 g2g4
## 8 A00 Grob spike attack 1. g4 d5… 3 g2g4…
## 9 A00 Grob Fritz gambit 1. g4 d5… 3 g2g4…
## 10 A00 Grob Romford counter-gambit 1. g4 d5… 3 g2g4…
## # ℹ 2,004 more rows
The next step is to clean the data. The first data set that we will clean is the chess games data set. This code chunk also contains a function which will remove a space after each dot which is found in the input string. This input string will represent a string of all the moves in the game. The space must be removed for formatting purposes within the data. The date column in the chess games data set is also modified to only show the year as some of the dates did not contain months or days. This way the data is much cleaner and easier to work with. After adding a new column for year, the Date column was removed from the data set.
## # A tibble: 6,006 × 10
## GameID Event Site Round White Black Result Movetext NMoves Year
## <int> <chr> <chr> <dbl> <chr> <chr> <chr> <chr> <int> <chr>
## 1 1 Candidats final Kuala … 1 Timm… Karp… 0-1 1.e4 e5… 36 1990
## 2 2 Candidats final Kuala … 2 Karp… Timm… 1/2-1… 1.d4 Nf… 42 1990
## 3 3 Candidats final Kuala … 3 Timm… Karp… 1/2-1… 1.d4 Nf… 61 1990
## 4 4 Candidats final Kuala … 4 Karp… Timm… 1-0 1.d4 Nf… 75 1990
## 5 5 Candidats final Kuala … 5 Timm… Karp… 1/2-1… 1.e4 e5… 38 1990
## 6 6 Candidats final Kuala … 6 Karp… Timm… 1/2-1… 1.d4 Nf… 62 1990
## 7 7 Candidats final Kuala … 7 Timm… Karp… 1/2-1… 1.e4 e5… 43 1990
## 8 8 Candidats final Kuala … 8 Karp… Timm… 1-0 1.d4 Nf… 38 1990
## 9 9 Candidats final Kuala … 9 Timm… Karp… 0-1 1.e4 e5… 56 1990
## 10 10 Candidats qf3 Brusse… 1 Ivan… Juss… 1/2-1… 1.e4 e6… 50 1991
## # ℹ 5,996 more rows
The next data set to clean is the chess openings data set. Similarly to the previous data set. We want to remove the space after each dot in Movetext data. We also are only interested in the opening name and the move sequence. It should be noted that there are many variations for each opening name. In order to simplify the study, we will be observing each opening as a collection of all of its variations. The last data cleaning operation is to arrange the data from most moves in opening sequence to least. This way the number of times an opening was played will not be skewed and it will consider every possible option for a particular sequence.
## # A tibble: 2,014 × 2
## Opening Movetext
## <chr> <chr>
## 1 Ruy Lopez 1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 4.Ba4 Nf6 5.O-O Be7 6.Re1 b5 7.Bb3 …
## 2 QGD semi-Slav 1.d4 d5 2.c4 e6 3.Nc3 Nf6 4.Nf3 c6 5.e3 Nbd7 6.Bd3 dxc4 7.Bxc4…
## 3 QGD 1.d4 d5 2.c4 e6 3.Nc3 Nf6 4.Bg5 Be7 5.e3 O-O 6.Nf3 Nbd7 7.Rc1 …
## 4 Giuoco Piano 1.e4 e5 2.Nf3 Nc6 3.Bc4 Bc5 4.c3 Nf6 5.d4 exd4 6.cxd4 Bb4+ 7.N…
## 5 Ruy Lopez 1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 4.Ba4 Nf6 5.O-O Be7 6.Re1 b5 7.Bb3 …
## 6 Ruy Lopez 1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 4.Ba4 Nf6 5.O-O Be7 6.Re1 b5 7.Bb3 …
## 7 Sicilian 1.e4 c5 2.Nf3 d6 3.d4 cxd4 4.Nxd4 Nf6 5.Nc3 g6 6.Be3 Bg7 7.Be2…
## 8 Gruenfeld 1.d4 Nf6 2.c4 g6 3.Nc3 d5 4.cxd5 Nxd5 5.e4 Nxc3 6.bxc3 Bg7 7.B…
## 9 Ruy Lopez 1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 4.Ba4 Nf6 5.O-O Be7 6.Re1 b5 7.Bb3 …
## 10 Ruy Lopez 1.e4 e5 2.Nf3 Nc6 3.Bb5 Nf6 4.O-O Nxe4 5.d4 Be7 6.Qe2 Nd6 7.Bx…
## # ℹ 2,004 more rows
FEN stands for Forsyth–Edwards Notation and it is the standard way of defining a particular position on the chess board in a single line of ASCII text. FEN is used to describe piece placement, color, castling, en passant, half moves, and full moves for a specific turn. This function is used to interpret FEN and return the appropriate data frame.
The following code chunk uses the “chess_board.csv” file, which contains the coordinates of all the chess square corners so that it can be used with the ggplot geom_polygon() method to display a chess board. Using the chess_baord data set, a board can be displayed and the color of each square and text in each square can be customized.
### Create functions for piece heat map In an attempt to visualize
patterns in the movement of chess pieces, we can determine the number of
times a piece moves to each square. Given that each piece starts on the
same square each game, similar squares are usually visited by the same
piece in different games. There are many general statements that are
given as advice and we believe many of these recommendations will become
evident this way. These recommendations include, keeping Knights away
from the edge of the board as they control less squares there, castling
early to get the King to a safe square, controlling the center of the
board and placing the Bishop on long diagonals to control as many
squares as possible.
The following code chuck contains 3 functions which are used to ultimately determine the frequency of a certain piece on each square of the chess board. This data will ultimately be used for the ggplot function in the next code chunk.
The following code uses the functions from above to calculate the frequency of each piece for both colors and creates a new data set based on the chess_board data set. This data set will be saved as a .csv so that it will not have to be recalculated every time.
The next code chunk takes the frequency data from above and displays it on a chess board. We expect to see differences between the white and black pieces since white always has the advantage of going first, therefore forcing black to usually play more defensive. We also expect the most frequent squares a piece visits to be the squares that the piece moves to in the opening stage of the game. During the first moves, where players move their pieces to more active and central squares, the number of squares that a piece can move to is very limited and we expected the same moves to be repeated across many different games. In the middlegame or endgame stages of the game, the number of possible squares a piece could reach will be too great to be similar across games. Furthermore, we expect the movement and position of the pieces to follow general chess principles which are taught and followed at most levels of chess. These principles include: - Castling Castling is a unique move a player can play where the King and Rook are moved at the same time. It is a defensive move used to get the King to safety in the corner of the board and at the same time move the rook to a more central square. It is recommended to always castle before going for an attack in chess to ensure the opponent has little counter play to slow you down. Even though a player can castle to either corner of the board on the back rank, castling most commonly is played to the short side. We expect the frequency of this move to greatly impact the results for the King and Rook pieces for both sides since it is expected to occur virtually all games. - Controlling the center The central squares of the board are where most of the activity occurs during a game. Controlling those squares and moving pieces to the center allows to pieces the greatest mobility and better chances at starting an attack. For this reason, the most common first moves in chess are d4 and e4, moving a pawn into the center. We expect this to be reflected in the frequency data by pieces being placed either in the center or close to it where they attack/defend a square in the center. - “A Knight on the rim is dim” This saying refers to keeping the knight away from the edge of the board. A Knight placed on the edge only has access to a maximum of 4 squares, half of its usual number of squares, and during a game, with many other pieces on the board, it can be even less. Therefore, it is good practice to keep the Knight in a central position where it has many more squares to move to.
There are countless other chess principles and many of them could be
visualized with a frequency board.
As can be seen in the graphs above, most of the pieces have very defined
squares that they frequent most in games. The only exceptions are the
queens and the bishops. The most likely explanation for the bishops is
that each bishop can only move on same colored squares. This results in
two independent sets frequency data where certain the frequency gets
split evenly between them. Taking a closer look a the bishops, some
patterns do emerge such as placing the bishops in front of the queen and
king and, especially for black, fianchettoing the bishops. A fianchetto
refers to placing the bishop on in the corner on either b2 or g2 for
white or b7 and g7 for black. As can be seen in the graph for the black
bishop, the fianchetto is performed quite frequently. The queen also
does not have a clear favorite square on the board. This is likely due
to the fact that the queen is the most powerful piece in chess since it
can move horizontally, vertically and diagonally. The queen is rarely
moved in the opening of the game since it can be easily attacked
allowing the opponent to play a forcing move. Delaying the movement of
the queen also results in the squares around the queen to open up so
that once it moves, the queen has many more options to move to, hence
resulting in no square being visited significantly more than others. As
predicted, the most frequent squares that the pieces move to are the
squares reached from their starting position. This agrees with our
hypothesis that this is caused by the number of possible moves for each
piece increases exponentially as the game progresses resulting in only
the first couple moves to be similar between games. This is not only
true for the pieces with clear most visited squares but also for the
bishops and queens who most frequently move to squares that are
accessible from their starting position. The other predictions are also
reflected in the data. Castling greatly affects the frequency data for
rooks and kings. It is evident that the squares are moved to through the
use of castling and not by manually moving the pieces since the squares
between the starting position and castling squares are not highlighted.
In addition, the knight can be seen to prefer central squares rather
than squares near the edge. The top players of chess need to utilize the
full potential of their pieces and cannot afford to limit them by
placing them on squares where they do not affect the game.
In chess, the opening is one of the most important parts of the game. It is the point of the game where everything is even and after the first move is played, a scramble ensues to achieve the advantage in the game. Knowing which openings are common among the best players to ever play chess is seemingly a good place to start when you want to analyze their play styles.
This code chunk is creates a function which can determine which opening was played by cross referencing the moves played in the game versus the opening sequence for each of the openings in the “opening_df” data frame. The name of the opening sequence will then be added to a new column in the chess games opening data set.
## # A tibble: 6,006 × 11
## GameID Event Site Round White Black Result Movetext NMoves Year Opening
## <int> <chr> <chr> <dbl> <chr> <chr> <chr> <chr> <int> <chr> <chr>
## 1 1 Candidat… Kual… 1 Timm… Karp… 0-1 1.e4 e5… 36 1990 Ruy Lo…
## 2 2 Candidat… Kual… 2 Karp… Timm… 1/2-1… 1.d4 Nf… 42 1990 King's…
## 3 3 Candidat… Kual… 3 Timm… Karp… 1/2-1… 1.d4 Nf… 61 1990 Nimzo-…
## 4 4 Candidat… Kual… 4 Karp… Timm… 1-0 1.d4 Nf… 75 1990 King's…
## 5 5 Candidat… Kual… 5 Timm… Karp… 1/2-1… 1.e4 e5… 38 1990 Ruy Lo…
## 6 6 Candidat… Kual… 6 Karp… Timm… 1/2-1… 1.d4 Nf… 62 1990 Benoni
## 7 7 Candidat… Kual… 7 Timm… Karp… 1/2-1… 1.e4 e5… 43 1990 Ruy Lo…
## 8 8 Candidat… Kual… 8 Karp… Timm… 1-0 1.d4 Nf… 38 1990 Queen'…
## 9 9 Candidat… Kual… 9 Timm… Karp… 0-1 1.e4 e5… 56 1990 Ruy Lo…
## 10 10 Candidat… Brus… 1 Ivan… Juss… 1/2-1… 1.e4 e6… 50 1991 French
## # ℹ 5,996 more rows
In this code chunk, we want to obtain the data regarding how many of each openings were played. This data will be stored in a separate data set called eco data which will be used to create visualizations regarding the use of each opening.
## # A tibble: 66 × 6
## Opening Num_played Result Wins Tot Cond_prop
## <chr> <int> <chr> <dbl> <dbl> <dbl>
## 1 Caro-Kann 215 White_wins 74 215 0.344
## 2 Caro-Kann 215 Black_wins 44 215 0.205
## 3 Caro-Kann 215 Draws 97 215 0.451
## 4 English 139 White_wins 44 139 0.317
## 5 English 139 Black_wins 21 139 0.151
## 6 English 139 Draws 74 139 0.532
## 7 English opening 251 White_wins 65 251 0.259
## 8 English opening 251 Black_wins 53 251 0.211
## 9 English opening 251 Draws 133 251 0.53
## 10 French 264 White_wins 88 264 0.333
## # ℹ 56 more rows
The data from above can then be plotted to display the likelihood of a win, draw, or loss depending on the opening choice of both players. The opening is an important part of the game and often determines the type of game that results. We expect the most common result to be a draw since chess played perfectly is a draw. After the draw, we expect white winning to be the second most likely outcome for any opening because white starts every game with the advantage of going first. This gives white the upper hand as they can have more control of the game and can force black to have to react to moves. At the highest level of chess, a win for black is often considered the most unlikely result for these reasons and visualizing the data should reveal this trend too.
As can be seen in the figure above, draws are the most likely outcome as
expected. Some openings are clearly better for white than black and
unsurprisingly no openings have a clear advantage in increasing the odds
for black. The Queens Gambit Accepted (GGA) is the best opening for
white as it results in the least wins for black, however it requires
black to accept the gambit which is unlikely and the Queen’s Gambit
Declined (QGD) is more common for this reason. Queen’s pawn openings in
general tend to favor white and result in more active pieces as it frees
up the queen early. The Giuoco Piano opening results in the most draws
at the highest level and this is most likely due to the opening being
very symmetrical, not giving either side an advantage, and the opening
being well studied and the theory widely known among the best players.
Many such openings exist in chess where the theory so well known that
the players often purposefully do not play the best move in the position
just to get the opponent to play outside of their preparation in an
attempt to create a more unbalanced game, forcing a decisive result.
In this code chunk, we want to obtain the data of the most widely played openings in the past 23 years. The data is stored in a data frame and used in the next code chunk to visualize graphically.
## `summarise()` has grouped output by 'Opening'. You can override using the
## `.groups` argument.
The chess_games data set contains chess games going back to 1990. The
use of computers in chess has since increased greatly and computers are
much stronger than humans ever will be at the game of chess. Therefore,
we expect the opening choice of players to change between different
years since, with the help of computers, the best moves in openings can
easily be studied. We do expect the people to playing the games to have
a greater effect on the opening choice since openings are also a
personal preference. Some players might also play the same opening every
game while others change openings between games.
As can be seen in the figure above, the Sicilian opening is the most
popular choice of opening in almost every year we examined. This was
unexpected, especially since the likelihood of a win for either white or
black is low compared to other openings. The Sicilian does result in
less draws than other openings which might be why it is so popular.e The
Sicilian is considered a very unbalanced opening as it results in two
different pawn structures for the white and black side. In a game, it is
often used by the higher rated player to force a result which should
result in a win for them due to the rating difference. Many other
factors, however also influence the opening choice that we cannot
conclude from these graphs.
In this next section the classification tree machine learning method will be employed to attempt to predict the end state of the game using the strength of position for both black and white as well as the year that the game was played. In this case, end state means either a decisive win or a draw. The first step in this process is to get all of the data necessary for the machine learning method. Here, two functions are used to pull the first 5 moves for both black and white from the Movetext string in chess_games_openings. These first 5 moves will then be analyzed by the Stockfish chess engine and given a score based on how strong the position is. These scores will be used in the classification tree.
Disclaimer: At the time of writing this the chess engine is still analyzing all 6000+ games which takes a very long time. It is unlikely that these results will be complete by the time this has uploaded. But we are confident that this process will be successful if there was enough time to thoroughly process all 6000+ games. Due to the need of having a running r script for the knitted document, we have substituted the variables Round and NMoves for the variables which would in turn hold the positional analysis scores
After the games have been analyzed, it is possible to create the full tree for explanatory variables. This is shown below.
## CP nsplit rel.error xerror xstd
## 1 1.560403e-01 0 1.0000000 1.0000000 0.01300270
## 2 3.624161e-02 1 0.8439597 0.8439597 0.01283025
## 3 2.013423e-02 2 0.8077181 0.8127517 0.01275740
## 4 9.060403e-03 3 0.7875839 0.8093960 0.01274878
## 5 4.697987e-03 5 0.7694631 0.7963087 0.01271372
## 6 4.194631e-03 6 0.7647651 0.7892617 0.01269387
## 7 3.020134e-03 8 0.7563758 0.7848993 0.01268124
## 8 2.516779e-03 9 0.7533557 0.7862416 0.01268515
## 9 1.761745e-03 13 0.7429530 0.7872483 0.01268807
## 10 1.677852e-03 20 0.7302013 0.7973154 0.01271650
## 11 1.510067e-03 23 0.7251678 0.8006711 0.01272567
## 12 1.342282e-03 27 0.7191275 0.8003356 0.01272476
## 13 1.275168e-03 30 0.7151007 0.8030201 0.01273199
## 14 1.230425e-03 37 0.7060403 0.8063758 0.01274090
## 15 1.174497e-03 40 0.7023490 0.8063758 0.01274090
## 16 1.118568e-03 42 0.7000000 0.8063758 0.01274090
## 17 1.006711e-03 48 0.6932886 0.8053691 0.01273824
## 18 9.228188e-04 59 0.6815436 0.8067114 0.01274178
## 19 8.948546e-04 63 0.6778523 0.8164430 0.01276670
## 20 8.389262e-04 73 0.6687919 0.8208054 0.01277745
## 21 6.711409e-04 94 0.6486577 0.8295302 0.01279819
## 22 6.375839e-04 149 0.6090604 0.8426174 0.01282738
## 23 6.291946e-04 160 0.6020134 0.8442953 0.01283096
## 24 6.040268e-04 172 0.5942953 0.8409396 0.01282377
## 25 5.872483e-04 185 0.5862416 0.8416107 0.01282522
## 26 5.592841e-04 210 0.5694631 0.8449664 0.01283238
## 27 5.369128e-04 237 0.5526846 0.8493289 0.01284146
## 28 5.033557e-04 242 0.5500000 0.8510067 0.01284489
## 29 4.697987e-04 288 0.5255034 0.8593960 0.01286147
## 30 4.474273e-04 293 0.5231544 0.8580537 0.01285888
## 31 4.194631e-04 308 0.5164430 0.8577181 0.01285823
## 32 4.026846e-04 331 0.5053691 0.8573826 0.01285757
## 33 3.835091e-04 338 0.5023490 0.8573826 0.01285757
## 34 3.355705e-04 347 0.4979866 0.8593960 0.01286147
## 35 2.982849e-04 663 0.3875839 0.8627517 0.01286783
## 36 2.796421e-04 677 0.3832215 0.8620805 0.01286657
## 37 2.684564e-04 692 0.3785235 0.8627517 0.01286783
## 38 2.516779e-04 744 0.3644295 0.8630872 0.01286846
## 39 2.396932e-04 800 0.3496644 0.8587248 0.01286017
## 40 2.237136e-04 819 0.3442953 0.8593960 0.01286147
## 41 2.097315e-04 1034 0.2845638 0.8620805 0.01286657
## 42 2.013423e-04 1042 0.2828859 0.8620805 0.01286657
## 43 1.917546e-04 1052 0.2808725 0.8624161 0.01286720
## 44 1.677852e-04 1064 0.2778523 0.8644295 0.01287096
## 45 1.438159e-04 1603 0.1835570 0.8637584 0.01286972
## 46 1.342282e-04 1621 0.1808725 0.8651007 0.01287220
## 47 1.118568e-04 1658 0.1758389 0.8681208 0.01287772
## 48 9.587728e-05 1880 0.1479866 0.8681208 0.01287772
## 49 8.389262e-05 1899 0.1459732 0.8684564 0.01287832
## 50 6.711409e-05 1944 0.1419463 0.8684564 0.01287832
## 51 5.592841e-05 1949 0.1416107 0.8691275 0.01287953
## 52 0.000000e+00 1955 0.1412752 0.8691275 0.01287953
## 53 -1.000000e+00 2341 0.1412752 0.8691275 0.01287953
The next step is to determine the pruning point for the tree.
## xerror cutoff cp prune value
## 0.797580566 0.004697987
Finally, this entire pruned tree can be displayed for analysis.
Using a confusion matrix it is possible to see data pertaining to the results of the machine learning method. Unfortunately, in this case there are no results to analyze so it is not possible to make any deductions about the data.
## # A tibble: 6,006 × 1
## chess_predict
## <fct>
## 1 draw
## 2 draw
## 3 draw
## 4 draw
## 5 draw
## 6 draw
## 7 draw
## 8 draw
## 9 draw
## 10 decisive
## # ℹ 5,996 more rows
## predict
## actual decisive draw
## decisive 2438 542
## draw 1751 1275
After analyzing the data found from this study, it is possible to recognize patterns in the moves of great chess players. The first of which is that nearly all of the games that were studied included a move called “castling”. This is where a player swaps the position of their king and rook. This is a special move and can only happen under certain conditions. On top of this, it was very common for these players to castle on their king side. This is called short-sided castling. According to this report, it is the much preferred method of castling by elite chess players. Another conclusion that can be made from this data is that the most played opening by these top players is the Sicilian and closely followed by the Ruy Lopez. This is interesting because the Sicilian is an opening that is not extremely complicated to play. This may be discouraging for less experienced players because it shows that the opening of the game may not have a huge impact on the outcome. However, in recent years, the Queens pawn game has become more and more popular. This may be because it has a higher draw rate and players at this level tend to play slower and more methodical which creates more draws. Overall, we believe that these analyses have helped pick out certain tendencies of great chess players so that we can try to implement them ourselves in the future.
Limitations of this project include the size of the data set. Only 6,000 games were analyzed which is an insignificant amount compared to the millions played online. Analyzing a larger data set would also allow for the data to be sorted by rating which could reveal different patterns at each level which could prove useful for a wider range of players. Analyzing the games of Grand Masters can be a great way to study chess, however opponents at lower level do not play the best move in every position and the style of play is generally different. A further recommendation would be to split up the frequency data into individual pieces instead of the type of piece. For example, separating the data for the white knight starting on b1 and g1 could provide more insight into the individual piece movement to learn where the pieces tend to move. This, however, is difficult to achieve using only the move text since it does not differentiate between the two and it is assumed that the PGN only refers to legal moves. If two pieces of the same type can move to the same square then the starting square of the piece is denoted to differentiate between the two. Finally, no machine learning methods were applied successfully in this project due to difficulties encountered while attempting to implement them. We suspect that the method would work well given a full set of data. However, the amount of time necessary to analyze all of the games was underestimated and the data was not ready to be analyzed in time for the submission. We will be running the script overnight to potentially give it enough time to compile before presenting where the data can then be analyzed for the machine learning method.