Re: [R] Model To Simulate Dice Roll

2022-04-22 Thread David Carlson via R-help
Sorry, The last three lines should read:

all <- apply(results, 1, function(x) length(intersect(x,
seq(sides)))==sides)
sum(all)/reps
results <- as.data.frame(results)

To generalize them for values of sides other than 6.

On Fri, Apr 22, 2022 at 11:05 PM Paul Bernal  wrote:

> Thank you so much David! El El vie, 22 de abr. de 2022 a la(s) 11:04 p.
> m., David Carlson  escribió: Since the rolls are
> independent, it is not necessary to separate the rolls into two stages:
> sides <- 6 ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍
> ZjQcmQRYFpfptBannerStart
> This Message Is From an External Sender
> This message came from outside your organization.
>
> ZjQcmQRYFpfptBannerEnd
> Thank you so much David!
>
> El El vie, 22 de abr. de 2022 a la(s) 11:04 p. m., David Carlson <
> dcarl...@tamu.edu> escribió:
>
>> Since the rolls are independent, it is not necessary to separate the
>> rolls into two stages:
>>
>> sides <- 6
>> months <- 12
>> reps <- 100
>>
>> set.seed(2022)
>> results <- matrix(sample.int
>> (sides,
>> months*reps, replace=TRUE), reps, months, byrow=TRUE)
>> colnames(results) <- month.name
>> 
>> all6 <- apply(results, 1, function(x) length(intersect(x, 1:6))==6)
>> sum(all6)/reps
>> # 0.53 which matches Rui's result
>> results <- as.data.frame(results)
>>
>> David L. Carlson
>>
>>
>>
>> On Thu, Apr 21, 2022 at 4:04 AM Rui Barradas 
>> wrote:
>>
>>> Hello, There's an error in my code, inline. Às 07:55 de 21/04/2022, Rui
>>> Barradas escreveu: > Hello, > > For what I understand of the question, the
>>> followng might answer it. > > The functions below roll dice and simulate R
>>> replicates
>>> ZjQcmQRYFpfptBannerStart
>>> This Message Is From an External Sender
>>> This message came from outside your organization.
>>>
>>> ZjQcmQRYFpfptBannerEnd
>>>
>>> Hello,
>>>
>>> There's an error in my code, inline.
>>>
>>> Às 07:55 de 21/04/2022, Rui Barradas escreveu:
>>> > Hello,
>>> >
>>> > For what I understand of the question, the followng might answer it.
>>> >
>>> > The functions below roll dice and simulate R replicates of dice rolls.
>>> > Then 12 (one per month) 6 sided dice rolls are simulated 100 times.
>>> >
>>> > The colMeans/apply computes the empiric probabilities of having all 6
>>> > sides occur in each row, Jan to Dec and a overall probabilty is the mean
>>> > of those probabilities.
>>> >
>>> > The matrix is coerced to data.frame only at the end.
>>> >
>>> >
>>> >
>>> > dice <- function(rolls = 1, ndice = 1, sides = 6) {
>>> >roll <- function(ndice = 1, sides = 6) {
>>> >  sample(seq_len(sides), ndice, replace = TRUE)
>>> >}
>>> >y <- replicate(rolls, roll(ndice = ndice, sides = sides))
>>> >if(is.null(dim(y))) y else colSums(y)
>>> > }
>>> > dice_simul <- function(rolls = 1, ndice = 1, sides = 6, R) {
>>> >if(missing(R)) {
>>> >  stop("number of simulations 'R' is missing with no default.")
>>> >}
>>> >replicate(R, dice(rolls = rolls, ndice = ndice, sides = sides))
>>> > }
>>> >
>>> > dice_rolls <- 100
>>> > #dice_rolls <- 1e6
>>> > num_dice <- 1
>>> > dice_sides <- 6
>>> > months <- 12
>>> >
>>> > set.seed(2022)
>>> > prob_frame <- t(dice_simul(months, num_dice, dice_sides, R = dice_rolls))
>>> > colnames(prob_frame) <- month.name 
>>> > 
>>> > head(prob_frame)
>>> >
>>>
>>> # --- wrong
>>> > p <- colMeans(apply(prob_frame, 1, \(x) 1:6 %in% x))
>>> > mean(p)
>>> > # [1] 0.9116667
>>>
>>> This should be
>>>
>>> yes_no <- apply(prob_frame, 1, \(x) all(1:6 %in% x))
>>> p <- mean(yes_no)
>>> p
>>> # [1] 0.53
>>>
>>>
>>> Rui Barradas
>>>
>>>
>>>
>>> >
>>> > prob_frame <- as.data.frame(prob_frame)
>>> >
>>> >
>>> > Hope this helps,
>>> >
>>> > Rui Barradas
>>> >
>>> >
>>> > Às 05:02 de 21/04/2022, Paul Bernal escreveu:
>>> >> Dear friend Bert,
>>> >>
>>> >> Thank you so much for your kind reply. The first thing I need to do is to
>>> >> simulate dice rolls, say 120 times.
>>> >>
>>> >> I want to populate an m by 12 dataframe with the results of each dice
>>> >> roll.
>>> >> For example, the result from dice roll #1 would need to go on row 1,
>>> >> column1, the result from dice roll #2 would have to go in row 1 column 2,
>>> >> and so on.
>>> >>
>>> >> The reason why I want to store those results in a dataframe is to be able
>>> >> to perform some other calculations afterwards.
>>> >>
>>> >> This is for a project I am doing.
>>> >>
>>> >> So this is the situation:
>>> >> You and five friends – a total of six people – plan to meet once per
>>> >> month
>>> >> to have dinner together, with one of you choosing the restaurant each
>>> >> month. Rather than

Re: [R] Model To Simulate Dice Roll

2022-04-22 Thread Paul Bernal
Thank you so much David!

El El vie, 22 de abr. de 2022 a la(s) 11:04 p. m., David Carlson <
dcarl...@tamu.edu> escribió:

> Since the rolls are independent, it is not necessary to separate the rolls
> into two stages:
>
> sides <- 6
> months <- 12
> reps <- 100
>
> set.seed(2022)
> results <- matrix(sample.int(sides, months*reps, replace=TRUE), reps,
> months, byrow=TRUE)
> colnames(results) <- month.name
> all6 <- apply(results, 1, function(x) length(intersect(x, 1:6))==6)
> sum(all6)/reps
> # 0.53 which matches Rui's result
> results <- as.data.frame(results)
>
> David L. Carlson
>
>
>
> On Thu, Apr 21, 2022 at 4:04 AM Rui Barradas  wrote:
>
>> Hello, There's an error in my code, inline. Às 07:55 de 21/04/2022, Rui
>> Barradas escreveu: > Hello, > > For what I understand of the question, the
>> followng might answer it. > > The functions below roll dice and simulate R
>> replicates
>> ZjQcmQRYFpfptBannerStart
>> This Message Is From an External Sender
>> This message came from outside your organization.
>>
>> ZjQcmQRYFpfptBannerEnd
>>
>> Hello,
>>
>> There's an error in my code, inline.
>>
>> Às 07:55 de 21/04/2022, Rui Barradas escreveu:
>> > Hello,
>> >
>> > For what I understand of the question, the followng might answer it.
>> >
>> > The functions below roll dice and simulate R replicates of dice rolls.
>> > Then 12 (one per month) 6 sided dice rolls are simulated 100 times.
>> >
>> > The colMeans/apply computes the empiric probabilities of having all 6
>> > sides occur in each row, Jan to Dec and a overall probabilty is the mean
>> > of those probabilities.
>> >
>> > The matrix is coerced to data.frame only at the end.
>> >
>> >
>> >
>> > dice <- function(rolls = 1, ndice = 1, sides = 6) {
>> >roll <- function(ndice = 1, sides = 6) {
>> >  sample(seq_len(sides), ndice, replace = TRUE)
>> >}
>> >y <- replicate(rolls, roll(ndice = ndice, sides = sides))
>> >if(is.null(dim(y))) y else colSums(y)
>> > }
>> > dice_simul <- function(rolls = 1, ndice = 1, sides = 6, R) {
>> >if(missing(R)) {
>> >  stop("number of simulations 'R' is missing with no default.")
>> >}
>> >replicate(R, dice(rolls = rolls, ndice = ndice, sides = sides))
>> > }
>> >
>> > dice_rolls <- 100
>> > #dice_rolls <- 1e6
>> > num_dice <- 1
>> > dice_sides <- 6
>> > months <- 12
>> >
>> > set.seed(2022)
>> > prob_frame <- t(dice_simul(months, num_dice, dice_sides, R = dice_rolls))
>> > colnames(prob_frame) <- month.name
>> > head(prob_frame)
>> >
>>
>> # --- wrong
>> > p <- colMeans(apply(prob_frame, 1, \(x) 1:6 %in% x))
>> > mean(p)
>> > # [1] 0.9116667
>>
>> This should be
>>
>> yes_no <- apply(prob_frame, 1, \(x) all(1:6 %in% x))
>> p <- mean(yes_no)
>> p
>> # [1] 0.53
>>
>>
>> Rui Barradas
>>
>>
>>
>> >
>> > prob_frame <- as.data.frame(prob_frame)
>> >
>> >
>> > Hope this helps,
>> >
>> > Rui Barradas
>> >
>> >
>> > Às 05:02 de 21/04/2022, Paul Bernal escreveu:
>> >> Dear friend Bert,
>> >>
>> >> Thank you so much for your kind reply. The first thing I need to do is to
>> >> simulate dice rolls, say 120 times.
>> >>
>> >> I want to populate an m by 12 dataframe with the results of each dice
>> >> roll.
>> >> For example, the result from dice roll #1 would need to go on row 1,
>> >> column1, the result from dice roll #2 would have to go in row 1 column 2,
>> >> and so on.
>> >>
>> >> The reason why I want to store those results in a dataframe is to be able
>> >> to perform some other calculations afterwards.
>> >>
>> >> This is for a project I am doing.
>> >>
>> >> So this is the situation:
>> >> You and five friends – a total of six people – plan to meet once per
>> >> month
>> >> to have dinner together, with one of you choosing the restaurant each
>> >> month. Rather than scheduling the entire year in advance, you decide to
>> >> make it interesting: each month a single six-sided die will be rolled to
>> >> determine which of you gets to choose the restaurant that month. How
>> >> likely
>> >> is it that everyone will have a chance to eat at their own favorite
>> >> restaurant? That is, what is the probability p that over the next 12
>> >> months, each of you will have had at least one opportunity to choose
>> >> where
>> >> to eat?
>> >>
>> >> This is what I am asked to do:
>> >> Write a program to estimate the desired probability p via simulation. The
>> >> program should input a sequence of positive integer number of trials to
>> >> simulate using the language's pseudorandom number generator and calculate
>> >> the corresponding fractions of simulated trials that are “successful"
>> >> (i.e., all 6 parties get at least one opportunity to choose where to eat.
>> >> Alice, Bob, Charley, Fred, Ellen, Don, Don, Don, Don, Alice, Charley, Bob
>> >> is a successful trial. Alice, Bob, Charley, Ellen, Don, Don, Don, Don,
>> >> Ellen, Alice, Charley, Bob is not a successful trial since Fred does not
>> >> get to choose.)
>> >> Turn in a set of 10 trials showing each roll of the dice to show
>> >> correct