简体   繁体   中英

Avoiding empty and small groups when using pretty_breaks with cut2

I'm working with variables resembling the data val values created below:

# data --------------------------------------------------------------------

data("mtcars")
val <- c(mtcars$wt, 10.55) 

I'm cutting this variable in the following manner:

# Cuts --------------------------------------------------------------------

cut_breaks <- pretty_breaks(n = 10, eps.correct = 0)(val)
res <- cut2(x = val, cuts = cut_breaks)

which produces the following results:

> table(res)
res
[ 1, 2) [ 2, 3) [ 3, 4) [ 4, 5) [ 5, 6)       6       7       8       9 [10,11] 
      4       8      16       1       3       0       0       0       0       1

In the created output I would like to change the following:

  • I'm not interested in creating grups with one value. Ideally, I would like to for each group to have at least 3 / 4 values. Paradoxically, I can leave with groups having 0 values as those will dropped later on when mergining on my real data
  • Any changes to the cutting mechanism, have to work on a variable with integer values
  • The cuts have to be pretty. I'm trying to avoid something like 1.23 - 2.35. Even if those values would be most sensible considering the distribution.
  • In effect, what I'm trying to achieve is this: try to make more or less even pretty group and if getting a really tiny group then bump it together with the next group, do not worry about empty groups .

Full code

For convenience, the full code is available below:

# Libs --------------------------------------------------------------------

   Vectorize(require)(package = c("scales", "Hmisc"),
                      character.only = TRUE)


   # data --------------------------------------------------------------------

   data("mtcars") val <- c(mtcars$wt, 10.55) 

   # Cuts --------------------------------------------------------------------

   cut_breaks <- pretty_breaks(n = 10, eps.correct = 0)(val) res <-
   cut2(x = val, cuts = cut_breaks)

What I've tried

First approach

I tried to play with the eps.correct = 0 value in the pretty_breaks like in the code:

cut_breaks <- pretty_breaks(n = cuts, eps.correct = 0)(variable)

but none of the values gets me anwhere were close

Second approach

I've also tried using the m= 5 argument in the cut2 function but I keep on arriving at the same result.


Comment replies

My breaks function

I tried the mybreaks function but I would have to put some work into it to get nice cuts for more bizzare variables. Broadly speaking, pretty_breaks cuts well for me, juts the tiny groups that occur from time to time are not desired.

> set.seed(1); require(scales)
> mybreaks <- function(x, n, r=0) {
+   unique(round(quantile(x, seq(0, 1, length=n+1)), r))
+ }
> x <- runif(n = 100)
> pretty_breaks(n = 5)(x)
[1] 0.0 0.2 0.4 0.6 0.8 1.0
> mybreaks(x = x, n = 5)
[1] 0 1

You could use the quantile() function as a relatively easy way to get similar numbers of observations in each of your groups.

For example, here's a function that takes a vector of values x , a desired number of groups n , and a desired rounding off point r for the breaks, and gives you suggested cut points.

mybreaks <- function(x, n, r=0) {
  unique(round(quantile(x, seq(0, 1, length=n+1)), r))
}

cut_breaks  <- mybreaks(val, 5)
res <- cut(val, cut_breaks, include.lowest=TRUE)
table(res)

 [2,3]  (3,4] (4,11] 
     8     16      5 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM