Advertisement
fuzzybluerain

2023 FIFA Women's World Cup Predictions

Jul 19th, 2023
1,499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 22.72 KB | Sports | 0 0
  1. wosodataset <- read.csv("~/wosodataset.csv")
  2.  
  3. fullteams <- sort(unique(c(sort(unique(wosodataset$Home)), sort(unique(wosodataset$Away)))))
  4. datasetmatrix <- data.frame(matrix(nrow = 177, ncol = 177))
  5. rownames(datasetmatrix) <- fullteams
  6. colnames(datasetmatrix) <- fullteams
  7. datasetmatrix[is.na(datasetmatrix)] <- 0
  8.  
  9. rawmatrix <- data.frame(matrix(nrow = 177, ncol = 12))
  10. rownames(rawmatrix) <- fullteams
  11. colnames(rawmatrix) <- c("Games", "GF", "GFPG", "xGF", "xGFPG", "FDiff", "GA", "GAPG", "xGA", "xGAPG", "ADiff", "TDiff")
  12. rawmatrix[is.na(rawmatrix)] <- 0
  13.  
  14. for(i in 1:nrow(wosodataset)) { # figure out how to add to the matchup based on what data you have
  15.   hometeam <- wosodataset[i, "Home"]; awayteam <- wosodataset[i, "Away"]
  16.   datasetmatrix[match(hometeam,rownames(datasetmatrix)),match(awayteam,colnames(datasetmatrix))] <- datasetmatrix[match(hometeam,rownames(datasetmatrix)),match(awayteam,colnames(datasetmatrix))] + 1
  17.   datasetmatrix[match(awayteam,rownames(datasetmatrix)),match(hometeam,colnames(datasetmatrix))] <- datasetmatrix[match(awayteam,rownames(datasetmatrix)),match(hometeam,colnames(datasetmatrix))] + 1
  18.   rawmatrix[match(hometeam,rownames(rawmatrix)),"GF"] <- rawmatrix[match(hometeam,rownames(rawmatrix)),"GF"] + wosodataset[i, "HomeGoals"]
  19.   rawmatrix[match(hometeam,rownames(rawmatrix)),"GA"] <- rawmatrix[match(hometeam,rownames(rawmatrix)),"GA"] + wosodataset[i, "AwayGoals"]
  20.   rawmatrix[match(awayteam,rownames(rawmatrix)),"GF"] <- rawmatrix[match(awayteam,rownames(rawmatrix)),"GF"] + wosodataset[i, "AwayGoals"]
  21.   rawmatrix[match(awayteam,rownames(rawmatrix)),"GA"] <- rawmatrix[match(awayteam,rownames(rawmatrix)),"GA"] + wosodataset[i, "HomeGoals"]
  22. }
  23.  
  24. for(i in fullteams) {
  25.   rawmatrix[i, "Games"] <- sum(mapply(sum, datasetmatrix[i,c(1:177)]))
  26.   rawmatrix[i, "GFPG"] <- rawmatrix[i, "GF"] / rawmatrix[i, "Games"]
  27.   rawmatrix[i, "GAPG"] <- rawmatrix[i, "GA"] / rawmatrix[i, "Games"]
  28. }
  29. for(i in fullteams) {
  30.   for(j in fullteams) {
  31.     rawmatrix[i, "xGF"] <- rawmatrix[i, "xGF"] + (((rawmatrix[i, "GFPG"] + rawmatrix[j, "GAPG"]) / 2) * datasetmatrix[i, j])
  32.     rawmatrix[i, "xGA"] <- rawmatrix[i, "xGA"] + (((rawmatrix[i, "GAPG"] + rawmatrix[j, "GFPG"]) / 2) * datasetmatrix[i, j])
  33.   }
  34.   rawmatrix[i, "xGFPG"] <- rawmatrix[i, "xGF"] / rawmatrix[i, "Games"]
  35.   rawmatrix[i, "xGAPG"] <- rawmatrix[i, "xGA"] / rawmatrix[i, "Games"]
  36.   rawmatrix[i, "FDiff"] <- rawmatrix[i, "GFPG"] - rawmatrix[i, "xGFPG"]
  37.   rawmatrix[i, "ADiff"] <- rawmatrix[i, "GAPG"] - rawmatrix[i, "xGAPG"]
  38.   rawmatrix[i, "TDiff"] <- rawmatrix[i, "FDiff"] - rawmatrix[i, "ADiff"]
  39. }
  40.  
  41. teams <- c("Argentina", "Australia", "Brazil", "Canada", "China PR", "Colombia", "Costa Rica", "Denmark", "England", "France", "Germany", "Haiti", "Italy", "Jamaica", "Japan", "Korea Republic", "Morocco", "Netherlands", "New Zealand", "Nigeria", "Norway", "Panama", "Philippines", "Portugal", "Rep. of Ireland", "South Africa", "Spain", "Sweden", "Switzerland", "USA", "Vietnam", "Zambia")
  42.  
  43. WCmultiplier <- 0 # adjust this as tournament goes on
  44. WCform <- read.csv("~/WCform.csv") # having to manually update the form, very sorry
  45. rownames(WCform) <- teams # can't fix this for myself easily, sadly
  46.  
  47. adjmatrix <- data.frame(matrix(nrow = 32, ncol = 8))
  48. rownames(adjmatrix) <- teams
  49. colnames(adjmatrix) <- c("RawF", "RawA", "WCF", "WCA", "HGF", "HGA", "AdjF", "AdjA")
  50. adjmatrix[is.na(adjmatrix)] <- 0
  51.  
  52. # home ground advantage for Australia and New Zealand, will be inaccurate if NZ makes the final
  53. adjmatrix["Australia", "HGF"] <- 0.2
  54. adjmatrix["Australia", "HGA"] <- -0.2
  55. adjmatrix["New Zealand", "HGF"] <- 0.2
  56. adjmatrix["New Zealand", "HGA"] <- -0.2
  57.  
  58. for(i in teams) {
  59.   adjmatrix[i, "RawF"] <- rawmatrix[i, "FDiff"]
  60.   adjmatrix[i, "RawA"] <- rawmatrix[i, "ADiff"]
  61.   adjmatrix[i, "WCF"] <- WCform[i, "WCF"]
  62.   adjmatrix[i, "WCA"] <- WCform[i, "WCA"]
  63.   adjmatrix[i, "AdjF"] <- ((adjmatrix[i, "RawF"]*(1-WCmultiplier))+(adjmatrix[i, "WCF"]*WCmultiplier)+adjmatrix[i,"HGF"])
  64.   adjmatrix[i, "AdjA"] <- ((adjmatrix[i, "RawA"]*(1-WCmultiplier))+(adjmatrix[i, "WCA"]*WCmultiplier)+adjmatrix[i,"HGA"])
  65. }
  66.  
  67. xG_matrix <- data.frame(matrix(nrow = 32, ncol = 32))
  68. rownames(xG_matrix) <- teams
  69. colnames(xG_matrix) <- teams
  70. for(i in teams){
  71.   for(j in teams){
  72.     xG_matrix[j, i] <- (sum(rawmatrix$GF) / sum(rawmatrix$Games)) + adjmatrix[i, "AdjF"] + adjmatrix[j, "AdjA"]
  73.   }
  74. }
  75.  
  76. finish_matrix <- data.frame(matrix(nrow = 32, ncol = 9))
  77. finish_matrix[is.na(finish_matrix)] <- 0
  78. rownames(finish_matrix) <- teams
  79. colnames(finish_matrix) <- c("Group 1st", "Group 2nd", "Group 3rd", "Group 4th", "Round of 16", "Quarter Finals", "Semi Finals", "Final", "Champion")
  80.  
  81. match_schedule <- read.csv("~/2023wcfixture.csv")
  82.  
  83. grouptotal <- c(1:48)
  84. r16total <- c(49:56)
  85. qftotal <- c(57:60)
  86. sftotal <- c(61,62)
  87. medaltotal <- c(63,64)
  88.  
  89. simcount <- c(1:10000)
  90.  
  91. for(count in simcount){
  92.  
  93.   ## FIXTURE PREDICTING (GROUP STAGE)
  94.   # note to future Claire: *away* teams first
  95.   for (val in grouptotal){
  96.     gamenumber <- val
  97.     match_schedule[gamenumber, "HomexG"] <- xG_matrix[match(match_schedule[gamenumber, "Away"],rownames(xG_matrix)),match(match_schedule[gamenumber, "Home"],colnames(xG_matrix))]
  98.     match_schedule[gamenumber, "AwayxG"] <- xG_matrix[match(match_schedule[gamenumber, "Home"],rownames(xG_matrix)),match(match_schedule[gamenumber, "Away"],colnames(xG_matrix))]
  99.     }
  100.  
  101.   ## FIXTURE SIMULATING (GROUP STAGE)
  102.   for (val in grouptotal){
  103.     gamenumber <- val
  104.     match_schedule[gamenumber, "HomeG"] <- rpois(1,match_schedule[gamenumber, "HomexG"])
  105.     match_schedule[gamenumber, "AwayG"] <- rpois(1,match_schedule[gamenumber, "AwayxG"])
  106.     }
  107.  
  108.   match_schedule$HomeDiff <- match_schedule$HomeG - match_schedule$AwayG; match_schedule$AwayDiff <- match_schedule$AwayG - match_schedule$HomeG
  109.   # this comes in handy when we sort the group stages later
  110.  
  111.   ## POINT ALLOCATING (GROUP STAGE)
  112.   for (val in grouptotal){
  113.     gamenumber <- val
  114.     if (match_schedule[gamenumber, "HomeG"] > match_schedule[gamenumber, "AwayG"]){
  115.       match_schedule[gamenumber, "HomePts"] <- 3
  116.       match_schedule[gamenumber, "AwayPts"] <- 0
  117.       match_schedule[gamenumber, "HomeW"] <- match_schedule[gamenumber, "HomeW"] + 1
  118.       } else if (match_schedule[gamenumber, "HomeG"] < match_schedule[gamenumber, "AwayG"]){
  119.         match_schedule[gamenumber, "HomePts"] <- 0
  120.         match_schedule[gamenumber, "AwayPts"] <- 3
  121.         match_schedule[gamenumber, "AwayW"] <- match_schedule[gamenumber, "AwayW"] + 1
  122.         } else {
  123.           match_schedule[gamenumber, "HomePts"] <- 1
  124.           match_schedule[gamenumber, "AwayPts"] <- 1
  125.           match_schedule[gamenumber, "Draw"] <- match_schedule[gamenumber, "Draw"] + 1
  126.         }
  127.     }
  128.  
  129.   ## GROUP TABLE GENERATION
  130.   groupresults <- array()
  131.  
  132.   for (var in c("A", "B", "C", "D", "E", "F", "G", "H")) {
  133.     groupnumber <- var
  134.     table <- match_schedule[match_schedule$Group == var, ]
  135.     table <- subset(table, select = -c(Match, Stage, Group, Date, Venue, HomexG, AwayxG))
  136.     table <- cbind(stack(table[1:2]),stack(table[3:4]),stack(table[5:6]),stack(table[10:11]))
  137.     colnames(table) <- c("Team", NA, "GF", NA, "Pts", NA, "GD", NA)
  138.     table <- subset(table, select = c(Team, GF, Pts, GD))
  139.     table <- table[, c(1, 2, 4, 3)]
  140.     table <- data.frame(c(aggregate(table$GF, list(table$Team), FUN=sum),
  141.                           aggregate(table$GD, list(table$Team), FUN=sum),
  142.                           aggregate(table$Pts, list(table$Team), FUN=sum)))
  143.     colnames(table) <- c("Team", "GF", NA, "GD", NA, "Pts")
  144.     table <- subset(table, select = c(Team, GF, GD, Pts))
  145.     table <- table[with(table, order(-Pts, -GD, -GF)), ]
  146.     groupresults <- append(groupresults, table)
  147.   }
  148.  
  149.   ## ROUND OF 16 SCHEDULING
  150.   match_schedule[49,"Home"] <- groupresults[[ 2]][[1]]; match_schedule[49,"Away"] <- groupresults[[10]][[2]]
  151.   match_schedule[50,"Home"] <- groupresults[[10]][[1]]; match_schedule[50,"Away"] <- groupresults[[ 2]][[2]]
  152.   match_schedule[51,"Home"] <- groupresults[[ 6]][[1]]; match_schedule[51,"Away"] <- groupresults[[14]][[2]]
  153.   match_schedule[52,"Home"] <- groupresults[[14]][[1]]; match_schedule[52,"Away"] <- groupresults[[ 6]][[2]]
  154.   match_schedule[53,"Home"] <- groupresults[[18]][[1]]; match_schedule[53,"Away"] <- groupresults[[26]][[2]]
  155.   match_schedule[54,"Home"] <- groupresults[[26]][[1]]; match_schedule[54,"Away"] <- groupresults[[18]][[2]]
  156.   match_schedule[55,"Home"] <- groupresults[[22]][[1]]; match_schedule[55,"Away"] <- groupresults[[30]][[2]]
  157.   match_schedule[56,"Home"] <- groupresults[[30]][[1]]; match_schedule[56,"Away"] <- groupresults[[22]][[2]]
  158.  
  159.   ## FIXTURE PREDICTING (KNOCKOUT STAGE)
  160.   for (val in r16total){
  161.     gamenumber <- val
  162.     match_schedule[gamenumber, "HomexG"] <- xG_matrix[match(match_schedule[gamenumber, "Away"],rownames(xG_matrix)),match(match_schedule[gamenumber, "Home"],colnames(xG_matrix))]
  163.     match_schedule[gamenumber, "AwayxG"] <- xG_matrix[match(match_schedule[gamenumber, "Home"],rownames(xG_matrix)),match(match_schedule[gamenumber, "Away"],colnames(xG_matrix))]
  164.     match_schedule[gamenumber, "HomeG"] <- rpois(1,match_schedule[gamenumber, "HomexG"])
  165.     match_schedule[gamenumber, "AwayG"] <- rpois(1,match_schedule[gamenumber, "AwayxG"])
  166.     if (match_schedule[gamenumber, "HomeG"] > match_schedule[gamenumber, "AwayG"]){
  167.       match_schedule[gamenumber, "HomePts"] <- 3
  168.       match_schedule[gamenumber, "AwayPts"] <- 0
  169.       } else if (match_schedule[gamenumber, "HomeG"] < match_schedule[gamenumber, "AwayG"]){
  170.         match_schedule[gamenumber, "AwayPts"] <- 3
  171.         match_schedule[gamenumber, "HomePts"] <- 0
  172.         } else {
  173.           match_schedule[gamenumber, "HomeG"] <- match_schedule[gamenumber, "HomeG"] + rpois(1,(match_schedule[gamenumber, "HomexG"])/2)
  174.           match_schedule[gamenumber, "AwayG"] <- match_schedule[gamenumber, "AwayG"] + rpois(1,(match_schedule[gamenumber, "AwayxG"])/2)
  175.           if (match_schedule[gamenumber, "HomeG"] > match_schedule[gamenumber, "AwayG"]){
  176.             match_schedule[gamenumber, "HomePts"] <- 3
  177.             match_schedule[gamenumber, "AwayPts"] <- 0
  178.             } else if (match_schedule[gamenumber, "HomeG"] < match_schedule[gamenumber, "AwayG"]){
  179.               match_schedule[gamenumber, "AwayPts"] <- 3
  180.               match_schedule[gamenumber, "HomePts"] <- 0
  181.               } else {
  182.                 penalties <- runif(1)
  183.                 if (penalties > 0.5){
  184.                   match_schedule[gamenumber, "HomePts"] <- 3
  185.                   match_schedule[gamenumber, "AwayPts"] <- 0
  186.                   } else {
  187.                     match_schedule[gamenumber, "AwayPts"] <- 3
  188.                     match_schedule[gamenumber, "HomePts"] <- 0
  189.                   }
  190.               }
  191.         }
  192.     }
  193.  
  194.   ## QUARTER FINAL SCHEDULE
  195.   if (match_schedule[49, "HomePts"] == 3){
  196.     match_schedule[57,"Home"] <- match_schedule[49,"Home"]
  197.     } else {
  198.       match_schedule[57,"Home"] <- match_schedule[49,"Away"]
  199.       }
  200.   if (match_schedule[51, "HomePts"] == 3){
  201.     match_schedule[57,"Away"] <- match_schedule[51,"Home"]
  202.     } else {
  203.       match_schedule[57,"Away"] <- match_schedule[51,"Away"]
  204.       }
  205.   if (match_schedule[53, "HomePts"] == 3){
  206.     match_schedule[59,"Home"] <- match_schedule[53,"Home"]
  207.     } else {
  208.       match_schedule[59,"Home"] <- match_schedule[53,"Away"]
  209.       }
  210.   if (match_schedule[55, "HomePts"] == 3){
  211.     match_schedule[59,"Away"] <- match_schedule[55,"Home"]
  212.     } else {
  213.       match_schedule[59,"Away"] <- match_schedule[55,"Away"]
  214.       }
  215.   if (match_schedule[50, "HomePts"] == 3){
  216.     match_schedule[58,"Home"] <- match_schedule[50,"Home"]
  217.     } else {
  218.       match_schedule[58,"Home"] <- match_schedule[50,"Away"]
  219.       }
  220.    if (match_schedule[52, "HomePts"] == 3){
  221.      match_schedule[58,"Away"] <- match_schedule[52,"Home"]
  222.    } else {
  223.      match_schedule[58,"Away"] <- match_schedule[52,"Away"]
  224.    }
  225.    if (match_schedule[54, "HomePts"] == 3){
  226.      match_schedule[60,"Home"] <- match_schedule[54,"Home"]
  227.    } else {
  228.      match_schedule[60,"Home"] <- match_schedule[54,"Away"]
  229.    }
  230.    if (match_schedule[56, "HomePts"] == 3){
  231.      match_schedule[60,"Away"] <- match_schedule[56,"Home"]
  232.    } else {
  233.      match_schedule[60,"Away"] <- match_schedule[56,"Away"]
  234.    }
  235.  
  236.   ## FIXTURE SIMULATING (KNOCKOUT STAGE)
  237.    for (val in qftotal){
  238.   gamenumber <- val
  239.   match_schedule[gamenumber, "HomexG"] <- xG_matrix[match(match_schedule[gamenumber, "Away"],rownames(xG_matrix)),match(match_schedule[gamenumber, "Home"],colnames(xG_matrix))]
  240.   match_schedule[gamenumber, "AwayxG"] <- xG_matrix[match(match_schedule[gamenumber, "Home"],rownames(xG_matrix)),match(match_schedule[gamenumber, "Away"],colnames(xG_matrix))]
  241.   match_schedule[gamenumber, "HomeG"] <- rpois(1,match_schedule[gamenumber, "HomexG"])
  242.   match_schedule[gamenumber, "AwayG"] <- rpois(1,match_schedule[gamenumber, "AwayxG"])
  243.   if (match_schedule[gamenumber, "HomeG"] > match_schedule[gamenumber, "AwayG"]){
  244.     match_schedule[gamenumber, "HomePts"] <- 3
  245.     match_schedule[gamenumber, "AwayPts"] <- 0
  246.   } else if (match_schedule[gamenumber, "HomeG"] < match_schedule[gamenumber, "AwayG"]){
  247.     match_schedule[gamenumber, "AwayPts"] <- 3
  248.     match_schedule[gamenumber, "HomePts"] <- 0
  249.   } else {
  250.     match_schedule[gamenumber, "HomeG"] <- match_schedule[gamenumber, "HomeG"] + rpois(1,(match_schedule[gamenumber, "HomexG"])/2)
  251.     match_schedule[gamenumber, "AwayG"] <- match_schedule[gamenumber, "AwayG"] + rpois(1,(match_schedule[gamenumber, "AwayxG"])/2)
  252.     if (match_schedule[gamenumber, "HomeG"] > match_schedule[gamenumber, "AwayG"]){
  253.       match_schedule[gamenumber, "HomePts"] <- 3
  254.       match_schedule[gamenumber, "AwayPts"] <- 0
  255.     } else if (match_schedule[gamenumber, "HomeG"] < match_schedule[gamenumber, "AwayG"]){
  256.       match_schedule[gamenumber, "AwayPts"] <- 3
  257.       match_schedule[gamenumber, "HomePts"] <- 0
  258.     } else {
  259.       penalties <- runif(1)
  260.       if (penalties > 0.5){
  261.         match_schedule[gamenumber, "HomePts"] <- 3
  262.         match_schedule[gamenumber, "AwayPts"] <- 0
  263.       } else {
  264.         match_schedule[gamenumber, "AwayPts"] <- 3
  265.         match_schedule[gamenumber, "HomePts"] <- 0
  266.       }
  267.     }
  268.   }
  269. }
  270.  
  271. ## SEMI FINAL SCHEDULE
  272.    if (match_schedule[57, "HomePts"] == 3){
  273.      match_schedule[61,"Home"] <- match_schedule[57,"Home"]
  274.    } else {
  275.      match_schedule[61,"Home"] <- match_schedule[57,"Away"]
  276.    }
  277.    if (match_schedule[58, "HomePts"] == 3){
  278.      match_schedule[61,"Away"] <- match_schedule[58,"Home"]
  279.    } else {
  280.      match_schedule[61,"Away"] <- match_schedule[58,"Away"]
  281.    }
  282.    if (match_schedule[59, "HomePts"] == 3){
  283.      match_schedule[62,"Home"] <- match_schedule[59,"Home"]
  284.    } else {
  285.      match_schedule[62,"Home"] <- match_schedule[59,"Away"]
  286.    }
  287.    if (match_schedule[60, "HomePts"] == 3){
  288.      match_schedule[62,"Away"] <- match_schedule[60,"Home"]
  289.    } else {
  290.      match_schedule[62,"Away"] <- match_schedule[60,"Away"]
  291.    }
  292.  
  293.   ## FIXTURE SIMULATING (KNOCKOUT STAGE)
  294.   for (val in sftotal){
  295.     gamenumber <- val
  296.     match_schedule[gamenumber, "HomexG"] <- xG_matrix[match(match_schedule[gamenumber, "Away"],rownames(xG_matrix)),match(match_schedule[gamenumber, "Home"],colnames(xG_matrix))]
  297.     match_schedule[gamenumber, "AwayxG"] <- xG_matrix[match(match_schedule[gamenumber, "Home"],rownames(xG_matrix)),match(match_schedule[gamenumber, "Away"],colnames(xG_matrix))]
  298.     match_schedule[gamenumber, "HomeG"] <- rpois(1,match_schedule[gamenumber, "HomexG"])
  299.     match_schedule[gamenumber, "AwayG"] <- rpois(1,match_schedule[gamenumber, "AwayxG"])
  300.     if (match_schedule[gamenumber, "HomeG"] > match_schedule[gamenumber, "AwayG"]){
  301.       match_schedule[gamenumber, "HomePts"] <- 3
  302.       match_schedule[gamenumber, "AwayPts"] <- 0
  303.     } else if (match_schedule[gamenumber, "HomeG"] < match_schedule[gamenumber, "AwayG"]){
  304.       match_schedule[gamenumber, "AwayPts"] <- 3
  305.       match_schedule[gamenumber, "HomePts"] <- 0
  306.     } else {
  307.       match_schedule[gamenumber, "HomeG"] <- match_schedule[gamenumber, "HomeG"] + rpois(1,(match_schedule[gamenumber, "HomexG"])/2)
  308.       match_schedule[gamenumber, "AwayG"] <- match_schedule[gamenumber, "AwayG"] + rpois(1,(match_schedule[gamenumber, "AwayxG"])/2)
  309.       if (match_schedule[gamenumber, "HomeG"] > match_schedule[gamenumber, "AwayG"]){
  310.         match_schedule[gamenumber, "HomePts"] <- 3
  311.         match_schedule[gamenumber, "AwayPts"] <- 0
  312.       } else if (match_schedule[gamenumber, "HomeG"] < match_schedule[gamenumber, "AwayG"]){
  313.         match_schedule[gamenumber, "AwayPts"] <- 3
  314.         match_schedule[gamenumber, "HomePts"] <- 0
  315.       } else {
  316.         penalties <- runif(1)
  317.         if (penalties > 0.5){
  318.           match_schedule[gamenumber, "HomePts"] <- 3
  319.           match_schedule[gamenumber, "AwayPts"] <- 0
  320.         } else {
  321.           match_schedule[gamenumber, "AwayPts"] <- 3
  322.           match_schedule[gamenumber, "HomePts"] <- 0
  323.         }
  324.       }
  325.     }
  326.   }
  327.  
  328.   ## MEDAL GAME SCHEDULES
  329.    if (match_schedule[61, "HomePts"] == 3){
  330.      match_schedule[64,"Home"] <- match_schedule[61,"Home"]
  331.      match_schedule[63,"Home"] <- match_schedule[61,"Away"]
  332.    } else {
  333.      match_schedule[64,"Home"] <- match_schedule[61,"Away"]
  334.      match_schedule[63,"Home"] <- match_schedule[61,"Home"]
  335.    }
  336.   if (match_schedule[62, "HomePts"] == 3){
  337.     match_schedule[64,"Away"] <- match_schedule[62,"Home"]
  338.     match_schedule[63,"Away"] <- match_schedule[62,"Away"]
  339.   } else {
  340.     match_schedule[64,"Away"] <- match_schedule[62,"Away"]
  341.     match_schedule[63,"Away"] <- match_schedule[62,"Home"]
  342.   }
  343.  
  344.   ## FIXTURE SIMULATING (MEDAL GAMES)
  345.   for (val in medaltotal){
  346.     gamenumber <- val
  347.     match_schedule[gamenumber, "HomexG"] <- xG_matrix[match(match_schedule[gamenumber, "Away"],rownames(xG_matrix)),match(match_schedule[gamenumber, "Home"],colnames(xG_matrix))]
  348.     match_schedule[gamenumber, "AwayxG"] <- xG_matrix[match(match_schedule[gamenumber, "Home"],rownames(xG_matrix)),match(match_schedule[gamenumber, "Away"],colnames(xG_matrix))]
  349.     match_schedule[gamenumber, "HomeG"] <- rpois(1,match_schedule[gamenumber, "HomexG"])
  350.     match_schedule[gamenumber, "AwayG"] <- rpois(1,match_schedule[gamenumber, "AwayxG"])
  351.     if (match_schedule[gamenumber, "HomeG"] > match_schedule[gamenumber, "AwayG"]){
  352.       match_schedule[gamenumber, "HomePts"] <- 3
  353.       match_schedule[gamenumber, "AwayPts"] <- 0
  354.     } else if (match_schedule[gamenumber, "HomeG"] < match_schedule[gamenumber, "AwayG"]){
  355.       match_schedule[gamenumber, "AwayPts"] <- 3
  356.       match_schedule[gamenumber, "HomePts"] <- 0
  357.     } else {
  358.       match_schedule[gamenumber, "HomeG"] <- match_schedule[gamenumber, "HomeG"] + rpois(1,(match_schedule[gamenumber, "HomexG"])/2)
  359.       match_schedule[gamenumber, "AwayG"] <- match_schedule[gamenumber, "AwayG"] + rpois(1,(match_schedule[gamenumber, "AwayxG"])/2)
  360.       if (match_schedule[gamenumber, "HomeG"] > match_schedule[gamenumber, "AwayG"]){
  361.         match_schedule[gamenumber, "HomePts"] <- 3
  362.         match_schedule[gamenumber, "AwayPts"] <- 0
  363.       } else if (match_schedule[gamenumber, "HomeG"] < match_schedule[gamenumber, "AwayG"]){
  364.         match_schedule[gamenumber, "AwayPts"] <- 3
  365.         match_schedule[gamenumber, "HomePts"] <- 0
  366.       } else {
  367.         penalties <- runif(1)
  368.         if (penalties > 0.5){
  369.           match_schedule[gamenumber, "HomePts"] <- 3
  370.           match_schedule[gamenumber, "AwayPts"] <- 0
  371.         } else {
  372.           match_schedule[gamenumber, "AwayPts"] <- 3
  373.           match_schedule[gamenumber, "HomePts"] <- 0
  374.         }
  375.       }
  376.     }
  377.   }
  378.  
  379.   # CALCULATION OF TOTAL RESULTS
  380.   for (var in teams){
  381.     trigramme <- var
  382.     poscheck <- c(groupresults[[ 2]][[1]],groupresults[[ 6]][[1]],groupresults[[10]][[1]],groupresults[[14]][[1]],
  383.                   groupresults[[18]][[1]],groupresults[[22]][[1]],groupresults[[26]][[1]],groupresults[[30]][[1]])
  384.     if (trigramme %in% poscheck == TRUE){
  385.       finish_matrix[trigramme, "Group 1st"] <- finish_matrix[trigramme, "Group 1st"] + 1
  386.     }
  387.     poscheck <- c(groupresults[[ 2]][[2]],groupresults[[ 6]][[2]],groupresults[[10]][[2]],groupresults[[14]][[2]],
  388.                   groupresults[[18]][[2]],groupresults[[22]][[2]],groupresults[[26]][[2]],groupresults[[30]][[2]])
  389.     if (trigramme %in% poscheck == TRUE){
  390.       finish_matrix[trigramme, "Group 2nd"] <- finish_matrix[trigramme, "Group 2nd"] + 1
  391.     }
  392.     poscheck <- c(groupresults[[ 2]][[3]],groupresults[[ 6]][[3]],groupresults[[10]][[3]],groupresults[[14]][[3]],
  393.                   groupresults[[18]][[3]],groupresults[[22]][[3]],groupresults[[26]][[3]],groupresults[[30]][[3]])
  394.     if (trigramme %in% poscheck == TRUE){
  395.       finish_matrix[trigramme, "Group 3rd"] <- finish_matrix[trigramme, "Group 3rd"] + 1
  396.     }
  397.     poscheck <- c(groupresults[[ 2]][[4]],groupresults[[ 6]][[4]],groupresults[[10]][[4]],groupresults[[14]][[4]],
  398.                   groupresults[[18]][[4]],groupresults[[22]][[4]],groupresults[[26]][[4]],groupresults[[30]][[4]])
  399.     if (trigramme %in% poscheck == TRUE){
  400.       finish_matrix[trigramme, "Group 4th"] <- finish_matrix[trigramme, "Group 4th"] + 1
  401.     }
  402.     poscheck <- c(match_schedule[49, "Home"], match_schedule[49, "Away"], match_schedule[50, "Home"], match_schedule[50, "Away"],
  403.                   match_schedule[51, "Home"], match_schedule[51, "Away"], match_schedule[52, "Home"], match_schedule[52, "Away"],
  404.                   match_schedule[53, "Home"], match_schedule[53, "Away"], match_schedule[54, "Home"], match_schedule[54, "Away"],
  405.                   match_schedule[55, "Home"], match_schedule[55, "Away"], match_schedule[56, "Home"], match_schedule[56, "Away"])
  406.     if (trigramme %in% poscheck == TRUE){
  407.       finish_matrix[trigramme, "Round of 16"] <- finish_matrix[trigramme, "Round of 16"] + 1
  408.     }
  409.     poscheck <- c(match_schedule[57, "Home"], match_schedule[57, "Away"], match_schedule[58, "Home"], match_schedule[58, "Away"],
  410.                   match_schedule[59, "Home"], match_schedule[59, "Away"], match_schedule[60, "Home"], match_schedule[60, "Away"])
  411.     if (trigramme %in% poscheck == TRUE){
  412.       finish_matrix[trigramme, "Quarter Finals"] <- finish_matrix[trigramme, "Quarter Finals"] + 1
  413.     }
  414.     poscheck <- c(match_schedule[61, "Home"], match_schedule[61, "Away"], match_schedule[62, "Home"], match_schedule[62, "Away"])
  415.     if (trigramme %in% poscheck == TRUE){
  416.       finish_matrix[trigramme, "Semi Finals"] <- finish_matrix[trigramme, "Semi Finals"] + 1
  417.     }
  418.     poscheck <- c(match_schedule[64, "Home"], match_schedule[64, "Away"])
  419.     if (trigramme %in% poscheck == TRUE){
  420.       finish_matrix[trigramme, "Final"] <- finish_matrix[trigramme, "Final"] + 1
  421.     }
  422.     if (match_schedule[64, "Home"] == trigramme & match_schedule[64, "HomePts"] == 3 | match_schedule[64, "Away"] == trigramme & match_schedule[64, "AwayPts"] == 3){
  423.       finish_matrix[trigramme, "Champion"] <- finish_matrix[trigramme, "Champion"] + 1
  424.     }
  425.   }
  426. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement