How does tidymodels initial_split function deal with a strata variable that is continuous?

Viewed 546

Using the R dataset diamonds, I was wondering how the initial_split function stratifies the continuous variable "x" in order to a create test and training set? ie initial_split(diamonds, probability=0.8, strata=x). I had read that the continuous data "x" is separated sequentially into bins, but I couldn't find out how many bins, or if the number of bins could be set by the user?

1 Answers

The number of breaks used by initial_split() when stratifying against a continuous variable is specified by the breaks argument. breaks defaults to 4 bins.

library(rsample)
library(ggplot2)
data("diamonds")

initial_split(diamonds, strata = x, breaks = 4)
#> <Analysis/Assess/Total>
#> <40453/13487/53940>

What is happening under the hood is that make_strata() is used to create a factor variable that is then for stratification. The factor variable will be created based on percentiles to ensure that the strata are of roughly uniform size.

table(make_strata(diamonds$x, breaks = 4))
#> 
#>    [0,4.71]  (4.71,5.7]  (5.7,6.54] (6.54,10.7] 
#>       13521       13665       13379       13375

table(make_strata(diamonds$x, breaks = 10))
#> 
#>    [0,4.36] (4.36,4.54] (4.54,4.82] (4.82,5.23]  (5.23,5.7]  (5.7,6.08] 
#>        5774        5032        5394        5463        5523        5186 
#> (6.08,6.42]  (6.42,6.7]  (6.7,7.31] (7.31,10.7] 
#>        5407        5429        5458        5274
Related