Monty Hall Simulations

March 20, 2012

The Monty Hall Problem

There’ve been many simulations of the Monty Hall-problem done in R. But since I’m trying to learn R, I wanted to try to simulate the paradox over many different trails and plot them all using ggplot2. The problem was originally posed as follows (from wikipedia):

“Suppose you’re on a game show, and you’re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1 [but the door is not opened], and the host, who knows what’s behind the doors, opens another door, say No. 3, which has a goat. He then says to you, ‘Do you want to pick door No. 2?‘. Is it to your advantage to switch your choice?”

Writing the R code

Most of you already now what strategy is best. But to try it out I wrote the following code:

To keep it simple, we always chose door number 1 as our first choice. The simulation would run the same, regardless of which door we choose. Since we always start with door 1, we can calculate which door we must switch to by 2 + (winning.door.matrix != 2). We can never choose to switch to either door 2 or 3 in the same game. Because if door 2 is the winner, door 3 will be opened by the host and if door 3 is the winner, door 2 will be opened by the host. Therefore, if we switch to 2 and 2 is the winner the condition in this calculation will be FALSE, hence 2 will be added to switch.door.matrix. And, likewise, if door 2 is not the winner, the condition will be TRUE and 3 will be added to switch.door.matrix. If the winning door is 1 the host will open either door 2 or 3. And we will add 2 to our matrix, because the condition is not met. Since we record switches in this way, we can calculate winnings by switch.door.matrix == winning.door.matrix.


I ran the codes using different values for trails (T) and guesses (N). I started with 100 trails x 10 guesses and ended with 100 trails x 10 000, and it clearly demonstrates that we need quite a lot of guesses before the lines converge towards the expected value. The 100 x 10 000 simulation gave a mean probability of winning if you chose not to change door = 0.333701 and 0.666299 if you always switched door. But already at the 100 x 100 plot we can see that it’s always better to switch door.

Monty Hall 100 x 10. By Kristoffer Magnusson Monty Hall 100 x 50. By Kristoffer Magnusson Monty Hall 100 x 100. By Kristoffer Magnusson Monty Hall 100 x 500. By Kristoffer Magnusson Monty Hall 100 x 5000. By Kristoffer Magnusson Monty Hall 100 x 10000. By Kristoffer Magnusson

Resources used

For this R session I used the following books:
Matloff, N. (2011). The Art of R Programming. San Francisco: No Starch Pess.
Wickham, H. (2009). ggplot2: elegant graphics for data analysis. New York: Springer.

Written by Kristoffer Magnusson a researcher in clinical psychology. You should follow him on Twitter and come hang out on the open science discord Git Gud Science.


Published March 20, 2012 (View on GitHub)