{"cells":[{"metadata":{},"cell_type":"markdown","source":"#Introduction and summary\n\nMarch Madness refers to that time of year (usually mid-March through the beginning of April) when the National Collegiate Athletic Association (NCAA) men's and women's college basketball tournaments are held.\n\nThe term madness describes the excitement that swirls around the sports world as tournament time approaches as well as it describes game upsets or unexpected winning of low seed team (Cinderella team).\n\nAt this report we are conducting descriptive analysis to explain the Cinderella upset by trying to answer the following questions:\n\n\n1- Do Cinderella team coaches have high winning history? How frequent Cinderella teams coaches were successful to lead their teams to win the First tournament round?\n\n2- Which team statistics did cinderella teams record higher than their opponent team? \n\n3- Are players with high performance an important factor for Cinderella teams winning?\n\n4- Did cinderella teams play more at home or away?\n\nOur results shows that Cinderella team coaches have strong first round winning history. Cinderella teams have defensive rebounds and turnovers higher than the opponent team and less personal fouls. Players with high performance play an important role as we have found that neutralizing award players in the successive season resulted in team failure to eneter the tournament while teams kept their award players were successful to eneter the tournament with high performance. 2 out of 3 Cinderella teams played regular season games at home more than away. \n\nTo summarize, coaches and players performance are strong indicator for winning the tournament even if the team is low seeded.\n\n\n\n#Loading Libraries"},{"metadata":{"trusted":true,"_kg_hide-output":true,"_kg_hide-input":true},"cell_type":"code","source":"\nlibrary(dplyr) #a tool for working with data frames.\nlibrary(ggplot2) # a visualization tool.\nlibrary(data.table) #for fast aggregation of large data.\nlibrary(stringr) #for wrapping common string operations.\nlibrary(tidyverse) \nlibrary(gridExtra) #for arranging grid-based plots.\nlibrary(gganimate) #to create animations with ggplot\n\nlibrary(rvest) #for web scarpping\nlibrary(xml2) #to work with xml files\nlibrary(igraph) #SNA visualization tool","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#Load Data"},{"metadata":{"trusted":true,"_kg_hide-input":false},"cell_type":"code","source":"\nreg_season_stats <-  read.csv(\"../input/march-madness-analytics-2020/MDataFiles_Stage2/MRegularSeasonDetailedResults.csv\", header = TRUE)\n\nreg_season_compact <- read.csv(\"../input/march-madness-analytics-2020/MDataFiles_Stage2//MRegularSeasonCompactResults.csv\", header = TRUE)\n\ntourney_stats <- read.csv(\"../input/march-madness-analytics-2020/MDataFiles_Stage2/MNCAATourneyDetailedResults.csv\", header = TRUE)\n\nteams <- read.csv(\"../input/march-madness-analytics-2020/MDataFiles_Stage2/MTeams.csv\", header = TRUE)\n\ntourney_stats_compact <- read.csv(\"../input/march-madness-analytics-2020/MDataFiles_Stage2/MNCAATourneyCompactResults.csv\", header = TRUE)\n\ntourney_seeds <- read.csv(\"../input/march-madness-analytics-2020/MDataFiles_Stage2/MNCAATourneySeeds.csv\", header = TRUE)\n\ncoaches <- read.csv(\"../input/march-madness-analytics-2020/MDataFiles_Stage2/MTeamCoaches.csv\", header = TRUE)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#Detecting Cinderella teams."},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"#Tourney Data preparation:\n\n#separate win and loss teams into 2 separate datasets\ndata_win <- select(tourney_stats_compact, \"Season\", \"DayNum\",\"WTeamID\", \"WScore\", \"WLoc\")\ndata_loss <- select(tourney_stats_compact, \"Season\", \"DayNum\",\"LTeamID\", \"LScore\", \"WLoc\")\n\n\n#Unify columns names for matching & merging datasets purposes\nnames(data_win) <- c(\"Season\", \"DayNum\", \"TeamID\", \"Score\", \"WLoc\")\nnames(data_loss) <- c(\"Season\", \"DayNum\", \"TeamID\", \"Score\", \"WLoc\")\n\n#Adding team name to datasets matched by team id \ndata_win2 <- data_win %>%\n  inner_join(teams %>% select(TeamID, TeamName), by = \"TeamID\") \ndata_loss2 <- data_loss %>%\n  inner_join(teams %>% select(TeamID, TeamName), by = \"TeamID\")\n\n#Adding seed to datasets matched by team id & season\ndat <-data_win2  %>%\n  inner_join(tourney_seeds %>% select(TeamID, Seed, Season), by = c(\"TeamID\", \"Season\")) %>%\n  mutate(Wseed = str_sub(Seed, 2,3)) %>% #select the seed number e.g selecting 08 from W08\n  select(Season, DayNum ,TeamID, Score, TeamName, Wseed)\n\ndat2 <-data_loss2  %>%\n  inner_join(tourney_seeds %>% select(TeamID, Seed, Season), by = c(\"TeamID\", \"Season\")) %>%\n  mutate(Lseed = str_sub(Seed, 2,3)) %>% #select the seed number e.g selecting 08 from W08\n  select(Season, DayNum,TeamID, Score, TeamName, Lseed)\n#Unifying columns names.\n\nnames(dat) <- c(\"Season\", \"DayNum\", \"TeamID\", \"Score\", \"TeamName\", \"seed\")\nnames(dat2) <- c(\"Season\", \"DayNum\", \"TeamID\", \"Score\", \"TeamName\", \"seed\")\n\n#reuniting win and loss teams\ndat2_fin <- cbind(dat, dat2)\n\n#unselect repeated columns\ndat2_fin <- dat2_fin[, -c( 8, 9)]\n\n#rename columns\nnames(dat2_fin) <- c(\"Season\", \"DayNum\", \"WTeamID\", \"WScore\", \"WTeamName\", \"Wseed\", \"LTeamID\",\"LScore\", \"LTeamName\", \"Lseed\")\ndat2_fin$Wseed <- as.numeric(dat2_fin$Wseed)\ndat2_fin$Lseed <- as.numeric(dat2_fin$Lseed)\ndat2_fin154 <- subset(dat2_fin, dat2_fin$DayNum == \"154\")\ndat2_fin154$Cinderella <- ifelse(dat2_fin154$Wseed - dat2_fin154$Lseed > \"2\", \"Yes\", \"No\")\ndat2_fin154 <- subset(dat2_fin154, dat2_fin154$Cinderella == \"Yes\")\ndat2_fin154\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We define the team as a cinderella if the winner team's seed is lesser than the loser team with more than two points.\nFrom the table, we can find that cinderella teams over years as following;\n\n1- Villanova (seed 8) won Georgetown (seed 1) in 1985.\n\n2-Kanasa (seed 6) won Oklahoma (seed 1) in 1988.\n\n3-Arizona (seed 4) won Kentucky (seed 1) in 1997.\n\n\nNow let's get the performance history of cinderella teams at all tournaments championchips from 1985 to 2019."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"dat$status <- rep(\"win\") #create a win column in win dataset named \"status\".\ndat2$status <- rep(\"loss\")#create a loss column in loss dataset named \"status\".\n\n#Combining win & loss datasets into 1 dataset.\ndat_fin <- rbind(dat, dat2) \n\n\n \n#Converting Scores and Seeds into numeric vectors.\ndat_fin$seed <- as.numeric(dat_fin$seed)\ndat_fin$Score <- as.numeric(dat_fin$Score)\n\n#Subsetting the final championship data at DayNum 154 to explore Championship cinderellas.\ndat_fin154 <- subset(dat_fin, dat_fin$DayNum == \"154\")\n\ndat_cind_hist <- subset(dat_fin154, dat_fin154$TeamName == \"Villanova\" | dat_fin154$TeamName == \"Kansas\" | dat_fin154$TeamName == \"Arizona\") #subsetting Cinderellas teams from tournament championship data.\n\ndat_cind_hist <- select(dat_cind_hist, \"Season\", \"DayNum\",\"TeamID\",\"TeamName\", \"status\", \"seed\") %>%\n  arrange(desc(TeamName)) #selecting desired columns and arranging columns by team name\n\ndat_cind_hist","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As we can observe from data, \n\n1-Villanova enetered the championship 3 times at 1985, 2016 and 2018 and it was the winner at all of them. Also, Villanova seed is increasing over years.\n\n2-Kansas enetered the championship 5 times in 1988, 1991, 2003, 2008 and 2012.Kansas has 2 wins at 1988 & 2008 and 3 losses at 1991, 2003 & 2012. Kansas seed is increasing over years too except at 2012 went down from seed 1 to seed 2.\n\n3-Arizona enetered the championship 2 times in 1997 and 2001 with one win and win loss.\n"},{"metadata":{},"cell_type":"markdown","source":"##Coaches\n\nFor coaches, we will first find the coaches of 3 championship cinderella teams, then we will find how frequent their teams won the tourney first round as a measurement of their performance."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"CTeam <- subset(dat_cind_hist, dat_cind_hist$Season == \"1985\" | dat_cind_hist$Season == \"1997\" | dat_cind_hist$Season == \"1988\") #subsetting Cinderella team year from their championship history\nCTeam_coaches <- inner_join(CTeam, coaches, by = c(\"TeamID\", \"Season\")) #joining coach name by team id and season\nCTeam_coaches","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The coaches of Cinderella teams are massimino, rollie for Villanova, brown, larry for Kansas & oslon, lute for Arizona.\n\nNow We will get their performance at First Round over years."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"coaches_records <-  coaches[coaches$CoachName %in% intersect(coaches$CoachName, CTeam_coaches$CoachName),] #subsetting Cinderella coaches from original coaches data.\n\ndat_fin_final <- subset(dat_fin, dat_fin$DayNum == \"136\" | dat_fin$DayNum == \"137\") #subsetting First round dates from our prepared data.\n\ncoaches_records2 <- inner_join(dat_fin_final, coaches_records, by = c(\"Season\", \"TeamID\")) %>%\n  select(\"Season\", \"DayNum\", \"TeamID\", \"Score\", \"TeamName\", \"seed\", \"status\", \"CoachName\") %>%\n  arrange(desc(CoachName)) #merging the two data by season and team id.\n\n coaches_records2 %>% #visualizing data\nggplot() + geom_bar(aes(x = CoachName, fill = status), position = \"dodge\") + labs(x = \"Coach Name\", y = \"win/loss counts\") + theme(panel.background = NULL) + ggtitle(\"Cinderella team coaches winning/loss history\") +ggtitle(\"Cinderella team coaches first round win/loss history\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As we can observe from the graph, the Cinderella coaches have strong First Round winning history. Brown was successful to eneter the First round 5 times with 4 wins & 1 loss. Olson has 24 successful entry with 15 wins and 9 losses. Massiminio has 5 successful entries with 4 wins and 1 loss. We can conclude that the coach performance may be a strong factor in cinderella cases.\n\n##Players\n\nWe will explore players factors in two forms:\n\n1-Comparing Cinderella teams stats with the opponent team.\n\n2-Exploring the role of award players selection in team performance.\n\n###Players performance\n\n####Villanova & Georgetown stats\nAs regular and tourney detailed data is only available starting from 2003 and our tournament Cinderella all before 2000, we will get the total cinderella teams and their opponents stats data from [sport Reference website](https://www.sports-reference.com/cbb/boxscores/1985-04-01-georgetown.html) \n\n"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"library(readxl) #import data\ngboxscores <- read_excel(\"../input/additional-datasets/gboxscores.xlsx\")\nvboxscores <- read_excel(\"../input/additional-datasets/vboxscores.xlsx\")\n\ngboxscores_totals <- gboxscores[9, ] #select the total score row\nvboxscores_totals <- vboxscores[9, ]\ngboxscores_totals[1,1] <- \"Georgetown\" #assign team name\nvboxscores_totals[1,1] <- \"Villanova\"\nboxscores_totals <- rbind(gboxscores_totals, vboxscores_totals) #combine the 2 datasets\nboxscores_totals$Starters <- as.factor(boxscores_totals$Starters) #convert data set to the long format\nlong_boxscores <- gather(boxscores_totals, Stats, Scores, MP:PTS, factor_key = TRUE)\nlong_boxscores$Scores <- as.numeric(long_boxscores$Scores)\nstats_long <- subset(long_boxscores, long_boxscores$Stats == \"FG%\" | long_boxscores$Stats == \"FT%\" |long_boxscores$Stats == \"ORB\" |long_boxscores$Stats == \"DRB\" |long_boxscores$Stats == \"AST\" |long_boxscores$Stats == \"STL\" |long_boxscores$Stats == \"BLK\" |long_boxscores$Stats == \"TOV\" |long_boxscores$Stats == \"PF\" ) #select stats we are interested in\nggplot(data=stats_long, aes(x=Starters, y=Scores, group=Stats, color=Stats))+  #plot the data\n  geom_line() + \n  geom_point() +ggtitle(\"Villanova vs Georgetown box scores stats\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Villanova has more field goal shooting percentage, free throw shooting, turnovers, defensive rebounds and steals. However, Georgetown has more offensive rebounds, personal fouls and assists. They have equal blocks numbers.\n\n####Kansas & Oklahoma stats\n\nWe are importing data from [sport Reference website](https://www.sports-reference.com/cbb/boxscores/1988-04-04-kansas.html).\n\n"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\noboxscores <- read_excel(\"../input/additional-datasets/oboxscores.xlsx\")\nkboxscores <- read_excel(\"../input/additional-datasets/kboxscores.xlsx\")\n\noboxscores_totals <- oboxscores[7, ] #select the total score row\nkboxscores_totals <- kboxscores[11, ]\noboxscores_totals[1,1] <- \"Oklahoma\" #assign team name\nkboxscores_totals[1,1] <- \"Kansas\"\nboxscores_totals <- rbind(oboxscores_totals, kboxscores_totals) #combine the 2 datasets\nboxscores_totals$Starters <- as.factor(boxscores_totals$Starters) #convert data set to the long format\nlong_boxscores <- gather(boxscores_totals, Stats, Scores, MP:PTS, factor_key = TRUE)\nlong_boxscores$Scores <- as.numeric(long_boxscores$Scores)\nstats_long <- subset(long_boxscores, long_boxscores$Stats == \"FG%\" | long_boxscores$Stats == \"FT%\" |long_boxscores$Stats == \"ORB\" |long_boxscores$Stats == \"DRB\" |long_boxscores$Stats == \"AST\" |long_boxscores$Stats == \"STL\" |long_boxscores$Stats == \"BLK\" |long_boxscores$Stats == \"TOV\" |long_boxscores$Stats == \"PF\" ) #select stats we are interested in\nggplot(data=stats_long, aes(x=Starters, y=Scores, group=Stats, color=Stats))+  #plot the data\n  geom_line() + \n  geom_point() +ggtitle(\"Kansas vs Oklahoma box scores stats\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Kansas has more field goal shooting percentage, turnovers, defensive rebounds and blocks. However, Oklahoma has more offensive rebounds, personal fouls, free throw shooting, steals and assists. \n\n####Arizona & Kentucky stats\n\nWe are importing data from [sport Reference website](https://www.sports-reference.com/cbb/boxscores/1997-03-31-arizona.html).\n\n"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\naboxscores <- read_excel(\"../input/additional-datasets/aboxscores.xlsx\")\nknboxscores <- read_excel(\"../input/additional-datasets/knboxscores.xlsx\")\n\naboxscores_totals <- aboxscores[9, ] #select the total score row\nknboxscores_totals <- knboxscores[10, ]\naboxscores_totals[1,1] <- \"Arizona\" #assign team name\nknboxscores_totals[1,1] <- \"Kentucky\"\nboxscores_totals <- rbind(aboxscores_totals, knboxscores_totals) #combine the 2 datasets\nboxscores_totals$Starters <- as.factor(boxscores_totals$Starters) #convert data set to the long format\nlong_boxscores <- gather(boxscores_totals, Stats, Scores, MP:PTS, factor_key = TRUE)\nlong_boxscores$Scores <- as.numeric(long_boxscores$Scores)\nstats_long <- subset(long_boxscores, long_boxscores$Stats == \"FG%\" | long_boxscores$Stats == \"FT%\" |long_boxscores$Stats == \"ORB\" |long_boxscores$Stats == \"DRB\" |long_boxscores$Stats == \"AST\" |long_boxscores$Stats == \"STL\" |long_boxscores$Stats == \"BLK\" |long_boxscores$Stats == \"TOV\" |long_boxscores$Stats == \"PF\" ) #select stats we are interested in\nggplot(data=stats_long, aes(x=Starters, y=Scores, group=Stats, color=Stats))+  #plot the data\n  geom_line() + \n  geom_point() +ggtitle(\"Arizona vs Kentucky box scores stats\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Arizona has more free throw shooting, turnovers, and defensive rebounds. However, Kentucky has more field goal shooting percentage, offensive rebounds, personal fouls,  steals,blocks and assists. \n\nFrom the results above, we can conclude that all Cinderella teams have higher defensive rebounds and turnovers. Two Cinderella teams have higher field goal shooting percentage. All loser teams have high personal fouls and offensive rebounds. \n\n\n###Players selection\n\nFor award players selection, we will look at cinderella teams performance the following year to see if they were able to keep their high performance or not and if keeping award players in the team has an effect on team performance or not. We got teams information from [sport Reference website](https://www.sports-reference.com/).\n\n1- Villanova at 1986, Seed is 10 (lower the previous year), Coach is Rollie Massiminio (the same coach), Villanova failed the second round against Georgia Tech. \n\n2- Kansas at 1989, failed to enter the tournament, Coach is Roy Williams (new coach).\n\n3- Arizona at 1998, Seed is 1 (higher than the previous year), Coach is Lute Oslon(the same coach), Arizona lost the regional tournament final against Utah.\n\nFrom the previous information, we can see that Villanova and Arizona kept the same coach but the results were different. Arizona performed much better than Villanova. Let's explore if players selection has a role or not.\n\nAt this part, we will get players and award players names list from [sport Reference website](https://www.sports-reference.com/) and [Wikepedia](https://www.wikipedia.org/), then we will visualize team players for the two succesive years using Social Networking Analysis (SNA) visualization.\n\n####Villanova team network\n"},{"metadata":{},"cell_type":"markdown","source":"Villanova team members 1985"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"#Villanova 1985 team players data mining\nv1985page <- \"https://www.sports-reference.com/cbb/schools/villanova/1985.html\"  #define the web page\nv1985 <- read_html(v1985page) #read html\n\nnamev1985 <- v1985 %>% #copy Xpath\n  rvest::html_nodes('body') %>% \n  xml2::xml_find_all(\"//*[@id='roster']\") %>% \n  rvest::html_table()\nnamev1985 <- as.data.frame(namev1985)\nnamev1985","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Villanova award players 1985"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"vawardpage <- \"https://en.wikipedia.org/wiki/1984%E2%80%9385_Villanova_Wildcats_men%27s_basketball_team\"\nvaward <- read_html(vawardpage)\naward1985 <- vaward %>%  #copy award players path\n  rvest::html_nodes('body') %>% \n  xml2::xml_find_all(\"//*[@id='mw-content-text']/div/ul\") %>% \n  rvest::html_text()\naward1985","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Villanova 1985 award players are Ed Pinckney, Dwayne McClain, Gary McLain and Harold Jensen"},{"metadata":{},"cell_type":"markdown","source":"Villanova team members 1986"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"#Villanova 1986 team players data mining\nv1986page <- \"https://www.sports-reference.com/cbb/schools/villanova/1986.html\" \nv1986 <- read_html(v1986page)\nnamev1986 <- v1986 %>% \n  rvest::html_nodes('body') %>% \n  xml2::xml_find_all(\"//*[@id='roster']\") %>% \n  rvest::html_table()\nnamev1986 <- as.data.frame(namev1986)\nnamev1986","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we will create SNA datasets to visulize team members with highlighting award players. SNA dataset consists of edges and nodes list. At edge list, we will list players in a cloumn and team in another column. We will have 2 teams Villanova 85 and Villanova 86. At node list, we will add the charachteristics of players and teams for visualization"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"#Edge creation\nv1985e <- select(namev1985, \"Player\") #select player column in a dataset\nv1986e <- select(namev1986, \"Player\")\n\nv1985e$Team <- rep(\"Villanova85\") #Create team column\nv1986e$Team <- rep(\"Villanova86\")\n\nE85_86 <- rbind(v1985e, v1986e) #combining 1985&1986 edges in a one dataset.\n\n#Nodes creation\n\n#Creating attribute column consists of our network elements (team and players)\nn1player <- select(E85_86, \"Player\")\nnames(n1player) <- \"attribute\"\n\nn1team <- select(E85_86, \"Team\")\nnames(n1team) <- \"attribute\"\nN1 <- rbind(n1player, n1team)\nN1 <- unique(N1)\n\n#create team color column to differentiate team for players and award players from non award players\nN1$teamcolor <- ifelse( N1$attribute ==  \"Ed Pinckney\" | N1$attribute ==  \"Dwayne McClain\" | N1$attribute ==  \"Gary McLain\" | N1$attribute ==  \"Harold Jensen\", \"award\", ifelse(N1$attribute == \"Villanova85\" | N1$attribute == \"Villanova86\" , \"team\", \"nonaward\"))\n\n#create team shape column to differentiate team for players\nN1$teamshape <- ifelse(N1$attribute == \"Villanova85\" | N1$attribute == \"Villanova86\", \"team\", \"player\")\n\n#Create SNA object graph from edge and node list, our network will be undirected.\n\ng1 <- graph.data.frame(E85_86, vertices = N1, directed = FALSE)\n\nV(g1)$color <-  ifelse(N1[V(g1), 2] == \"award\", \"green\", ifelse(N1[V(g1), 2] == \"nonaward\",\"red\", \"blue\")) #color awrad players with green, nonaward with red and team with blue\n\nV(g1)$shape <- ifelse(N1[V(g1), 3] == \"player\", \"circle\", \"rectangle\") #shape players as a circle and team as a rectangle\n\n\nplot(g1, vertex.color=V(g1)$color, vertex.shape = V(g1)$shape, vertex.label.cex =0.6, main = \"Villanova 1985&1986 team players network\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As we can observe from the graph, only one 1985 award player(Harold Jansen-green circle) was selected to play in 1986 game. Let's do the same with Kansas first then Arizona.\n\n####Kansas team network"},{"metadata":{},"cell_type":"markdown","source":"Kansas team members 1988"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"#Kansas 1988 team players data mining\nk1988page <- \"https://www.sports-reference.com/cbb/schools/kansas/1988.html\"  #define the web page\nk1988 <- read_html(k1988page) #read html\n\nnamek1988 <- k1988 %>% #copy Xpath\n  rvest::html_nodes('body') %>% \n  xml2::xml_find_all(\"//*[@id='roster']\") %>% \n  rvest::html_table()\nnamek1988 <- as.data.frame(namek1988)\nnamek1988","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Kansas award players 1988"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"kawardpage <- \"https://en.wikipedia.org/wiki/1987%E2%80%9388_Kansas_Jayhawks_men%27s_basketball_team\"\nkaward <- read_html(kawardpage)\naward1988 <- kaward %>%  #copy award players path\n  rvest::html_nodes('body') %>% \n  xml2::xml_find_all(\"/html/body/div[3]/div[3]/div[4]/div/ul\") %>% \n  rvest::html_text()\naward1988","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Kansas 1988 award players are Danny Manning only and Larry Brown was awarded the coach of the year. This may explain changing Kansas coach in the subsequent year resulted in team failure to enter the tournament."},{"metadata":{},"cell_type":"markdown","source":"Kansas team members 1989"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"#Kansas 1989 team players data mining\nk1989page <- \"https://www.sports-reference.com/cbb/schools/kansas/1989.html\" \nk1989 <- read_html(k1989page)\nnamek1989 <- k1989 %>% \n  rvest::html_nodes('body') %>% \n  xml2::xml_find_all(\"//*[@id='roster']\") %>% \n  rvest::html_table()\nnamek1989 <- as.data.frame(namek1989)\nnamek1989","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we will create SNA datasets to visulize Kansas team members with highlighting award players. \n"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"#Edge creation\nk1988e <- select(namek1988, \"Player\") #select player column in a dataset\nk1989e <- select(namek1989, \"Player\")\n\nk1988e$Team <- rep(\"kansas88\") #Create team column\nk1989e$Team <- rep(\"kansas89\")\n\nE88_89 <- rbind(k1988e, k1989e) #combining 1985&1986 edges in a one dataset.\n\n#Nodes creation\n\n#Creating attribute column consists of our network elements (team and players)\nn1player <- select(E88_89, \"Player\")\nnames(n1player) <- \"attribute\"\n\nn1team <- select(E88_89, \"Team\")\nnames(n1team) <- \"attribute\"\nN1 <- rbind(n1player, n1team)\nN1 <- unique(N1)\n\n#create team color column to differentiate team for players and award players from non award players\nN1$teamcolor <- ifelse( N1$attribute ==  \"Danny Manning\" , \"award\", ifelse(N1$attribute == \"kansas88\" | N1$attribute == \"kansas89\" , \"team\", \"nonaward\"))\n\n#create team shape column to differentiate team for players\nN1$teamshape <- ifelse(N1$attribute == \"kansas88\" | N1$attribute == \"kansas89\", \"team\", \"player\")\n\n#Create SNA object graph from edge and node list, our network will be undirected.\n\ng1 <- graph.data.frame(E88_89, vertices = N1, directed = FALSE)\n\nV(g1)$color <-  ifelse(N1[V(g1), 2] == \"award\", \"green\", ifelse(N1[V(g1), 2] == \"nonaward\",\"red\", \"blue\")) #color awrad players with green, nonaward with red and team with blue\n\nV(g1)$shape <- ifelse(N1[V(g1), 3] == \"player\", \"circle\", \"rectangle\") #shape players as a circle and team as a rectangle\n\n\nplot(g1, vertex.color=V(g1)$color, vertex.shape = V(g1)$shape, vertex.label.cex =0.6, main = \"Kansas 1988&1989 team players network\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As we can observe from the graph that the only awarded player was not selected as well as the awarded coach. This may explain kansas failure to enter the tournament.\n\n####Arizona team network\n"},{"metadata":{},"cell_type":"markdown","source":"Arizona team members 1997"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\n#Arizona 1997 team players data mining\nA1997page <- \"https://www.sports-reference.com/cbb/schools/arizona/1997.html\"  #define the web page\nA1997 <- read_html(A1997page) #read html\n\nnameA1997 <- A1997 %>% #copy Xpath\n  rvest::html_nodes('body') %>% \n  xml2::xml_find_all(\"//*[@id='roster']\") %>% \n  rvest::html_table()\nnameA1997 <- as.data.frame(nameA1997)\nnameA1997","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Arizona award players 1997"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"Aawardpage <- \"https://arizonawildcats.com/sports/2013/12/16/209343001.aspx\"\nAaward <- read_html(Aawardpage)\naward1997 <- Aaward %>%  #copy award players path\n  rvest::html_nodes('body') %>% \n  xml2::xml_find_all(\"/html/body/form/main/div/div/article/div[3]/div[4]\") %>% \n  rvest::html_text()\naward1997","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"There is no information about Ariazona award players in Wikepedia as other teams but I managed to get information about players with highest performance from [Arizona wildcats](https://arizonawildcats.com/sports/2013/12/16/209343001.aspx). Award players are Miles Simon, Michael Dickerson and Jason Terry.\n"},{"metadata":{},"cell_type":"markdown","source":"Arizona team members 1998"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"#Arizona 1998 team players data mining\nA1998page <- \"https://www.sports-reference.com/cbb/schools/arizona/1998.html\" \nA1998 <- read_html(A1998page)\nnameA1998 <- A1998 %>% \n  rvest::html_nodes('body') %>% \n  xml2::xml_find_all(\"//*[@id='roster']\") %>% \n  rvest::html_table()\nnameA1998 <- as.data.frame(nameA1998)\nnameA1998","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we will create SNA datasets to visulize Arizona team members with highlighting award players. "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"#Edge creation\nA1997e <- select(nameA1997, \"Player\") #select player column in a dataset\nA1998e <- select(nameA1998, \"Player\")\n\nA1997e$Team <- rep(\"Arizona97\") #Create team column\nA1998e$Team <- rep(\"Arizona98\")\n\nE97_98 <- rbind(A1997e, A1998e) #combining 1985&1986 edges in a one dataset.\n\n#Nodes creation\n\n#Creating attribute column consists of our network elements (team and players)\nn1player <- select(E97_98, \"Player\")\nnames(n1player) <- \"attribute\"\n\nn1team <- select(E97_98, \"Team\")\nnames(n1team) <- \"attribute\"\nN1 <- rbind(n1player, n1team)\nN1 <- unique(N1)\n\n#create team color column to differentiate team for players and award players from non award players\nN1$teamcolor <- ifelse( N1$attribute ==  \"Miles Simon\" | N1$attribute == \"Michael Dickerson\" | N1$attribute == \"Jason Terry\", \"award\", ifelse(N1$attribute == \"Arizona97\" | N1$attribute == \"Arizona98\" , \"team\", \"nonaward\"))\n\n#create team shape column to differentiate team for players\nN1$teamshape <- ifelse(N1$attribute == \"Arizona97\" | N1$attribute == \"Arizona98\", \"team\", \"player\")\n\n#Create SNA object graph from edge and node list, our network will be undirected.\n\ng1 <- graph.data.frame(E97_98, vertices = N1, directed = FALSE)\n\nV(g1)$color <-  ifelse(N1[V(g1), 2] == \"award\", \"green\", ifelse(N1[V(g1), 2] == \"nonaward\",\"red\", \"blue\")) #color awrad players with green, nonaward with red and team with blue\n\nV(g1)$shape <- ifelse(N1[V(g1), 3] == \"player\", \"circle\", \"rectangle\") #shape players as a circle and team as a rectangle\n\n\nplot(g1, vertex.color=V(g1)$color, vertex.shape = V(g1)$shape, vertex.label.cex =0.6, main = \"Arizona 1997&1998 team players network\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As we can see from the graph, all awarded players were selected. Despite Arizona did not win the tournament but it did good job by winning the regional semifinal against Maryland.\n\nWe can conclude from all teams network that selecting award players increase team chances to win the tournament and they could be an important factor for cinderella upset.\n\n\n\n##Location\n\n\nAs the location in the tournament is always Neutral, we will explore regular season loction for the three cinderella teams.\n\n"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"#get the teams location\nVillanove_loc <- subset(reg_season_compact, reg_season_compact$WTeamID == \"1437\" & reg_season_compact$Season == \"1985\")\nKansas_loc <- subset(reg_season_compact,  reg_season_compact$WTeamID == \"1242\" & reg_season_compact$Season == \"1988\" )\nArizona_loc <- subset(reg_season_compact,  reg_season_compact$WTeamID == \"1112\" & reg_season_compact$Season == \"1997\")\n\n#plot data\np1 <- ggplot(data = Villanove_loc, aes(x = WLoc)) + geom_bar(fill = c(\"cadetblue4\", \"chartreuse3\", \"coral3\")) + xlab(\"Villanova Team Location\") + theme(axis.text = element_text(angle = 360, hjust = 1, vjust = 0)) \n\np2 <- ggplot(data = Kansas_loc, aes(x = WLoc)) + geom_bar(fill = c(\"cadetblue4\", \"chartreuse3\", \"coral3\")) + xlab(\"Kansas Team Location\") + theme(axis.text = element_text(angle = 360, hjust = 1, vjust = 0))\n\np3 <- ggplot(data = Arizona_loc, aes(x = WLoc)) + geom_bar(fill = c(\"cadetblue4\", \"chartreuse3\", \"coral3\")) + xlab(\"Arizona Team Location\") + theme(axis.text = element_text(angle = 360, hjust = 1, vjust = 0))\n\ngrid.arrange(p1, p2, p3, ncol = 3, top = \"Cinderella teams regular season location\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Villanova has played games at home as same as away from home. Kansas played a little bit higher at home while Arizona played most of games at home. Home location could have a positive relationship with winning.\n\n#Conclusion\n\nAt this notebook, we analyzed NCAA tournament Championship cinderella upsets to explore the mutual winning factors among teams. From our analysis, we can conclude the following:\n\n1- All Cinderella teams coaches have a strong tournament first round history.\n\n2- All Cinderella teams have defensive rebounds and turnovers higher than the opponent team and less personal fouls.\n\n3-Players with high performance play an important role as we have found that neutralizing award players in the successive season resulted in team failure to eneter the tournament while teams kept their award players were successful to eneter the tournament with high performance.\n\n4- Home location could be related to winning the tournament. 2 out of 3 Cinderella teams played regular season games at home more than away. "}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat":4,"nbformat_minor":4}