PBIO 294 HW 6 Sep 2017

Solving the Monty Hall problem via simulation using R.

The problem. The contestant has three doors to choose from: door 1, 2, or 3. One door has a large prize behind it. The contestant chooses a door, and then Monty reveals one of the two remaining doors and there is no prize behind it. Monty then offers the contestant the chance to switch his choice of door to the other remaining door. Should the contestant change their door selection or keep their original choice? Does it matter?

Please code a simulation model in R to address this question. You must turn in a program that runs, is clearly annotated, and produces the correct result. Summarize and interpret the results of your simulation experiment.

doors <- c("Door 1","Door 2","Door 3") # make a vector of doors
N <- 1000 # number of iterations for the model for loop

modelResults=c() # create a variable to store the data in the for loop
for(i in 1:N) # looping 1 through N times
{ 
  prizeDoor <- sample(doors)[1] # assign a prize door using "sample" function to randomly sample and then select the first value in the door vector
  
  firstChoice <- sample(doors)[1] # use same scipt to select a door picked  by the contestant. Note that there is still a 1/3 chance this is the same as prizeDoor
  
  goatDoor <- sample(doors[which(doors != firstChoice & doors != prizeDoor)])[1] #create a variable that ~randomly samples from the door vector, using the "which" function to assign criteria so that our model does not pick the prizeDoor or the firstDoor picked by the contestant. The [1] selects the first value in the goatDoor vector
  
  switchDoor <- doors[which(doors != firstChoice & doors != goatDoor)] # a variable that follows the same reasoning at goatDoor above. It tells the model to select (aka switch) doors that are not the contestant's firstChoice or the revealed goatDoor

  
  #### now use IF statements to assign the result from each loop iteraction back to the modelResults vector we created earlier
  if(firstChoice==prizeDoor){modelResults=c(modelResults,"First choice wins")} # is the contestant's first choice is the same as the price door, then assign "First choice wins" to the model results vectore.
  if(switchDoor==prizeDoor){modelResults=c(modelResults,"Switch Win")} # see above, but results for switching doors
}

length(which(modelResults == "Switch Win"))/N # calculate the number of times in the model results where a "switch win" appears, then divide by N to get proportion, which should be around 0.66 or 66%
## [1] 0.661
length(which(modelResults == "First choice wins"))/N #same as above. Should be around 0.33
## [1] 0.339

Plot ‘convergence’ to true winning proportions

## Plot winning proportions iterated over N trials
## Blue is switching doors, red is staying

plot(cumsum(modelResults == "Switch Win") / c(1:N), main = "Winning Proportions for Switching", xlab = "Trial", ylab = "Win Percent", ylim = c(0, 1), col = "blue") 

plot(cumsum(modelResults == "First choice wins") / c(1:N), main = "Winning Proportions for NOT Switching", xlab = "Trial", ylab = "Win Percent", ylim = c(0, 1), col = "red") 

Interpretation

There is 66% probability that switching doors results in the correctly chosen door (and a 33% chance if you stay). This is because during the first choice, the contestant has 1/3rd probability of chosing the correct door. Once the goat door is revealed, this then means the probability for the remaining door is elevated to 2/3rds.