Skip to content

Instantly share code, notes, and snippets.

@benjamin-chan
Last active February 6, 2018 22:58
Show Gist options
  • Save benjamin-chan/12809afd73339da5abf0f6908fcea09f to your computer and use it in GitHub Desktop.
Save benjamin-chan/12809afd73339da5abf0f6908fcea09f to your computer and use it in GitHub Desktop.
Simulate frequency of lottery quick picks
library(checkpoint)
checkpoint("2018-01-01")
library(magrittr)
library(tidyverse)
library(parallel)
library(doParallel)
s <- 1e6 # Number of lottery tickets
cores <- detectCores()
cl <- makeCluster(cores)
registerDoParallel(cl)
quickPicks <- foreach (i = 1:s, .combine = rbind) %dopar% {
sample(1:49, 6)
}
stopCluster(cl)
M <-
0 %>%
rep(s * 49) %>%
as.integer() %>%
matrix(nrow = s, ncol = 49)
for (i in 1:s) {
M[i, quickPicks[i, ]] <- 1
}
df <-
M %>%
data.frame() %>%
gather() %>%
mutate(number = as.integer(gsub("X", "", key))) %>%
rename(picked = value) %>%
select(-key) %>%
group_by(number) %>%
summarize(numberTickets = n(),
countPicked = sum(picked),
propPicked = sum(picked) / n(),
seProp = sqrt((sum(picked) / n()) * (1 - (sum(picked) / n()))/ n())) %>%
mutate(lower = propPicked - seProp,
upper = propPicked + seProp)
df %>%
ggplot() +
aes(x = number, y = propPicked) +
# geom_linerange(aes(ymin = lower, ymax = upper), color = "grey") +
geom_line() +
geom_point() +
geom_abline(intercept = mean(df$propPicked), slope = 0, color = "blue", size = 2, alpha = 1/2) +
scale_x_continuous("Number") +
scale_y_continuous(sprintf("Proportion of %s tickets",
format(s, big.mark = ",", scientific = FALSE))) +
theme_bw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment