Skip to content

Commit ba9e688

Browse files
authored
Merge pull request #1 from Lionel-Re/fix-issues-330
Closes mjskay#330 and partal Fix issues mjskay#332 Comment for our approach: Purpose: In our approach, we modified the gather_draws() function to handle NA values more effectively after gathering the variables. This change ensures that rows with missing values in the .value column are removed, allowing us to work with clean data. Implementation: To achieve this, we used lapply() within the gather_draws() function. This function dynamically applies the filter() operation to each of the gathered variables, ensuring that rows containing NA values are excluded from each variable. The filtering happens automatically for each variable based on its respective values. Specific Code Change: We added the following lines of code to filter out NA values in the .value column after gathering the data: tidied = tidied %>% filter(!is.na(.value)) # Filter out rows where .value is NA This ensures that only rows with valid (non-NA) .value entries are retained for further analysis. Reasoning: By applying this filter dynamically for each gathered variable, our solution became more flexible and could handle datasets where some variables might contain missing data. This approach removes the need for manually dealing with missing values for each variable, ensuring that the resulting data is clean and ready for analysis. Outcome: After applying this change, the data returned by gather_draws() no longer contains rows with missing values in the .value column, making the data ready for the next steps in the analysis process. library(tidybayes) library(dplyr) library(tidyr) library(waldo) my_tidy_draws <-expand_grid(.chain = 1:2, .iteration = 1:5) |> mutate(.draw = tidybayes:::draw_from_chain_and_iteration_(.chain, .iteration)) |> mutate(aa[1] = rnorm(n()), aa[2] = rnorm(n()), ab[1] = rnorm(n())) |> tidy_draws() gather_draws_explicit <- my_tidy_draws |> gather_draws(aa[i], ab[i]) gather_draws_regex <- my_tidy_draws |> gather_draws(a.*[i], regex = T) Result compare(gather_draws_explicit, gather_draws_regex) ✔ No differences
2 parents fb49436 + 1e88616 commit ba9e688

File tree

3 files changed

+112
-105
lines changed

3 files changed

+112
-105
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ importFrom(tidyr,separate)
475475
importFrom(tidyr,spread)
476476
importFrom(tidyr,unite)
477477
importFrom(tidyr,unnest_legacy)
478+
importFrom(tidyr,unnest)
478479
importFrom(tidyselect,all_of)
479480
importFrom(tidyselect,any_of)
480481
importFrom(tidyselect,one_of)

R/gather_draws.R

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,40 @@
1111
#' @importFrom rlang enquos
1212
#' @export
1313
gather_draws = function(
14-
model,
15-
...,
16-
regex = FALSE,
17-
sep = "[, ]",
18-
ndraws = NULL,
19-
seed = NULL,
20-
draw_indices = c(".chain", ".iteration", ".draw"),
21-
n
14+
model,
15+
...,
16+
regex = FALSE,
17+
sep = "[, ]",
18+
ndraws = NULL,
19+
seed = NULL,
20+
draw_indices = c(".chain", ".iteration", ".draw"),
21+
n
2222
) {
2323
ndraws = .Deprecated_argument_alias(ndraws, n)
24-
24+
2525
draws = sample_draws_from_model_(model, ndraws, seed)
26-
26+
2727
draw_indices = intersect(draw_indices, names(draws))
2828
tidysamples = lapply(enquos(...), function(variable_spec) {
29-
gather_variables(
29+
tidied = gather_variables(
3030
spread_draws_(draws, variable_spec, regex = regex, sep = sep, draw_indices = draw_indices),
3131
exclude = c(draw_indices, ".row")
3232
)
33+
34+
# Drop NAs for the specific variable columns after gathering
35+
tidied = tidied %>%
36+
filter(!is.na(.value)) # Filter out rows where .value is NA
37+
38+
return(tidied)
3339
})
34-
40+
3541
#get the groups from all the samples --- when we bind them together,
3642
#the grouping information is not always retained, so we'll have to recreate
3743
#the full set of groups from all the data frames after we bind them
3844
groups_ = tidysamples %>%
3945
lapply(group_vars) %>%
4046
reduce_(union)
41-
47+
4248
bind_rows(tidysamples) %>%
4349
group_by_at(groups_)
44-
}
50+
}

0 commit comments

Comments
 (0)