Watts-College / paf-514-template

https://watts-college.github.io/paf-514-template/
1 stars 0 forks source link

Lab 2 - Simulations - Switching works 100% of the time? #71

Closed swest235 closed 3 weeks ago

swest235 commented 1 month ago

@castower I used the solutions code and just wrapped it in the play.game(), then proceeded as instructed in the video. I'm getting results that say switching wins 100% of the time.

Could you please help me isolate what is happening?

rep( c("goat","car"), times=c(3,2) )
build_doors <- function( n=5 ){ return( 1:n ) }

create_game <- function( )
{
    a.game <- sample( x=rep( c("goat","car"), c(3,2) ), size=5, replace=F )
    return( a.game )
}

select_door <- function( )
{
  doors <- build_doors() 
  a.pick <- sample( doors, size=1 )
  return( a.pick )  # number between 1 and 5
}

open_doors <- function( game, a.pick )
{
   # reveal one car and one goat

   doors <- build_doors()

   if( game[ a.pick ] == "car" )
   { 
     opened.car.door <- doors[ game == "car" & doors != a.pick ]
     goat.doors <- doors[ game != "car" ] 
     opened.goat.door <- sample( goat.doors, size=1 )
     opened.doors <- c( opened.car.door, opened.goat.door )
   }

   if( game[ a.pick ] == "goat" )
   { 
     opened.car.door <- sample( doors[game=="car"], size=1 )
     available.goat.doors <- doors[ game != "car" & doors != a.pick ] 
     opened.goat.door <- sample( available.goat.doors, size=1 )
     opened.doors <- c( opened.car.door, opened.goat.door )
   }
   return( opened.doors ) # two numbers
}

change_door <- function( stay=T, opened.doors, a.pick )
{
   doors <- build_doors()

   if( stay )
   {
     final.pick <- a.pick
   }
   if( ! stay )
   {
     available.doors <- doors[ ! ( doors %in% opened.doors | doors == a.pick ) ]
     final.pick  <- sample( available.doors, size=1 ) 
   }

   return( final.pick )  # number between 1 and 5
}

determine_winner <- function( final.pick, game )
{
   if( game[ final.pick ] == "car" )
   {
      return( "WIN" )
   }
   if( game[ final.pick ] == "goat" )
   {
      return( "LOSE" )
   }
}
play.game <- function()
{
this.game <- create_game()
my.initial.pick <- select_door()
opened.doors <- open_doors( this.game, my.initial.pick )
my.final.pick.stay <- change_door( stay=T, opened.doors=opened.doors, a.pick=my.initial.pick )
my.final.pick.switch <- change_door( stay=F, opened.door=opened.doors, a.pick=my.initial.pick )

game.outcome.stay <- determine_winner( final.pick=my.final.pick.stay, game=this.game )
game.outcome.switch <- determine_winner( final.pick=my.final.pick.switch, game=this.game )

strategy <- c("stay", "switch")
outcome <- c(outcome.stay, outcome.switch)
game.results <- data.frame(strategy, outcome, stringsAsFactors=F)

return(game.results)
}

play.game()
results.df <- NULL   # collector

for( i in 1:10000 )  # iterator
{
  game.outcome <- play.game()
  # binding step
  results.df <- rbind( results.df, game.outcome )
}

table( results.df ) 
swest235 commented 1 month ago

image

swest235 commented 1 month ago

@castower I think I realized my mistake. My outcome.stay/outcome.switch objects had "game." before them and I didn't include that when combining the two into my outcome object. I cleared my environment and fixed those, now we're cooking.

castower commented 1 month ago

Great @swest235! Glad you were able to resolve it. Let me know if you have any other questions.