--- title: "Lab 5: Debugging" author: 'Group Member Names - here' date: "February 13, 2018" output: html_document --- 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 Sunday, February 18. ## 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. ```{r, error=TRUE} 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) RouletteSpin(4) RouletteSpin('4') RouletteSpin(4.1) ``` ### 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](https://www.youtube.com/watch?v=Zr_xWfThjJ0) or [from numb3rs tv show](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjLwcvG1rTWAhUrsVQKHWq9CbEQyCkIKjAA&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DP9WFKmLK0dc&usg=AFQjCNEYUi91dn4_xNLXQr9pESAdn9aApQ). ```{r, error= TRUE} 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) ```