Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Randomize strata #363

Merged
merged 1 commit into from
Aug 30, 2022
Merged

Randomize strata #363

merged 1 commit into from
Aug 30, 2022

Conversation

mikemahoney218
Copy link
Member

@mikemahoney218 mikemahoney218 commented Aug 28, 2022

This is a follow-up to #360 . I realized that the function was deterministic for any strata with only one group, and that it would always "short change" the last strata in the vector by giving it fewer folds as options (if length(unique(strata)) %% v != 0). Both of these strike me as small, but non-ideal bugs in the function; this PR changes the function so that the order of strata themselves are randomized, which I believe fixes both behaviors.

Sorry to pitch a breaking change right after we merged the first version of the feature -- I had this as a shower thought this morning and realized it wouldn't be that hard to change.

I made this reprex and I don't know that it's actually helpful at all, but here's proof that this stratified sampling works:

library(rsample)
set.seed(11)

group_table <- tibble::tibble(
  group = 1:100,
  outcome = sample(c(rep(0, 70), rep(1, 30)))
)
observation_table <- tibble::tibble(
  group = sample(1:100, 1e5, replace = TRUE),
  observation = 1:1e5
)
sample_data <- dplyr::full_join(group_table, observation_table, by = "group")

strata_rate <- purrr::map(
  1:100,
  \(x) {
    rs4 <- group_vfold_cv(sample_data, group, v = 5, strata = outcome)
    purrr::map_dbl(
      rs4$splits,
      function(x) {
        dat <- as.data.frame(x)$outcome
        mean(dat == "1")
      }
    )
  }
) |> unlist()

base_rate <- purrr::map(
  1:100,
  \(x) {
    rs4 <- group_vfold_cv(sample_data, group, v = 5)
    purrr::map_dbl(
      rs4$splits,
      function(x) {
        dat <- as.data.frame(x)$outcome
        mean(dat == "1")
      }
    )
  }
) |> unlist()

# Mean absolute error of strata proportions, versus expected
mean(abs(0.3 - strata_rate))
#> [1] 0.001477664
mean(abs(0.3 - base_rate))
#> [1] 0.01895246

Created on 2022-08-27 by the reprex package (v2.0.1)

@mikemahoney218 mikemahoney218 marked this pull request as ready for review August 28, 2022 02:28
@mikemahoney218 mikemahoney218 requested a review from hfrick August 28, 2022 02:28
Copy link
Member

@hfrick hfrick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thank you! 🚿

@hfrick hfrick merged commit 188eff1 into main Aug 30, 2022
@hfrick hfrick deleted the mike/randomize_strata branch August 30, 2022 17:25
@github-actions
Copy link

This pull request has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants