The following guide provides a description with examples of the main
features of the package Rdice
. Such package provides basic
functions to experiment dice rollings and corresponding outputs
frequencies, coin flips, non-transitive Efron dice generators and
simulations of percolations in one dimension.
dice.roll
The function dice.roll
simulates rolling of a set of
dice given their number of faces and the probability of each face to
appear. Arguments to be passed are
The function returns an object of class , namely a list whose values
are themselves data.table
objects, in turn, so that one can
directly apply any data.table
function thereupon. The
values returned are:
set.seed(1902)
dice.roll(faces = 6, dice = 2, rolls = 5)$results
die_1 die_2
1: 6 1
2: 1 5
3: 6 5
4: 6 1
5: 6 5
set.seed(1902)
dice.roll(faces = 6, dice = 2, rolls = 5)$frequencies
die_1 die_2 N freq
1: 6 1 2 0.4
2: 1 5 1 0.2
3: 6 5 2 0.4
set.seed(1902)
dice.roll(faces = 6, dice = 2, rolls = 5)$sums_freq
sum N freq cum_sum
1: 6 1 0.2 0.2
2: 7 2 0.4 0.6
3: 11 2 0.4 1.0
dice.combinations
The function dice.combinations
calculates all possible
combinations as result of rolling a set of dice. Similar permutations
are identified under the same combination and counted as many times as
many occurrencies. Thee user can choose wheter to match exact values or
to perform partial matches. The function accepts as arguments:
c(1:faces)
,
namemly the function returns all combinations.As an example let us roll 3 six-faces dice many times and look for the frequencies of obtaining a one, a four and either a five or a six.
set.seed(1987)
dice.combinations(faces = 6, dice = 3, rolls = 1000,
getExact = c(1,4), getPartial = c(5,6))
value_1 value_2 value_3 freq
1: 1 4 5 0.018
2: 1 4 6 0.027
If we want to just obtain the overall probability of the above events then
set.seed(1987)
dice.combinations(faces = 6, dice = 3, rolls = 1000,
getExact = c(1,4), getPartial = c(5,6), toSum = TRUE)
[1] 0.052
The case faces = 2
corresponds to the
coin.flip
.
coin.flip
The function coin.flip
is a particular case of the
function dice.combinations
. Valid arguments are
c("H", "H", "T")
as head and tails (make sure to
provide the labels in quotation marks).Let us experiment with 5 coins and see what the probability of tossing five heads is, assumed the coin is fair.
set.seed(1902)
coin.flip(coins = 5, flips = 10000, getExact = replicate(5, c("H")))
value_1 value_2 value_3 value_4 value_5 freq
1: H H H H H 0.0311
Notice that if we had chosen a p-value of 0.05 this would allow us to reject the hypothesis that the coin is fair, although we know by default it is. A good indication that one experiment against the p-value threshold does not make much sense. By the way the value we obtained, 0.0311, is pretty close to the actual probability value of tossing five heads in a row P(5 heads) = (1/2)5 = 0.03125.
is.nonTransitive
The function is.nonTransitive
checks whether a given set
of dice is non-transitive. The dice must be inputted as a data frame
with one column for each die:
# generates a data frame of dice
df <- data.frame(
die1 = c(5,4,6,15),
die2 = c(3,6,16,2),
die3 = c(0,2,14,15),
die4 = c(8,10,1,9))
df
## die1 die2 die3 die4
## 1 5 3 0 8
## 2 4 6 2 10
## 3 6 16 14 1
## 4 15 2 15 9
## [1] TRUE
where we have specified the non-transitive probability to check
against. For general non-transitive dice with different mutual
probabilities we can leave the argument prob
unspecified
and let R
do the verification for us: for example
# generates a data frame of dice
df <- data.frame(
die1 = c(5,14,0,14),
die2 = c(7,10,4,13),
die3 = c(12,9,2,8),
die4 = c(1,7,16,8))
df
## die1 die2 die3 die4
## 1 5 7 12 1
## 2 14 10 9 7
## 3 0 4 2 16
## 4 14 13 8 8
## [1] TRUE
as different dice have different probabilities to mutually win against each other.
nonTransitive.generator
The function nonTransitive.generator
generates a random
set of non-transitive dice matching the conditions given as arguments.
It internally generates random sets of dice and applies the
is.nonTransitive
thereupon until a match is found. If no
matches occur, the function times out after a certain amount of time and
returns a NULL
value. Valid arguments are:
set.seed(1902)
nonTransitive.generator(faces = 3, dice = 3, max_value = 9, prob = 5/9)
die_1 die_2 die_3
1: 4 3 8
2: 6 2 5
3: 3 9 0
Had we not specified the probability, an asymettric non-transitive set of dice would have been generated.
The package contains the following data sets for testing:
data(efron)
die1 die2 die3 die4
1: 0 3 2 1
2: 0 3 2 1
3: 4 3 2 1
4: 4 3 2 5
5: 4 3 6 5
6: 4 3 6 5
data(oskar)
die1 die2 die3 die4 die5 die6
1: 2 7 5 1 6 4
2: 2 7 5 1 6 4
3: 14 10 13 12 8 11
4: 14 10 13 12 8 11
5: 17 16 15 20 19 18
6: 17 16 15 20 19 18
data(miwin)
die1 die2 die3
1: 1 1 2
2: 2 3 3
3: 5 4 4
4: 6 5 6
5: 7 8 7
6: 9 9 8