Watts-College / paf-514-template

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

Module 1 Monty Hall Problem Coding Issue In Step Four #64

Open lindaalvarez opened 2 weeks ago

lindaalvarez commented 2 weeks ago

Hello, @castower I'm running into issues in my step four of Module One. I'm watching the video on module one and referring to the 100 envelopes lab. I think i'm getting confused comparing Step Four and the Host Selects 98 envelopes and Store Host's Choice. I back tracked on some on my code but here it is:


select_door <- function( )
{

  doors <- c(1,2,3) 
  a.pick <- sample(1:3, size=1, replace = F)
  return( a.pick )  # number between 1 and 3

}
select_door()
select_door()
select_door()

typeof(my.initial.pick)

######## PSUEDOCODE

INSERT LOGIC HERE

#######


open_goat_door <- function( game, a.pick )
{

 opened.door <- a.pick

   return( opened.door ) # number between 1 and 3

}

this.game <- create_game()

this.game

my.initial.pick <- select_door()

my.initial.pick

open_goat_door( this.game, my.initial.pick )

STEP 4:

####### PSUEDOCODE

#######


change_door <- function( stay=T, opened.door, a.pick )

my.final.pick <- doors [-my.initial.pick]
my.final.pick<- final.door[-open_goat_door]

#switch choice

if (toupper(switch_choice) == "SWITCH") {
  out <- my.final.pick
} else {
  out <- doors[my.initial.pick]
}

   return( final.pick )  # number between 1 and 3

}
castower commented 2 weeks ago

@lindaalvarez

You'll have to decide how you would like to evaluate your if statement in step 4.

The 100 envelopes example uses switch_choice == "STAY".

Your current change_door() function is expecting the "stay" variable and should check if stay == T.

You can either adjust the if statement section to use the stay variable or change your function input.

lindaalvarez commented 2 weeks ago

@castower So Should I go back and start the game store the answers like the 100 envelope lap does? I'm not quite understanding, I changed my choices to stay and i'm still getting an Error: object 'final.door' not found after this code:

change_door <- function( stay=T, opened.door, a.pick )

my.final.pick <- doors [-my.initial.pick] my.final.pick<- final.door[-open_goat_door]

switch choice

if (toupper(switch_choice) == "STAY") { out <- my.final.pick } else { out <- doors[my.initial.pick] }

return( final.pick ) # number between 1 and 3

}

castower commented 2 weeks ago

@lindaalvarez

First, you'll need to define open_goat_door. Thus, you need to make sure you've saved the number of the opened goat door to this variable.

Then you will need to remove both the contestant's choice and the opened goat door from the door selection options (which should be 1, 2, 3).

So, for example, if the contestant picked door number 1 and the host opened door number 2 as the open_goat_door then only door number 3 should be left.

If the contestant switches, then they will now have door number 3/the remaining prize. If they do not switch they will keep door 1/their original prize.

Finally, update your function input to match your if statement:

So if you keep stay as the variable, your input will be the following:

function( stay=T, opened.door, a.pick )

If you use switch_choice as the variable it will be the following:

function( switch_choice, opened.door, a.pick )

lindaalvarez commented 2 weeks ago

I went ahead and did the steps of removing the contestants' choice as well as the goat door but now I'm having issues with saving the host choice. Attached are screenshots of the errors I am getting as well as my code for eliminating player's choice and saving the host choice. @castower

select_host_choice <- function(doors, player_choice) {

Now we need to eliminate the player choice

remainding_door <- doors[-player_choice]

goat_door <- which(remaining_door == "goat")

host_choice <- c(host_choice)

host_choice <- sample(goat_door, size =1)

return(host_choice) }


### Now We Need To Save The Host Choice

```{r, fig.height=6, fig.width=7, echo=F}
host_choice <- select_host_choice(doors, player_choice)

host_choice
# test it

this.game <- create_game()

this.game

my.initial.pick <- select_contestant_door()

my.initial.pick

select_host_choice( this.game, my.initial.pick )
Screen Shot 2024-08-27 at 10 30 09 AM Screen Shot 2024-08-27 at 10 30 17 AM
lindaalvarez commented 2 weeks ago

@castower I went ahead and restarted the project while following the code through video. Everything was going great but now i'm stuck trying to save the goat door/host choice. I attached a screen shot and will add my code here as well, thank you again for helping me.

host_choice <- doors[-my.initial.pick]

host_choice == "goat"

which(host_choice == "goat")
select_host_choice <- function(doors, my.initial.pick) {

  #update our options by taking out contestant's initial pick
  host_choice <- sample(which(host_choice == "goat"), size = 98, replace = F)

  host_choice <- c(host_choice)

  return(host_choice)
}

``

STORE HOST/GOAT CHOICE


host_choice <- select_host_choice(doors, my.initial.pick)

host_choice

Error in sample.int(x, size, replace, prob) : 
  cannot take a sample larger than the population when 'replace = FALSE'

<img width="713" alt="Screen Shot 2024-08-27 at 6 20 30 PM" src="https://github.com/user-attachments/assets/c3f7e648-836d-4a96-b8af-c6848a7e0ac9">
castower commented 2 weeks ago

Hello @lindaalvarez,

Glad to see you're making progress! You'll want to note your error message: Error in sample.int(x, size, replace, prob) : cannot take a sample larger than the population when 'replace = FALSE'

This is because you have a sample size set for 98 (from the example) and you should only have 2 objects to select from. You'll want to update this to only select 1.

lindaalvarez commented 2 weeks ago

I figured it out! Thank you so much for your help!