STAT 360 Lab 17: Non-Spatial Simulations (Part 2)

Name: Rebecca Lewis

Set Up Work Space

Load R Libraries

library(rmarkdown)
library(knitr)

Define Additional Functions

resample <- function(x, ...) x[sample.int(length(x), ...)]

Set the Seed

set.seed(33)

The Monty Hall (Game Show) Problem

Background:

Suppose you are on a game show and are given the choice to open one of three doors. Behind one of the three doors is a brand new car; behind the other two, goats. After you select a door, the host (who knows what is behind each door) opens one of the two doors that you did not select, revealing a goat. The host then asks you if you would like to switch your selection to the other unopened door. Is it to your advantage to switch your choice?

Constructing a Function

Exercise 1:

Use function() to construct a function that takes no arguments and returns a TRUE or FALSE value depending on whether or not switching doors resulted in the contestant winning the good prize and assign it to the object montyHall. Hint: the function return() will come in handy.

montyHall<- (function(){possibleDoors<-c(1,2,3)
prizeDoor<-resample(possibleDoors,1)
chosenDoor<-resample(possibleDoors,1)
revealDoor<- if(prizeDoor==chosenDoor){resample(possibleDoors[-prizeDoor],1)} else{resample(possibleDoors[c(-prizeDoor,-chosenDoor)],1)}
switchedDoor<-resample(possibleDoors[c(-chosenDoor,-revealDoor)],1)
outcome<-switchedDoor==prizeDoor
return(outcome)})

Exercise 2:

Use montyHall() to simulate the actions of a single contestant and determine if switching doors resulted in the contestant winning the good prize.

montyHall()
## [1] FALSE
Switching doors did not result in the contestant winning the good prize.

Simulating Multiple Game Show Contestants

Exercise 3:

Use replicate() and montyHall() to simulate the actions of a single contestant 5,000 times, calculate what proportion of the time switching doors resulted in the contestant winning the good prize, and display the result. Hint: most programming languages, including R, treat TRUEs as 1s and FALSEs as 0s when used in conjunction with functions such as sum(), min(), and max().

Replicating<-replicate(5000, montyHall())
mean(Replicating)
## [1] 0.6618

Exercise 4:

Use rep() to construct two separate blank vectors of 5000 NAs each and assign them to simResults and cumulativeProb. Hint: the value of NA (Not Available) is used to represent blank or missing values within a vector, matrix, or array.

simResults<-rep(NA,5000)
cumulativeProb<-rep(NA,5000)

Exercise 5:

Use for() to construct a loop that will call montyHall() and assign the resulting value to each consecutive element of simResults 5,000 times. Hint: you will need to designate which element of simResults should be used to record each consecutive montyHall() result.

 for (i in 1:length(simResults)){simResults[i]<-(montyHall())}

Exercise 6:

Use for() to construct a loop that will calculate the cumulative probability for each element in simResults (i.e., whether or not switching doors resulted in the contestant winning the good prize) and assign it to cumulativeProb. Hint: the cumulative probability at the first observation is the proportion of times that switching resulted in the good prize during the first observation, the cumulative probability at the second observation is the proportion of times that switching resulted in the good prize during the first two observations, etc.

for(i in 1:length(cumulativeProb)){cumulativeProb[i]<-sum(simResults[1:sum(i)]==TRUE)/sum(i)}

Exercise 7:

Use plot() to generate a cumulative probability plot of the Monty Hall simulation and identify which value the empirical probability settles on.

plot(cumulativeProb, main="The Cumulative Probability of Switching To The Car's Door", xlab="The Number of Times Switching Doors", ylab="The Cumulative Probability")

cumulativeProb[5000]
## [1] 0.6662
The empirical probability settles on 66.62% or 2/3. 

Exercise 8:

Based on the results of this simulation, is it more advantageous for the contestant to switch his or her choice?

It is more advantageous for the contestant to switch his/her choice, since there is a 2/3 probability that the car is behind the switched door compared to the 1/3 probability it is behind the non-switched door.