DS4PS / cpp-527-fall-2020

http://ds4ps.org/cpp-527-fall-2020/
0 stars 1 forks source link

Lab 02, Part 02 #8

Open ecking opened 4 years ago

ecking commented 4 years ago

Hi there,

I'm using the solutions from last week and added the wrapper of play game. But I'm getting an error. Error in build_doors(n = length(game)) : argument "game" is missing, with no default

I'm not sure how to figure this out. From my research i'm guessing it has something to do with a function() but I'm not sure.

lecy commented 4 years ago

I'm a little confused. What question are you working on? And what code are you running that you hit that error?

Solutions from last week for the basic game set-up, or Part 02?

Note the build_doors() function requires the argument n and the select_door() function requires game.

Are there a defaults provided?

What information do you think build_doors() would need in order to create a set of doors?

Hint, what does length(game) return?

create_game( num.goats=7, num.cars=3 )

build_doors <- function( n ){ return( 1:n ) }

# we had to add a 'game' argument so we can get length(game)

select_door <- function( game )
{
  doors <- build_doors( n=length(game) ) 
  a.pick <- sample( doors, size=1 )
  return( a.pick )  # number between 1 and N
}
JayCastro commented 4 years ago

Hi, Im getting this error. "the condition has length > 1" This is the code Im running

results.df <- NULL
i <- 1
for( i in 1:10000 )  # iterator
{
  game.outcome <- play_game()
  i <- 1 + i
  results.df <- rbind( results.df, game.outcome )
}

table( results.df ) 

( table( results.df ) / 10000 ) %>% 
  round( 2 )
ecking commented 4 years ago

I'm a little confused. What question are you working on? And what code are you running that you hit that error?

Solutions from last week for the basic game set-up, or Part 02?

Note the build_doors() function requires the argument n and the select_door() function requires game.

Are there a defaults provided?

What information do you think build_doors() would need in order to create a set of doors?

Hint, what does length(game) return?

create_game( num.goats=7, num.cars=3 )

build_doors <- function( n ){ return( 1:n ) }

# we had to add a 'game' argument so we can get length(game)

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

Sorry yes, from Part 2. I took the code from the solutions in Lab 1.

I have a chunk that starts with build_doors and I have a second code chunk for specifying the number of goats and cars as shown below.

Then I have the play_game chunk and when I run it it's saying error in build_doors (n = length(game)): argument "game" is missing with no default. Did I need to do something with the code from last week as it relates to the build_doors?

num.goats <- 9
num.cars <- 1
this.game <- create_game( num.goats, num.cars )
my.initial.pick <- select_door( game=this.game )
opened.door <- open_goat_door( this.game, my.initial.pick )
# save results for both strategies for the game
my.final.pick.stay <- change_door( stay=T, game=this.game, 
                                   opened.door=opened.door, a.pick=my.initial.pick )
my.final.pick.switch <- change_door( stay=F, game=this.game,
                                     opened.door=opened.door, 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 )
play_game <- function()
{
  new.game <- create_game( num.goats, num.cars)
  first.pick <- select_door()
  opened.door <- open_goat_door ( new.game, first.pick )
  final.pick.stay <- change_door( stay=T, opened.door, first.pick )
  final.pick.switch <- change_door( stay=F, opened.door, first.pick )
  outcome.stay <- determine_winner( final.pick.stay, new.game  )
  outcome.switch <- determine_winner( final.pick.switch, new.game )

  # game.results <- bundle the results
  # return( <<< game.results >>> )

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

play_game()
lecy commented 4 years ago

You need to pass the game to the new select_door() function because it has no default.

new.game <- create_game( num.goats, num.cars)
first.pick <- select_door( game=new.game )
lecy commented 4 years ago

@JayCastro it means you are using more than one TRUE/FALSE for an IF statement, I believe.

I would need more code to diagnose, though. The error is coming from play_game(), not the loop.

ecking commented 4 years ago

You need to pass the game to the new select_door() function because it has no default.

new.game <- create_game( num.goats, num.cars)
first.pick <- select_door( game=new.game )

Ah ok I missed that. I'm getting a similar error now: Error in change_door(stay = T, opened.door, first.pick) : argument "a.pick" is missing, with no default

But I don't really understand it. Why would a.pick come up in this error in this line?

lecy commented 4 years ago

@ecking I can't re-create the error because I am not sure how you created opened.door and first.pick.

I would need a reproducible example not just a code snippet.

ecking commented 4 years ago

I suspect I'm doing something wrong in the play_game wrap. 🤔


play_game <- function( )
{
  new.game <- create_game(num.goats, num.cars)
  first.pick <- select_door(game = new.game)
  opened.door <- open_goat_door( new.game, first.pick)
  final.pick.stay <- change_door( stay=T, opened.door, first.pick )
  final.pick.switch <- change_door( stay=F, opened.door, first.pick )
  outcome.stay <- determine_winner( final.pick.stay, new.game  )
  outcome.switch <- determine_winner( final.pick.switch, new.game )

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

play_game()
ecking commented 4 years ago

Oh nevermind. a.pick needed to be passed.

lecy commented 4 years ago

Where are you passing a.pick? What did you change?