{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "b8b04cd4-1a00-571c-664a-948e44ee6771"
      },
      "source": [
        "This is a visualization of the win % by the higher seed for all tourney games in the input data set."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d0b28c98-05a2-9171-704e-87c7a7b9631d"
      },
      "outputs": [],
      "source": [
        "library(dplyr)\n",
        "library(ggplot2)\n",
        "library(scales)\n",
        "library(RColorBrewer)\n",
        "library(readr) # CSV file I/O, e.g. the read_csv function\n",
        "\n",
        "tcr <- read.csv(\"../input/TourneyCompactResults.csv\")\n",
        "seeds <- read.csv(\"../input/TourneySeeds.csv\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d2db4045-4680-dcce-9695-8b88627127d0"
      },
      "outputs": [],
      "source": [
        "#merge seeds and results\n",
        "tcr <- merge(tcr, seeds, by.x = c(\"Wteam\",\"Season\"), by.y = c(\"Team\",\"Season\"))\n",
        "tcr <- merge(tcr, seeds, by.x = c(\"Lteam\",\"Season\"), by.y = c(\"Team\",\"Season\"))\n",
        "names(tcr)[9:10] <- c(\"Wseed\",\"Lseed\")\n",
        "\n",
        "#clean up seeds to numerical only\n",
        "tcr$Wrank <- as.numeric(gsub('[a-z]','',tolower(tcr$Wseed)))\n",
        "tcr$Lrank <- as.numeric(gsub('[a-z]','',tolower(tcr$Lseed)))\n",
        "\n",
        "#Getting total wins and loses by matchup by flipping the win and loss seed\n",
        "rnk_w <- tcr %>% group_by(Wrank,Lrank) %>% summarize(wins=n())\n",
        "rnk_flip <- tcr %>% group_by(Lrank,Wrank) %>% summarize(loses=n())\n",
        "\n",
        "#merging the two together so that wins and losses by seed combo are on the same row\n",
        "rnk_tot <- merge(rnk_w,rnk_flip, by.x = c(\"Wrank\",\"Lrank\"), by.y = c(\"Lrank\",\"Wrank\"), all.x=T)\n",
        "head(rnk_tot)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2b61d233-199a-710d-5626-0ace2a50a496"
      },
      "outputs": [],
      "source": [
        "#now make the rows unique by tagging the higher and lower seed explicitly (regardless of W or L) \n",
        "#then using this to deduplicate.  This will mean row with higher seed win % is always kept.\n",
        "rnk_tot <- rnk_tot %>% mutate(hseed=ifelse(Wrank <= Lrank,Wrank,Lrank),\n",
        "                              lseed=ifelse(Wrank >= Lrank,Wrank,Lrank))\n",
        "rnk_tot <- rnk_tot %>% filter(!duplicated(paste0(hseed,lseed)))\n",
        "\n",
        "#recode na to 0 where a given seed has never lost in a matchup\n",
        "rnk_tot$loses[is.na(rnk_tot$loses)] <- 0\n",
        "\n",
        "#calculate n, win probalbility, and make a pcnt label \n",
        "rnk_tot <- rnk_tot %>% mutate(n=wins+loses, \n",
        "                              win_pcnt=wins/n,\n",
        "                              win_pcnt_label=paste0(round(win_pcnt*100,0),'%'))\n",
        "head(rnk_tot)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e306c9ac-6c9f-a0ca-0776-b9e17db8db1e"
      },
      "outputs": [],
      "source": [
        "ggplot(data=rnk_tot,aes(x=factor(lseed),y=factor(hseed),fill=win_pcnt)) + \n",
        "  geom_tile() +\n",
        "  scale_fill_distiller(palette=\"YlGnBu\") +\n",
        "  geom_text(label=rnk_tot$win_pcnt_label,size=3,vjust=-.5) +\n",
        "  geom_text(label=rnk_tot$n,size=2,vjust=1) +\n",
        "  ggtitle(\"Win % by seeds in NCAA tournament (1985 to 2016) \\nHigher seed's win %, Number of matchups\") +\n",
        "  ylab(\"Higher Seed\") +\n",
        "  xlab(\"Lower Seed\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "66a6f415-a31b-ae20-1c6e-9b0933128569"
      },
      "source": [
        "### Some observations:\n",
        "* Because of the tourney structure many lower seeds have never been matched up.  This matrix is   sparser than I had realized.\n",
        "* 2 seeds are 0-4 against 5 seeds, and 1 seeds are 3-3 against 11s.\n",
        "* A 15 seed has never been the higher seed in any game."
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "R",
      "language": "R",
      "name": "ir"
    },
    "language_info": {
      "codemirror_mode": "r",
      "file_extension": ".r",
      "mimetype": "text/x-r-source",
      "name": "R",
      "pygments_lexer": "r",
      "version": "3.3.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}