-
Notifications
You must be signed in to change notification settings - Fork 10
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
Expected behaviour for grouped data + sample_n_keys #104
Comments
I think facet_strata() might do it to plot the result. But you’d like sample to do similarly, I think
…Sent from my iPhone
On 1 Nov 2020, at 9:25 pm, Emi Tanaka ***@***.***> wrote:
I was hoping to sample 1 key per group as below but the output seems to be a bit random where I get some correct but another sampling gets 2 samples instead of 1 and so on.
library(tsibble)
library(brolgar)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
set.seed(1)
out <- ChickWeight %>%
as_tsibble(key = Chick, index = Time) %>%
group_by(Diet) %>% # 4 diets
sample_n_keys(1) # expecting 1 chick per diet
# shows as 50 chicks - is it a tsibble thing?
out
#> # A tsibble: 48 x 4 [1]
#> # Key: Chick [50]
#> # Groups: Diet [4]
#> weight Time Chick Diet
#> <dbl> <dbl> <ord> <fct>
#> 1 41 0 13 1
#> 2 48 2 13 1
#> 3 53 4 13 1
#> 4 60 6 13 1
#> 5 65 8 13 1
#> 6 67 10 13 1
#> 7 71 12 13 1
#> 8 70 14 13 1
#> 9 71 16 13 1
#> 10 81 18 13 1
#> # … with 38 more rows
# actual number of chicks sampled
# the number sampled seems random. Sometimes it is correct, some times like below.
out %>%
distinct(Chick, Diet)
#> # A tibble: 7 x 2
#> # Groups: Diet [4]
#> Chick Diet
#> <ord> <fct>
#> 1 13 1
#> 2 30 2
#> 3 22 2
#> 4 37 3
#> 5 36 3
#> 6 45 4
#> 7 43 4
Created on 2020-11-01 by the reprex package (v0.3.0.9001)
Session info
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Oh, I think it is simply that brolgar assumes unique keys. Similarly, tsibble is also assuming unique keys. You need to use tibble/dplyr: group_by(your_character_variable) %>% sample_n() Think of your variable as a grouping variable rather than an id variable. It would be interesting to think of handling this as an extension of tsibble, brolgar. Its a tsibble with replicates %^) |
Oh, nope, its something not working in sample_n_keys(), eg wages %>% ignores the group_by |
Thanks for posting the issue, @emitanaka ! This seems like a bug, I'll fix this before submitting to CRAN. |
@dicook actually the behaviour is random. If you repeat your command, occasionally it shows some rows. The bug could be related to that it still thinks the number of keys is the same as the big data (not sure) |
I was hoping to sample 1 key per group as below but the output seems to be a bit random where I get some correct but another sampling gets 2 samples instead of 1 and so on.
Created on 2020-11-01 by the reprex package (v0.3.0.9001)
Session info
The text was updated successfully, but these errors were encountered: