# montyHall: Simulating the "Monty Hall" Game In johnmchambers/XRexamples: Extending R: Examples

## Description

In the game, a prize is located behind one of 3 doors (or in one of 3 boxes). The player makes a guess, after which the game owner opens one door/box that is neither the winner nor the guess. The player can then make a second guess. This function runs the game `N` times.

## Usage

 `1` ```montyHall(N) ```

## Arguments

 `N` sample size; i.e., number of times to run the simulation of the game.

## Details

The assumption is that both the prize location and the first guess are sampled uniformly. Also, if the owner has a choice, the open is also sampled.

The point is to show that the second guess has twice the winning chance of the first. Sometimes framed as Bayesian inference (Bain, 2016) but in fact obvious if the event space is partitioned relevantly (as the example illustrates).

## Value

A data frame with columns for the first guess, the opened door/box, the second guess and the location of the prize.

John M. Chambers

## References

Chambers, John M. (2016) Extending R, Chapman & Hall/CRC.

Bain, Robert. (2016) “Are our brains Bayesian?” Significance, vol. 13, issue 4, pp 14-19.

## Examples

 ```1 2 3 4 5 6 7``` ```set.seed(378) N <- 10000 mh <- montyHall(N) x <- mh[mh\$guess1 != mh\$prize,] # guess1 was wrong all(x\$guess2 == x\$prize) # => guess2 was right sum(mh\$guess1 == mh\$prize)/N ## probability 1/3 sum(mh\$guess2 == mh\$prize)/N ## probability 2/3 ```

johnmchambers/XRexamples documentation built on May 18, 2017, 5:09 a.m.