In a previous post, we discussed why R is a great choice to simplify your data and statistical analysis. R has a steep learning curve at first, but with practice, the advantages of R far outweigh the initial bumps. In this post, we provide an example of using R to solve a well-known brain teaser – The Monty Hall Problem.

### Monty Hall Problem

You are a contestant on a game show. In front of you are 3 doors A, B, and C. Behind one is a cash prize and the other two hide goats. If you can correctly select the prize door then the prize is yours.

The host asks you to pick a door. Regardless if your choice is a prize door or goat door, the host will reveal an unselected goat door. You are then given these options: change your selection to the other unopened door or stay with the original.

What is the right choice? Should you stay or change? Is there even a difference? The answer may surprise you.

We can write a simple R function to answer this question.

Calculating the win percentage based on staying with the original guess is relatively straight forward. Just determine how often a random guess matches with a randomly generated prize door.

Figuring out the percentage of winning if you switch might seem complicated at first but we actually just need to figure out how often our original guess picks a goat door. When given the choice between staying or switching there is only one goat door and one prize door left. If a goat door was selected originally, then switching will pick the prize door, while if the prize door was selected, switching picks a goat door.

Now let’s run 1000 simulations of the game (nsim=1000), and see what the win percentages are for each strategy.

Not bad. Very close to 1/3 and 2/3. At the start, there is a 1 in 3 chance of selecting the prize door and a 2 in 3 chance of selecting a goat door. The best strategy is therefore to switch your guess. You will have double the chance of winning.

This may seem counter-intuitive, but let’s frame the game in a slightly different manner. If there are 50 doors (49 goat and 1 cash prize). After the host reveals 48 goat doors, would you then switch or stay? Let’s modify the original code above to take the input “ndoors” for how many doors are in the game.

With 50 doors and 1000 simulations the output is as follows:

At the start, there is a 1 in 50 chance of selecting the correct door, or 2% which is about what we got above with 1000 simulations.

Clearly switching is the right strategy!