Skip to content

Commit

Permalink
First Submission for Midterm
Browse files Browse the repository at this point in the history
  • Loading branch information
KPDuBose committed Mar 16, 2023
1 parent 0731b36 commit e3ebf02
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 18 deletions.
3 changes: 1 addition & 2 deletions R/plotElephant.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#' A general function to plot the results of a White Elephant simulation.
#'
#' @param sim An object of class `elphList` or `elphSum` to be summarized
#' @param ... Arguments to be passed to other methods. See `barplot` for further details.
#'
#' @return A plot showing the distribution of each winning seat and how often they won.
#'
Expand All @@ -12,7 +11,7 @@
#'


plotElephant <- function(sim, ...){
plotElephant <- function(sim){
if(!(is(sim, "elphList") | is(sim, "elphSum"))) stop("'sim' must be an 'elphList' or 'elphSum' class object")

if(is(sim, "elphList")){
Expand Down
10 changes: 5 additions & 5 deletions R/simulateElephant.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,33 @@ simulateElephant <- function(n,
if(dice == FALSE){
if(coins == 1){
cl <- parallel::makeCluster(2)
ans <- parallel::parSapply(cl, 1:5, function(i) replicate(iter/5, leftRightElephant(n, pheads), simplify = FALSE))
ans <- parallel::parSapply(cl, 1, function(i) replicate(iter, leftRightElephant(n, pheads), simplify = FALSE))
parallel::stopCluster(cl)
}

if(coins == 2){
cl <- parallel::makeCluster(2)
ans <- parallel::parSapply(cl, 1:5, function(i) replicate(iter/5, twoCoinElephant(n, pheads1 = pheads, pheads2 = pheads2), simplify = FALSE))
ans <- parallel::parSapply(cl, 1, function(i) replicate(iter, twoCoinElephant(n, pheads1 = pheads, pheads2 = pheads2), simplify = FALSE))
parallel::stopCluster(cl)
}
}

if(dice == TRUE){
if(coins == 0){
cl <- parallel::makeCluster(2)
ans <- parallel::parSapply(cl, 1:5, function(i) replicate(iter/5, dieElephant(n, sides = sides, numDice = numDice), simplify = FALSE))
ans <- parallel::parSapply(cl, 1, function(i) replicate(iter, dieElephant(n, sides = sides, numDice = numDice), simplify = FALSE))
parallel::stopCluster(cl)
}

if(coins == 1){
cl <- parallel::makeCluster(2)
ans <- parallel::parSapply(cl, 1:5, function(i) replicate(iter/5, lrDieElephant(n, pheads = pheads, sides = sides, numDice = numDice), simplify = FALSE))
ans <- parallel::parSapply(cl, 1, function(i) replicate(iter, lrDieElephant(n, pheads = pheads, sides = sides, numDice = numDice), simplify = FALSE))
parallel::stopCluster(cl)
}

if(coins == 2){
cl <- parallel::makeCluster(2)
ans <- parallel::parSapply(cl, 1:5, function(i) replicate(iter/5, twoCoinDieElephant(n, pheads1 = pheads, pheads2 = pheads2, sides = sides, numDice = numDice), simplify = FALSE))
ans <- parallel::parSapply(cl, 1, function(i) replicate(iter, twoCoinDieElephant(n, pheads1 = pheads, pheads2 = pheads2, sides = sides, numDice = numDice), simplify = FALSE))
parallel::stopCluster(cl)
}
}
Expand Down
10 changes: 7 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ The goal of partyGames is to simulate various party games and help you devise a
You can install the development version of partyGames like so:

``` r
# FILL THIS IN! HOW CAN PEOPLE INSTALL YOUR DEV PACKAGE?
# install.package("devtools")
devtools::install_github("KPDuBose/partyGames")
```

## Example

This is a basic example which shows you how to solve a common problem:
Simulate 1500 White Elephant Games under different conditions.

```{r example}
library(partyGames)
diceSum(6, 2) #Roll two six-sided dice
games <- simulateElephant(15, dice = TRUE, coins = 1, iter = 1500, pheads = 0.5, sides = 6, numDice = 1)
summaryElephant(games)
class(games)
# plotElephant(summaryElephant(games))
```

24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,31 @@ devise a better strategy to win.
You can install the development version of partyGames like so:

``` r
# FILL THIS IN! HOW CAN PEOPLE INSTALL YOUR DEV PACKAGE?
# install.package("devtools")
devtools::install_github("KPDuBose/partyGames")
```

## Example

This is a basic example which shows you how to solve a common problem:
Simulate 1500 White Elephant Games under different conditions.

``` r
library(partyGames)
diceSum(6, 2) #Roll two six-sided dice
#> [1] 8
games <- simulateElephant(15, dice = TRUE, coins = 1, iter = 1500, pheads = 0.5, sides = 6, numDice = 1)
summaryElephant(games)
#> $winner
#> winner
#> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#> 116 110 85 85 104 110 101 77 112 90 83 106 102 115 104
#>
#> $moves
#> moves
#> -6 -5 -4 -3 -2 -1 1 2 3 4 5 6
#> 1852 1874 1871 1901 1880 1857 1861 1879 1859 1835 1853 1978
#>
#> attr(,"class")
#> [1] "elphSum" "list"
class(games)
#> [1] "elphList" "list"
# plotElephant(summaryElephant(games))
```
4 changes: 1 addition & 3 deletions man/plotElephant.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

166 changes: 165 additions & 1 deletion vignettes/example-vignette.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: "The Basics of Party Games"
author: "Kline DuBose"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{example-vignette}
Expand All @@ -22,9 +23,172 @@ The goal of this package is to simulate various party game scenarios to help eva

# Dice Rolling

One of the functions simply rolls a dice.
## diceSum

```{r, eval=FALSE}
diceSum(sides, numDice)
```

This function rolls a given number of dice with a given number of sides and has two arguments, `sides` and `numDice`. `sides` designates how many sides the dice have and `numDice` designates how many dice are going to be rolled.

If we wished to roll a single six-sided die, we would input:
```{r}
diceSum(6, 1)
```
And our output is the number we rolled.

Now, let's roll three 100-sided dice just to see what it looks like:
```{r}
diceSum(100, 3)
```

# White Elephant Games

The package can also simulate various white elephant games with some differing rules since everyone plays this game differently or knows it by a different name.

## leftRightElephant

```{r eval=FALSE}
leftRightElephant(n, pheads)
```

This version of the game is played using a coin. A certain number of friends get together and each has a gift. Everyone takes turns flipping a coin and based on if the result is heads or tails, each participant passes the present clockwise (to the left) if heads or counter-clockwise (to the right) if tails.

`n` designates how many people are participating in the game. `pheads` designates the probability of heads.

The present starts in position 1, the person sitting to the right of position 1 is in position 2, to the right of position 3 is position 4, and so on.

### Example

Let's say 15 friends gather together to play. The coin is fair, or `pheads` is 0.5.

```{r}
leftRightElephant(15, 0.5)
```

Movement of the present to the right is designated by a 1 and movement to the left is designated by -1. The position of the winner is also noted.

## dieElephant

```{r eval=FALSE}
dieElephant(n, sides, numDice)
```

This version of the game is played with dice. Each participant rolls a dice and passes counter-clockwise(to the right) the number of the sum of the dice. Once again, the desired present starts at position 1.

`n` is the number of participants. `sides` is the number of sides on the dice. `numDice` is how many dice will be rolled.

### Example

Let's say 15 friends play with two six-sided dice.

```{r}
dieElephant(15, 6, 2)
```

Each roll is shown in moves. The winner is also displayed.

## lrDieElephant

```{r eval=FALSE}
lrDieElephant(n, pheads, sides, numDice)
```

This version of the game combines the previous two. Each participant rolls a die and flips a coin. As before, if the coin is heads, pass the present to the left. If the coin is tails, pass it to the right. Now however, you pass according to the number of dice you rolled.

### Example

15 friends gathered to play with a fair coin and a single six-sided die.

```{r}
lrDieElephant(15, 0.5, 6, 1)
```

Negative numbers in movement indicate a pass to the left and positive numbers indicate a pass to the right. Winner is also indicated.

## twoCoinElephant

```{r eval=FALSE}
twoCoinElephant(n, pheads1, pheads2)
```

This version of the game builds on the idea of using a coin. What if there were two coins with different probability of flipping heads, a fair coin and an unfair coin?

`n` is the number of participants in the game. `pheads1` and `pheads2` represent the probability of heads on each coin respectively.

We give each participant the chance to choose the coin that they use, at random, and flip it. As before, the desired present starts in position 1.

### Example

15 friends gather to play. The first coin is fair, the second coin is less likely to land on heads(`pheads2` = 0.3).

```{r}
twoCoinElephant(15, 0.5, 0.3)
```

1 indicates the present moved clockwise and -1 indicates the present moved to the left. Winner is also indicated.

## twoCoinDieElephant

```{r eval=FALSE}
twoCoinDieElephant(n, pheads1, pheads2, sides, numDice)
```

This combines the two coin version of the game with the added effect of rolling dice. Again, the present starts in position 1 and moves to the left or right based on a coin flip. Each participant randomly chooses a coin, flips it, and then rolls the dice to see how many spaces the present moves.

### Example

15 friends gather to play with two unfair coins, with probability of flipping heads 0.3 and 0.7 respectively. They roll two four-sided dice.

```{r}
twoCoinDieElephant(15, 0.3, 0.7, 4, 2)
```


# Simulation

The package also allows to simulate many iterations of the game at once.

## White Elephant

```{r eval=FALSE}
simulateElephant(n, dice, coins, iter, pheads, pheads2, sides, numDice)
```

This function runs many simulations of the chosen white elephant game. It returns an object of class `elphList`, and is basically a list of lists for each iteration of the game. It is recommended you save as a separate object for use later.

We will demonstrate this by simulating 1500 games played by a group of 20 people. There are two coins, the first is fair and the second has a probability of heads of 0.9. Three four-sided dice will be rolled.

```{r}
games <- simulateElephant(20, dice = TRUE, coins = 2, iter = 1500, pheads = 0.5,
pheads2 = 0.9, sides = 4, numDice = 3)
```

Now, we will summarize the information with:
```{r eval=FALSE}
summaryElephant(sim)
```

This requires an object of class `elphList` and returns an object of class `elphSum` which has two tables, one with the counts of the moves made and the other with counts of how many times each seat won.

```{r}
games.summary <- summaryElephant(games)
games.summary
```

Lastly, let's look at a graph of the distribution of each seat winning.
```{r eval=FALSE}
plotElephant(sim)
```

`sim` in this case, can be either a `elphList` or `elphSum` class object. We will look at examples using both types of objects to show that the output is the same.

```{r eval=FALSE}
# elphList object
plotElephant(games)
# elphSum object
plotElephant(games.summary)
```


0 comments on commit e3ebf02

Please sign in to comment.