Turn in one copy for each group. If group members are not present in class they will be required to complete their own lab to receive credit. Please turn in both an HTML file and your R Markdown script. This is due by the end of the day on Monday, October 9.

Lab Overview

This lab focuses on debugging functions that I have “started” for you.

Questions

Answer the following questions in this R Markdown document. Please include code where necessary.

1. Debugging Fuction 1 (50 points)

Debug the following function, by rewritting the function below and demonstrating that the function calls specified below return the correct answer.

RouletteSpin <- function(num.spins){
  # function that simulates Roulette Spins
  # ARGS: num.spins - number of spins can be encoded as an integer or double,
  #       but must be an integer value
  # Returns: data frame consisting of color (character) and number (double), 
  #          there are 38 possible outcomes with two '0' values (0, 00)
  if (!is.integer(num.spins)) stop('Please enter an integer or double')
  if (!num.spins %% 1 == 0) stop('Please enter an integer or double')
  numbers <- 00:36
  # assume all odds are red
  colors <- c(rep("green",2),rep('red',black,each=18))
  roulette.board <- data.frame(numbers = numbers, colors = colors)
  spin.results <- matrix(0, nrow=num.spins, ncol=2)
  for (i in num.spins)
    spin.results[i,] <- roulette.board[sample(38,1)] 
  return(spin.results)
}

RouletteSpin(4L)
## Error in RouletteSpin(4L): object 'black' not found
RouletteSpin(4)
## Error in RouletteSpin(4): Please enter an integer or double
RouletteSpin('4')
## Error in RouletteSpin("4"): Please enter an integer or double
RouletteSpin(4.1)
## Error in RouletteSpin(4.1): Please enter an integer or double

2. Debugging Fuction 2 (50 points)

Debug the following function, by rewritting the function below and demonstrating that the function calls specified below return the correct answer. For a short video of the Monty Hall problem see from 21 with Kevin Spacey or from numb3rs tv show.

MontyHallMonteCarlo <- function(num.sims, print){
  # Function to simulate Monty Hall winning probability when switching doors
  # ARGS: number of simulations (as integer or double), print command
  #       that accepts TRUE or FALSE as to whether to print simulation results
  # Returns: list containing winning probability and (if print = TRUE)
  #          vector of results with strings "Win" or "Lose" for each simulation
  if (!num.spins %% 1 == 0) stop('Please enter an integer or double')
  results <- rep(FALSE,num.sims)
  for (i in 1:num.sims){
    # randomly choose door with car
    car.door <- sample(3,1)
    # randomly choose door for participant to select
    select.door <- sample(1,3)
    # you win when switching if the door with a car is not the
    # one you initally selected
    if (car.door = select.door) {
      results <- FALSE
    }
  }
  win.prob <- mean(results)
  ifelse(print, return(list(win.prob,results)),return(list(win.prob))
}

MonteHallMonteCarlo(8.1,print=T)
MonteHallMonteCarlo('8.1',print=T)
MonteHallMonteCarlo(8,print=T)
MonteHallMonteCarlo(10000,print=F)
## Error: <text>:16:18: unexpected '='
## 15:     # one you initally selected
## 16:     if (car.door =
##                      ^