Created
February 27, 2012 17:24
-
-
Save timriffe/1925641 to your computer and use it in GitHub Desktop.
ModelThinking- Monty Hall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MontyHall <- function(verbose=TRUE){ | |
choices <- as.character(1:3) | |
rightchoice <- sample(choices,size=1) | |
yourfirstchoice <- select.list(choices=choices,multiple=FALSE,title="First Guess") | |
remaining <- choices[!choices %in% yourfirstchoice] | |
eliminate <- sample(choices[!choices %in% c(rightchoice,yourfirstchoice)],size=1) | |
otherdoor <- choices[!choices %in% c(eliminate,yourfirstchoice)] | |
if (verbose){ | |
cat("You chose door",yourfirstchoice,"\n") | |
cat("Of the other two doors, I can tell you that it's definitely NOT door",eliminate,"\n") | |
cat("given that info, would you rather switch your answer to door",otherdoor,"or keep the first choice, door",yourfirstchoice,"?\n") | |
} | |
switchornot <- select.list(choices=c(paste("keep door",yourfirstchoice),paste("switch to door",otherdoor)),multiple=FALSE,title="Switch or Not?") | |
switchornot <-unlist(strsplit(switchornot,split="")) | |
switchornot <- switchornot[length(switchornot)] | |
if (switchornot == rightchoice){ | |
cat("you win!\n") | |
} else { | |
cat("you are so wrong!\n") | |
} | |
return(c(rightchoice=rightchoice,firstchoice=yourfirstchoice,finalchoice = switchornot,switched=(yourchoice == switchornot),correct=(rightchoice == switchornot))) | |
} | |
# i.e. you'll need to click twice per run. I suggest just behaving consistently with regard to | |
# switching or keeping- then you'll see the pattern: | |
MyAnswers <- t(replicate(25,MontyHall())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment