Title: | Simulates Dice Rolls and Coin Flips |
---|---|
Description: | Utils for basic statistical experiments, that can be used for teaching introductory statistics. Each experiment generates a tibble. Dice rolls and coin flips are simulated using sample(). The properties of the dice can be changed, like the number of sides. A coin flip is simulated using a two sided dice. Experiments can be combined with the pipe-operator. |
Authors: | Roland Krasser |
Maintainer: | Roland Krasser <[email protected]> |
License: | GPL-3 |
Version: | 1.0.0 |
Built: | 2025-02-21 04:01:02 UTC |
Source: | https://github.com/rolkra/tidydice |
Generates a tibble containing the binomial distribution using dbinom().
binom(times, prob_success)
binom(times, prob_success)
times |
number of trials |
prob_success |
probability of success (number between 0 and 1) |
Binomial distribution as a tibble
binom(times = 10, prob_success = 1/10)
binom(times = 10, prob_success = 1/10)
Generates a tibble containing the binomial distribution of flipping a coin using dbinom().
binom_coin(times, sides = 2, success = 2)
binom_coin(times, sides = 2, success = 2)
times |
how many times a coin is flipped (or how many coins are flipped at the same time) |
sides |
number of sides of the coin (default = 2) |
success |
which result is a success (default = 2) |
binomial distribution as a tibble
binom_coin(times = 10)
binom_coin(times = 10)
Generates a tibble containing the binomial distribution of rolling the dice using dbinom().
binom_dice(times, sides = 6, success = 6)
binom_dice(times, sides = 6, success = 6)
times |
How many times a dice is rolled (or how many dice are rolled at the same time) |
sides |
Number of sides of the dice (default = 6) |
success |
Which result is a success (default = 6) |
Binomial distribution as a tibble
binom_dice(times = 10)
binom_dice(times = 10)
Helper function to draw a circle
circle_points(center = c(0, 0), diameter = 1, npoints = 61)
circle_points(center = c(0, 0), diameter = 1, npoints = 61)
center |
Vector with x and y coordinate of center |
diameter |
Diameter of circle |
npoints |
Number of points used for drawing a circle |
Dataframe with x and y coordinates to draw a circle
Flipping a coin is simulated using sample(). The default coin has 2 sides and is fair. The properties of the coin can be changed. The result is returned as a tibble.
flip_coin( data = NULL, times = 1, rounds = 1, success = c(2), agg = FALSE, sides = 2, prob = NULL, seed = NULL )
flip_coin( data = NULL, times = 1, rounds = 1, success = c(2), agg = FALSE, sides = 2, prob = NULL, seed = NULL )
data |
Data from a previous experiment |
times |
How many times coin is flipped (or how many coins are flipped at the same time) |
rounds |
Number of rounds |
success |
Which result is a success (default = 2) |
agg |
If TRUE, the result is aggregated (by experiment, rounds) |
sides |
Number of sides of the coin (default = 2) |
prob |
Vector of probabilities for each side of the coin |
seed |
Seed to produce reproducible results |
Result of experiment as a tibble
# flipping a coin flip_coin() # flipping a coin 10 times flip_coin(times = 10) # aggregate result flip_coin(times = 10, agg = TRUE) # rounds flip_coin(times = 10, rounds = 3, agg = TRUE) # experiments library(dplyr) flip_coin(times = 10, rounds = 3, agg = TRUE) %>% flip_coin(times = 12, rounds = 3, agg = TRUE)
# flipping a coin flip_coin() # flipping a coin 10 times flip_coin(times = 10) # aggregate result flip_coin(times = 10, agg = TRUE) # rounds flip_coin(times = 10, rounds = 3, agg = TRUE) # experiments library(dplyr) flip_coin(times = 10, rounds = 3, agg = TRUE) %>% flip_coin(times = 12, rounds = 3, agg = TRUE)
The forced result is returned as a tibble.
force_coin(data = NULL, result = 6, round = 1, experiment = 1, success = 2)
force_coin(data = NULL, result = 6, round = 1, experiment = 1, success = 2)
data |
Data from a previous experiment |
result |
Vector of flipping coin results |
round |
Round of flipping coin |
experiment |
Experiment Number |
success |
Which result is a success (default = 6) |
Result of experiment as a tibble
force_coin(6) force_coin(1:6)
force_coin(6) force_coin(1:6)
The forced result is returned as a tibble.
force_dice(data = NULL, result = 6, round = 1, experiment = 1, success = 6)
force_dice(data = NULL, result = 6, round = 1, experiment = 1, success = 6)
data |
Data from a previous experiment |
result |
Vector of rolling dice results |
round |
Round of rolling dice |
experiment |
Experiment Number |
success |
Which result is a success (default = 6) |
Result of experiment as a tibble
force_dice(6) force_dice(1:6)
force_dice(6) force_dice(1:6)
This is the main function to parse a string containing complex formula specifications for rolling dice.
parse_dice_formula(dice_formula)
parse_dice_formula(dice_formula)
dice_formula |
A string containing a dice formula, e.g. 1d6e2+1d4 |
The input can be a string containing specifications for multiple dice, e.g.:
1d6e6 -> roll 1 six-sided dice, explode on 6
1d6e6+2d4-1d10 -> Roll 1 six-sided dice, explode on 6, plus two 4-sided dice, subract one 10-sided dice
This is inspired by Avrae's bot syntax for rolling dice. See https://github.com/avrae/d20
Helper function to parse a dice formula
parse_dice_formula_part(dice_formula_part)
parse_dice_formula_part(dice_formula_part)
dice_formula_part |
A split dice formula, e.g. 1d6e2. For more complex formula, e.g. 1d6e2+3d4, see parse_dice_formula |
Plot a binomial distribution generated with binom_dice() or binom_coin()
plot_binom( data, title = "Binomial distribution", color = "darkgrey", color_highlight = "coral", label = NULL, label_size = 3, min_pct = 0.05, highlight = NULL )
plot_binom( data, title = "Binomial distribution", color = "darkgrey", color_highlight = "coral", label = NULL, label_size = 3, min_pct = 0.05, highlight = NULL )
data |
data containing values for binomial distribution |
title |
title of the plot |
color |
color of bars |
color_highlight |
color of highlighted bars |
label |
add labels to plot? |
label_size |
size of label |
min_pct |
surpress values < min_pct |
highlight |
vector of values to be highlighted |
ggplot object
plot_binom(data = binom_dice(times = 10))
plot_binom(data = binom_dice(times = 10))
Plot result of flip_coin()
plot_coin( data, detailed = FALSE, fill = "white", fill_success = "gold", line_color = "black", line_size = 0.8 )
plot_coin( data, detailed = FALSE, fill = "white", fill_success = "gold", line_color = "black", line_size = 0.8 )
data |
result of flip_coin() |
detailed |
not supported at moment |
fill |
Fill color |
fill_success |
Fill color if result is a success |
line_color |
Color of Lines |
line_size |
Size of Lines |
ggplot-Object
library(magrittr) # plot one coin plot_coin() # plot multiple coin flips flip_coin(times = 3, rounds = 3) %>% plot_coin() # change coin design flip_coin(times = 3, rounds = 3) %>% plot_coin(fill_success = "red")
library(magrittr) # plot one coin plot_coin() # plot multiple coin flips flip_coin(times = 3, rounds = 3) %>% plot_coin() # change coin design flip_coin(times = 3, rounds = 3) %>% plot_coin(fill_success = "red")
Plot result of roll_dice()
plot_dice( data, detailed = FALSE, fill = "white", fill_success = "gold", point_color = "black", line_color = "black", line_size = 0.8 )
plot_dice( data, detailed = FALSE, fill = "white", fill_success = "gold", point_color = "black", line_color = "black", line_size = 0.8 )
data |
result of roll_dice() |
detailed |
If TRUE, the dice is plotted with more details |
fill |
Fill color |
fill_success |
Fill color if result is a success |
point_color |
Color of Points |
line_color |
Color of Lines |
line_size |
Size of Lines |
ggplot-Object
library(magrittr) plot_dice() roll_dice(times = 3, rounds = 3) %>% plot_dice() roll_dice(times = 3, rounds = 3) %>% plot_dice(fill_success = "red")
library(magrittr) plot_dice() roll_dice(times = 3, rounds = 3) %>% plot_dice() roll_dice(times = 3, rounds = 3) %>% plot_dice(fill_success = "red")
Draw a single coin
plot_single_coin( ggplot = NULL, result = 1, x = 0, y = 0, width = 0.9, fill = "white", detailed = FALSE, line_size = 0.8, line_color = "black" )
plot_single_coin( ggplot = NULL, result = 1, x = 0, y = 0, width = 0.9, fill = "white", detailed = FALSE, line_size = 0.8, line_color = "black" )
ggplot |
ggplot-Object. If passed, the dice will be added to plot |
result |
Result of flip coin (0/1) |
x |
X-coordinate of dice (center) |
y |
y-coordinate of dice (center) |
width |
Width of coin |
fill |
Fill color |
detailed |
If TRUE, the dice is plotted with more details |
line_size |
Size of Lines |
line_color |
Color of Lines |
ggplot-Object
Draw a single dice
plot_single_dice( ggplot = NULL, result = 6, x = 0, y = 0, width = 0.9, fill = "white", detailed = FALSE, rounding = width/5, line_size = 0.8, line_color = "black", point_size = width/6, point_color = "black" )
plot_single_dice( ggplot = NULL, result = 6, x = 0, y = 0, width = 0.9, fill = "white", detailed = FALSE, rounding = width/5, line_size = 0.8, line_color = "black", point_size = width/6, point_color = "black" )
ggplot |
ggplot-Object. If passed, the dice will be added to plot |
result |
Result of dice rolling (0..6) |
x |
X-coordinate of dice (center) |
y |
y-coordinate of dice (center) |
width |
Width of dice |
fill |
Fill color |
detailed |
If TRUE, the dice is plotted with more details |
rounding |
Rounding of dice (only used if detailed == TRUE) |
line_size |
Size of Lines |
line_color |
Color of Lines |
point_size |
Size of Points |
point_color |
Color of Points |
ggplot-Object
Rolling a dice is simulated using sample(). The default dice has 6 sides and is fair. The properties of the dice can be changed. The result is returned as a tibble.
roll_dice( data = NULL, times = 1, rounds = 1, success = c(6), agg = FALSE, sides = 6, prob = NULL, seed = NULL )
roll_dice( data = NULL, times = 1, rounds = 1, success = c(6), agg = FALSE, sides = 6, prob = NULL, seed = NULL )
data |
Data from a previous experiment |
times |
How many times a dice is rolled (or how many dice are rolled at the same time) |
rounds |
Number of rounds |
success |
Which result is a success (default = 6) |
agg |
If TRUE, the result is aggregated (by experiment, rounds) |
sides |
Number of sides of the dice (default = 6) |
prob |
Vector of probabilities for each side of the dice |
seed |
Seed to produce reproducible results |
Result of experiment as a tibble
# rolling a dice once roll_dice() # rolling a dice 10 times roll_dice(times = 10) # aggregate result roll_dice(times = 10, agg = TRUE) # rounds roll_dice(times = 10, rounds = 3, agg = TRUE) # experiments library(dplyr) roll_dice(times = 10, rounds = 3, agg = TRUE) %>% roll_dice(times = 12, rounds = 3, agg = TRUE)
# rolling a dice once roll_dice() # rolling a dice 10 times roll_dice(times = 10) # aggregate result roll_dice(times = 10, agg = TRUE) # rounds roll_dice(times = 10, rounds = 3, agg = TRUE) # experiments library(dplyr) roll_dice(times = 10, rounds = 3, agg = TRUE) %>% roll_dice(times = 12, rounds = 3, agg = TRUE)
Simulating rolling a dice, using a formula
roll_dice_formula( data = NULL, dice_formula = "1d6", times = 1, rounds = 1, seed = NULL, prob = NULL, success = c(6), agg = FALSE, label = NULL )
roll_dice_formula( data = NULL, dice_formula = "1d6", times = 1, rounds = 1, seed = NULL, prob = NULL, success = c(6), agg = FALSE, label = NULL )
data |
Data from a previous experiment |
dice_formula |
Dice formula (e.g. "1d6" = 1 dice with 6 sides) |
times |
How many times a dice is rolled (or how many dice are rolled at the same time) |
rounds |
Number of rounds |
seed |
Seed to produce reproducible results |
prob |
Vector of probabilities for each side of the dice |
success |
Which result is a success (default = 6) |
agg |
If TRUE, the result is aggregated (by experiment, rounds) (not implemented) |
label |
Custom text to distinguish an experiment, can be used for plotting etc. |
Result of experiment as a tibble
# roll one 6-sided dice roll_dice_formula(dice_formula = "1d6") # roll one 8-sided dice roll_dice_formula(dice_formula = "1d8") # roll two 6-sided dice roll_dice_formula(dice_formula = "2d6") # roll two 6-sided dice, explode dice on a 6 roll_dice_formula(dice_formula = "2d6e6") # roll three 6-sided dice, keep highest 2 rolls roll_dice_formula(dice_formula = "3d6kh2") # roll three 6-sided dice, keep lowest 2 rolls roll_dice_formula(dice_formula = "3d6kl2") # roll four 6-sided dice, keep highest 3 rolls, but explode on a 6 roll_dice_formula(dice_formula = "4d6kh3e6") # roll one 20-sided dice, and add 4 roll_dice_formula(dice_formula = "1d20+4") # roll one 4-sided dice and one 6-sided dice, and sum the results roll_dice_formula(dice_formula = "1d4+1d6")
# roll one 6-sided dice roll_dice_formula(dice_formula = "1d6") # roll one 8-sided dice roll_dice_formula(dice_formula = "1d8") # roll two 6-sided dice roll_dice_formula(dice_formula = "2d6") # roll two 6-sided dice, explode dice on a 6 roll_dice_formula(dice_formula = "2d6e6") # roll three 6-sided dice, keep highest 2 rolls roll_dice_formula(dice_formula = "3d6kh2") # roll three 6-sided dice, keep lowest 2 rolls roll_dice_formula(dice_formula = "3d6kl2") # roll four 6-sided dice, keep highest 3 rolls, but explode on a 6 roll_dice_formula(dice_formula = "4d6kh3e6") # roll one 20-sided dice, and add 4 roll_dice_formula(dice_formula = "1d20+4") # roll one 4-sided dice and one 6-sided dice, and sum the results roll_dice_formula(dice_formula = "1d4+1d6")
Helper function to get sum of top n dice
top_n_dice(x, n, dec = F)
top_n_dice(x, n, dec = F)
x |
Vector of dice-values |
n |
Number of dice |
dec |
Decreasing |