# Golf Scramble Simulation in R

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**You Know**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

# Golf Scramble Simulation

This is a simulation of a standard best-ball golf scramble. Conventional wisdom has it that the best golfer (A) should hit last, the idea being that one of the lesser golfers may have a decent shot already so the best golfer can take a risky shot. This simulation suggests that the worst golfer should indeed go first, but after that the order should be best on down (D, A, B, C). Perhaps a rationale is that golfer A will likely make a decent safe shot, which allows the other two medium skilled golfers a chance at a risky shot.

This is one of my first cracks at a sports simulation in R, so I welcome any comments about errors or constructive criticism.

Prepare

library(combinat)<br />

## Attaching package: 'combinat'<br />

## The following object is masked from 'package:utils':<br />## <br />## combn<br />

library(ggplot2)<br />set.seed(4444)<br />n <- 10000<br />Create golfer attributes

safe.attributes <- data.frame(golfer = c("a", "b", "c", "d"), mean = c(8, 7, <br /> 6, 5), sd = c(1.5, 1.5, 1.5, 1.5))<br />risk.attributes <- data.frame(golfer = c("a", "b", "c", "d"), mean = c(7, 6, <br /> 5, 4), sd = c(3, 3, 3, 3))<br /><br />safe.densities <- apply(safe.attributes[, -1], 1, function(x) sort(rnorm(n = 1000, <br /> mean = x[1], sd = x[2])))<br />colnames(safe.densities) <- safe.attributes$golfer<br />safe.df <- data.frame(safe.densities)<br /><br />risk.densities <- apply(risk.attributes[, -1], 1, function(x) sort(rnorm(n = 1000, <br /> mean = x[1], sd = x[2])))<br />colnames(risk.densities) <- risk.attributes$golfer<br />risk.df <- data.frame(risk.densities)<br />Plot golfer attributes

par(mfrow = c(2, 2))<br />par(mar = rep(2, 4))<br /><br />plot(density(safe.df$a), col = "blue", xlim = c(0, 16), ylim = c(0, 0.3), main = "Golfer A", <br /> col.main = "black", font.main = 4)<br />lines(density(risk.df$a), col = "red")<br />legend("topright", c("safe", "risk"), cex = 0.8, col = c("blue", "red"), lty = 1)<br /><br />plot(density(safe.df$b), col = "blue", xlim = c(0, 16), ylim = c(0, 0.3), main = "Golfer B", <br /> col.main = "black", font.main = 4)<br />lines(density(risk.df$b), col = "red")<br />legend("topright", c("safe", "risk"), cex = 0.8, col = c("blue", "red"), lty = 1)<br /><br />plot(density(safe.df$c), col = "blue", xlim = c(0, 16), ylim = c(0, 0.3), main = "Golfer C", <br /> col.main = "black", font.main = 4)<br />lines(density(risk.df$c), col = "red")<br />legend("topright", c("safe", "risk"), cex = 0.8, col = c("blue", "red"), lty = 1)<br /><br />plot(density(safe.df$d), col = "blue", xlim = c(0, 16), ylim = c(0, 0.3), main = "Golfer D", <br /> col.main = "black", font.main = 4)<br />lines(density(risk.df$d), col = "red")<br />legend("topright", c("safe", "risk"), cex = 0.8, col = c("blue", "red"), lty = 1)<br />

Create holes dataframe

golfPerms <- permn(letters[1:4])<br />holes <- data.frame(matrix(NA, nrow = n, length(golfPerms)))<br /><br />for (i in 1:length(golfPerms)) {<br /> colnames(holes)[i] <- paste0(golfPerms[[i]][1], golfPerms[[i]][2], golfPerms[[i]][3], <br /> golfPerms[[i]][4])<br />}<br />Process

for (j in 1:n) {<br /> for (i in 1:length(golfPerms)) {<br /> shot1 <- sample(safe.df[, substr(golfPerms[[i]][1], 1, 1)], 1, T)<br /><br /> if (shot1 >= 6) {<br /> shot2 <- max(shot1, sample(risk.df[, substr(golfPerms[[i]][2], 1, <br /> 1)], 1, T))<br /> } else {<br /> shot2 <- max(shot1, sample(safe.df[, substr(golfPerms[[i]][2], 1, <br /> 1)], 1, T))<br /> }<br /><br /> if (shot2 >= 6) {<br /> shot3 <- max(shot2, sample(risk.df[, substr(golfPerms[[i]][3], 1, <br /> 1)], 1, T))<br /> } else {<br /> shot3 <- max(shot2, sample(safe.df[, substr(golfPerms[[i]][3], 1, <br /> 1)], 1, T))<br /> }<br /><br /> if (shot3 >= 6) {<br /> shot4 <- max(shot3, sample(risk.df[, substr(golfPerms[[i]][4], 1, <br /> 1)], 1, T))<br /> } else {<br /> shot4 <- max(shot3, sample(safe.df[, substr(golfPerms[[i]][4], 1, <br /> 1)], 1, T))<br /> }<br /><br /> holes[j, i] <- shot4<br /> }<br />}<br />Find winning order per hole

winners <- data.frame(matrix(NA, nrow = n, ncol = 1))<br />names(winners) <- "winner"<br />for (k in 1:n) {<br /> winners[k, 1] <- colnames(holes)[(which.max(holes[k, ]))]<br />}<br />winnerCounts <- data.frame(table(winners))<br />winnerCounts$winners <- reorder(winnerCounts$winners, -winnerCounts$Freq)<br />Plot results

par(mfrow = c(1, 1))<br />ggplot(data = winnerCounts, aes(x = winners, y = Freq)) + geom_bar(colour = "black", <br /> fill = "#DD8888", width = 0.7, stat = "identity") + guides(fill = FALSE) + <br /> xlab("Order") + ylab("Wins") + ggtitle("Golf Scramble Simulation")<br />

ddunn801 at gmail dot com

To

**leave a comment**for the author, please follow the link and comment on their blog:**You Know**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.