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

should remove_variables be exported? #333

Closed
n-kall opened this issue Jan 10, 2024 · 4 comments
Closed

should remove_variables be exported? #333

n-kall opened this issue Jan 10, 2024 · 4 comments
Labels
feature New feature or request

Comments

@n-kall
Copy link
Collaborator

n-kall commented Jan 10, 2024

I was recently looking for a way to remove unwanted variables from a draws object, and I found that there is a remove_variables function (mentioned by in #331) but it does not seem to be exported. Is this intentional?

Here, the generic is not exported, but the individual methods appear to be:
https://github.com/stan-dev/posterior/blob/1210f4a42eeb6b942572097d60a8cc2fb8540adf/R/remove_variables.R#L1C1-L14C2

Perhaps another option would be to add an exclude parameter in subset_draws (e.g. subset_draws(example_draws(), variable = "theta", exclude = TRUE) would remove all theta variables)

@paul-buerkner
Copy link
Collaborator

I think we can export the generic and thus the methods actually. However, then we would have to add all kinds of validity checks, which we currently don't have in place there. I like the idea of adding an exclude option to subset_draws.
This could internally call remove_variables then.

BTW: the fact that the remove_variables methods are exported just ensures that they are registered as S3 methods to the generic. They won't actually be exported this way.

@paul-buerkner paul-buerkner added the feature New feature or request label Jan 10, 2024
@n-kall
Copy link
Collaborator Author

n-kall commented Jan 11, 2024

I think an exclude option for subset_draws could be implemented with setdiff like this. If this seems ok, I can make a PR

subset_draws.draws_array <- function(x, variable = NULL, iteration = NULL,
                                     chain = NULL, draw = NULL, regex = FALSE,
                                     unique = TRUE, exclude = FALSE, ...) {
  if (all_null(variable, iteration, chain, draw)) {
    return(x)
  }

  x <- repair_draws(x)
  variable <- check_existing_variables(variable, x, regex = regex)
  iteration <- check_iteration_ids(iteration, x, unique = unique)
  chain <- check_chain_ids(chain, x, unique = unique)
  draw <- check_draw_ids(draw, x, unique = unique)

 ########## code for exclude option
  if (exclude) {
    if (!is.null(variable)) {
      variable <- setdiff(variables(x), variable)
    }
    if (!is.null(iteration)) {
      iteration <- setdiff(iteration_ids(x), iteration)
    }
    if (!is.null(chain)) {
      chain <- setdiff(chain_ids(x), chain)
    }
    if (!is.null(draw)) {
      draw <- setdiff(draw_ids(x), draw)
    }
  }
 #############

  x <- prepare_subsetting(x, iteration, chain, draw)
  if (!is.null(draw)) {
    iteration <- draw
  }
  x <- .subset_draws(x, iteration, chain, variable, reserved = TRUE)
  if (!is.null(chain) || !is.null(iteration)) {
    x <- repair_draws(x, order = FALSE)
  }
  x
}

@paul-buerkner
Copy link
Collaborator

Yes, that looks sensible to me. Thank you!

@n-kall n-kall mentioned this issue Jan 11, 2024
@n-kall
Copy link
Collaborator Author

n-kall commented Jan 16, 2024

Closed by #336

@n-kall n-kall closed this as completed Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants