Skip to content

Instantly share code, notes, and snippets.

View orrymr's full-sized avatar

Orry orrymr

View GitHub Profile
n_ <- 2000
m_ <- 10000
g <- ggplot(roll(m = m_, n = n_),
mapping = aes(x = V1)) +
geom_vline(xintercept = 3.5, colour = "tomato3") +
labs(
subtitle = str_interp('Density of means of dice
rolls for ${n_} dice over ${m_} rolls.'),
x = 'Mean Score',
n_ <- 20
m_ <- 10000
g <- ggplot(roll(m = m_, n = n_),
mapping = aes(x = V1)) +
geom_vline(xintercept = 3.5, colour = "tomato3") +
labs(
subtitle = str_interp('Density of means of dice
rolls for ${n_} dice over ${m_} rolls.'),
x = 'Mean Score',
n_ <- 4
m_ <- 10000
g <- ggplot(roll(m = m_, n = n_),
mapping = aes(x = V1)) +
geom_vline(xintercept = 3.5, colour = "tomato3") +
labs(
subtitle = str_interp('Density of means of dice
rolls for ${n_} dice over ${m_} rolls.'),
x = 'Mean Score',
@orrymr
orrymr / dens.R
Created September 13, 2021 18:05
n_ <- 2
m_ <- 10000
g <- ggplot(roll(m = m_, n = n_),
mapping = aes(x = V1)) +
geom_vline(xintercept = 3.5, colour = "tomato3") +
labs(
subtitle = str_interp('Density of means of dice
rolls for ${n_} dice over ${m_} rolls.'),
x = 'Mean Score',
g <- g +
geom_density()
g
n_ <- 1
m_ <- 10000
g <- ggplot(roll(m = m_, n = n_),
mapping = aes(x = V1)) +
geom_vline(xintercept = 3.5, colour = "tomato3") +
labs(
subtitle = str_interp('Density of means of dice
rolls for ${n_} dice over ${m_} rolls.'),
x = 'Mean Score',
@orrymr
orrymr / roll.R
Created September 13, 2021 17:58
#roll, as in roll di(c)e
# m = number of times
# n = number of dice
roll <- function(m, n){
set.seed(1234)
means <- plyr::ldply(1:m, function(x){
return(mean(sample(1:6, n, replace = TRUE)))
})
}
@orrymr
orrymr / mean.R
Created September 13, 2021 17:54
number_of_rolls <- 1000000 # Or, we could manually roll a die 1 million times. Nah, let's let the computer do it.
mean(sample(1:6, number_of_rolls, replace = TRUE))
# [1] 3.499435
@orrymr
orrymr / dice_matrix.R
Last active September 13, 2021 18:17
row1 <- c(2, 3, 4, 5, 6, 7) / 2
row2 <- c(3, 4, 5, 6, 7, 8) / 2
row3 <- c(4, 5, 6, 7, 8, 9) / 2
row4 <- c(5, 6, 7, 8, 9, 10) / 2
row5 <- c(6, 7, 8, 9, 10, 11) / 2
row6 <- c(7, 8, 9, 10, 11, 12) / 2
dice <- matrix(c(row1, row2, row3, row4, row5, row6), nrow = 6, byrow = TRUE)
dice
@orrymr
orrymr / mean_value.R
Last active September 13, 2021 17:44
mean_value <- sum(seq(1:6) * (1 / 6))
print(mean_value)
# [1] 3.5