# Monty Hall in R

**Posted:**September 13, 2008

**Filed under:**programming, R-project, rosetta code 2 Comments

Inspired by paddy3118, I decided to write up a Monty Hall simulation in R for Rosetta Code. Enjoy!

… The rules of the game show are as follows: After you have chosen a door, the door remains closed for the time being. The game show host, Monty Hall, who knows what is behind the doors, now has to open one of the two remaining doors, and the door he opens must have a goat behind it…

# Since R is a vector based language that penalizes for loops, we will avoid

# for-loops, instead using “apply” statement variants (like “map” in other

# functional languages).

## RULES

# The contestant in in front of three doors that he cannot see behind..

# The three doors conceal one prize and the rest being booby prizes, arranged randomly.

# The Host asks the contestant to choose a door.

# The host then goes behind the doors where only he can see what is

# concealed, then always opens one door, out of the other s not chosen

# by the contestant, that must reveal a booby prize to the contestant.

# The host then asks the contestant if he would like either to stick with

# his previous choice, or switch and choose the other remaining closed door.

set.seed(19771025) # set the seed to set the same results as this code

N <- 10000 # trials
true_answers <- sample(1:3, N, replace=TRUE)
# We can assme that the contestant always choose door 1 without any loss of
# generality, by equivalence. That is, we can always relabel the doors
# to make the user-chosen door into door 1.
# Thus, the host opens door '2' unless door 2 has the prize, in which case
# the host opens door 3.
host_opens <- 2 + (true_answers == 2)
other_door <- 2 + (true_answers != 2)
## if always switch
summary( other_door == true_answers )
## if we never switch
summary( true_answers == 1)
## if we randomly switch
random_switch = .5] <- 1
summary(random_switch == true_answers)
## To go with the exact parameters of the Rosetta challenge, complicating matters....
## Note that the player may initially choose any of the three doors (not just Door 1),
## that the host opens a different door revealing a goat (not necessarily Door 3), and
## that he gives the player a second choice between the two remaining unopened doors.
N <- 10000 #trials
true_answers <- sample(1:3, N, replace=TRUE)
user_choice <- sample(1:3, N, replace=TRUE)
## the host_choice is more complicated
host_chooser <- function(user_prize) {
# this could be cleaner
bad_choices <- unique(user_prize)
# in R, the x[-vector] form implies, choose the indices in x not in vector
choices <- c(1:3)[-bad_choices]
# if the first arg to sample is an int, it treats it as the number of choices
if (length(choices) == 1) { return(choices)}
else { return(sample(choices,1))}
}
host_choice <- apply( X=cbind(true_answers,user_choice), FUN=host_chooser,MARGIN=1)
not_door <- function(x){ return( (1:3)[-x]) } # we could also define this
# directly at the FUN argument following
other_door <- apply( X = cbind(user_choice,host_choice), FUN=not_door, MARGIN=1)
## if always switch
summary( other_door == true_answers )
## if we never switch
summary( true_answers == user_choice)
## if we randomly switch
random_switch <- user_choice
change = .5
random_switch[change] <- other_door[change]
summary(random_switch == true_answers)
## AUTHOR: Gregg Lind
## Date: 9/13/2008
## License: Public Domain, attribution politely requested
## Purpose: Two variations on the Monty Hall problem written in R
[/sourcecode]
> ## if always switch
> summary( other_door == true_answers )
Mode FALSE TRUE
logical 3298 6702
> ## if we never switch
> summary( true_answers == 1)
Mode FALSE TRUE
logical 6702 3298
> ## if we randomly switch
> summary(random_switch == true_answers)
Mode FALSE TRUE
logical 5028 4972
> ## if always switch
> summary( other_door == true_answers )
Mode FALSE TRUE
logical 3295 6705
> ## if we never switch
> summary( true_answers == user_choice)
Mode FALSE TRUE
logical 6705 3295
> ## if we randomly switch
> summary(random_switch == true_answers)
Mode FALSE TRUE
logical 4986 5014

It feels good to pass on such an interesting game, and since I stumbled across Rosetta Code, I’ve really enjoyed contributing.

– Paddy.

I shared your solution with my intro stats class. They really enjoyed it.

Thanks!

A