---
title: "ETC 2420/5242 Lab 2 2017"
author: "Di Cook"
date: "Week 2"
output: pdf_document
---

```{r, echo = FALSE, message = FALSE, warning = FALSE, warning = FALSE}
knitr::opts_chunk$set(
  message = FALSE,
  warning = FALSE,
  error = FALSE, 
  collapse = TRUE,
  comment = "#",
  fig.height = 4,
  fig.width = 8,
  fig.align = "center",
  cache = FALSE
)
```

## Purpose

This lab is an introduction to simulation. We are going to create a version of the famous game Monty Hall. 

## Description of the game

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, 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? [Source: wikipedia](https://en.wikipedia.org/wiki/Monty_Hall_problem)

## Exercise 1

Go to the web site [InterActivate's Simple Monty Hall](http://www.shodor.org/interactivate/activities/SimpleMontyHall/). Each member of the group should do this themselves.

- Play the game 21 times with the strategy "Do not switch doors". Note the proportion of times you win the jackpot.

- Play the game 21 times with the strategy "Switch doors". Note the proportion of times you win the jackpot.

Discuss with your group how the two strategies change your chances of winning.

## The game algorithm

The components for the simulation need to be:

(1) Randomly assign `{pig, pig, car}` to door `{1, 2, 3}`.

(2) Player selects a door

(3) Check if door selected matches a `{pig}`, and if so show the other `{pig}`. If it is the `{car}`, randomly select one of the two other doors to reveal

(4) Enable the player choose their next door, either to `switch` or `stay`.

(5) Print result

## Question 1

Here is a function that will randomly sample one value from a choice of `{pig, pig, car}`

```{r}
ppc_sample <- function(){
  sample(c("pig","pig","car"), 1)
}
```

- Run this function 20 times and report the proportion of times that `car` is selected. (You could put this in a loop and compute the proportion of times `car` shows.)
- How many times would you expect `car` to be selected out of 20 runs?
- Modify the function so that it takes one argument, which will be the player's initial choice of door. 

## Question 2

- Modify your function so that it shows the player the door that the host selects.

## Question 3

- Modify your function so that it includes the player strategy as an input, and returns the appropriate result.

## Question 4

- Run your function 100 times, for each strategy and report the proportion of times that the player wins the car. (You might want to do this in a loop to automate it. Also, if you have had trouble writing your function, you can use the code at the bottom of this Rmd document, which simulates the game - it can be found on the web, too.)

## Question 5

- What is different about Monty Hall, and the popular TV game show "Deal or No Deal"? Players are given the choice to switch cases, if they play to the very end of the game. Do they get an advantage if they switch cases?

## TURN IN 

- Your `.Rmd` file
- Your `.html` file that results from knitting the Rmd.
- Make sure your group members are listed as authors, one person per group will turn in the report

```{r echo=FALSE, eval=FALSE}
#####################################################
# Simulation of the Monty Hall Problem
# Demonstrates that switching is always better
# than staying with your initial guess
#
# Corey Chivers, 2012
#####################################################
 
monty<-function(strat='stay',N=1000,print_games=TRUE)
{
 doors<-1:3 #initialize the doors behind one of which is a good prize
 win<-0 #to keep track of number of wins
 
for(i in 1:N)
 {
 prize<-sample(1:3, 1) #randomize which door has the good prize
 guess<-sample(1:3, 1) #guess a door at random
 
## Reveal one of the doors you didn't pick which has a bum prize
 if(prize!=guess)
 reveal<-doors[-c(prize,guess)]
 else
 reveal<-sample(doors[-c(prize,guess)],1)
 
 ## Stay with your initial guess or switch
 if(strat=='switch')
 select<-doors[-c(reveal,guess)]
 if(strat=='stay')
 select<-guess
 if(strat=='random')
 select<-sample(doors[-reveal],1)
 
## Count up your wins
 if(select==prize)
 {
 win<-win+1
 outcome<-'Winner!'
 }else
 outcome<-'Losser!'
 
if(print_games)
 cat(paste('Guess: ',guess,
 '\nRevealed: ',reveal,
 '\nSelection: ',select,
 '\nPrize door: ',prize,
 '\n',outcome,'\n\n',sep=''))
 }
 cat(paste('Using the ',strat,' strategy, your win percentage was ',win/N*100,'%\n',sep='')) #Print the win percentage of your strategy
}
```


## Resources


- [R Bloggers post](http://www.r-bloggers.com/monty-hall-by-simulation-in-r/)
- [Web app](http://www.shodor.org/interactivate/activities/SimpleMontyHall/) where you can play the game


