Stratified sampling in R , Screen -5

Here , we’re using only ‘No. of games played’ VARIABLE & for stratified sampling . Why players’ position variable POS is not considered here for sample_mean for our purpose.

What is the role of x in the sample mean( ).

Hi @sharathnandalike. We are using the Games_Played variable here to practice using the cut() function and other methods for creating stratum with a quantitative variable.

Regarding your second question regarding the role of x in the sample_mean() function, essentially we are saying when creating the function that it requires a single input, x. When used in the map() function the input is sample_number(). We need the x argument for the sample_mean() function to work with map. If we build the sample_mean() function without specifying a single input x the code run will fail in the map() call with the following error message from R/purrr:

Error in .f(.x[[i]], ...): unused argument (.x[[i]])
Traceback:

1. map_dbl(sample_number, sample_mean)
2. .f(.x[[i]], ...)

Essentially map() didn’t use the specified argument because the sample_mean() function did not require an argument when built like this:

sample_mean <- function(){
under_12 <- wnba %>% 
  filter(Games_Played <= 12) %>% 
  sample_n(1)
btw_13_22 <- wnba %>% 
  filter(Games_Played > 12 & Games_Played <= 22) %>% 
  sample_n(2)
over_22 <- wnba %>% 
  filter(Games_Played > 22) %>% 
  sample_n(7)

combined <- bind_rows(under_12, btw_13_22, over_22)
mean(combined$PTS)
}

Good question!