Skip to content

Exclude parameters (eg: "lp__") #161

Closed
@aornugent

Description

@aornugent

Perhaps I've missed something, but I can't find a simple way to exclude a parameter. For something like mcmc_parcoord I often want to remove "lp__" and display all other parameters.

I thought I could use negative lookaheads, but R needs an extra flag for Perl compatible expressions. Can this be passed through to base::grep?

 grepl("^(?!lp).*$", c("par_1", "lp__"), perl = F)
> Error in grepl("^(?!lp).*$", c("par_1", "lp__"), perl = F) : 
> invalid regular expression '^(?!lp).*$', reason 'Invalid regexp'

grepl("^(?!lp).*$", c("par_1", "lp__"), perl = T)
> [1]  TRUE FALSE

Activity

jtimonen

jtimonen commented on Oct 16, 2018

@jtimonen

I am currently using something like this:

exclude <- c("lp__")
par_names <- setdiff(dimnames(x)[[3]], exclude)
mcmc_parcoord(x, pars = par_names)

Here x is the three-dimensional array of posterior samples, and exclude could have more than one excluded parameter, for example

exclude <- c("lp__", "par1", "par2", "par3")
jgabry

jgabry commented on Apr 11, 2019

@jgabry
Member

@aornugent I noticed this hadn't been addressed yet, sorry it's taken so long. Thanks @jtimonen for responding.

There hasn't been a release in a while but we're getting closer to the next release, so maybe we can do something about it before then. I agree that it would be nice to have an easier way of excluding parameters.

If you have a stanfit object from RStan then the simplest way to exclude just lp__ (or any parameter) would be to use

posterior <- as.matrix(stanfit, pars = "lp__", include=FALSE) # include passed to rstan::extract
mcmc_parcoord(posterior)

but that only works for stanfit objects and we don't have a way to do this within bayesplot itself. Here are three options that come to mind for how we could add it to bayesplot, although maybe there's a better solution I haven't thought of:

  • Allow additional arguments to be passed to grep (which is called internally for matching regexp_pars) so that @aornugent's approach would work. Right now ... isn't used for anything in the bayesplot functions other than to enforce that after ... all arguments must be named, so we could use it to allow passing arguments to grep. Alternatively we could add additional named arguments to the plotting functions that correspond to the arguments to grep.

  • Add arguments pars_drop and regex_pars_drop to allow users to specify parameters to drop before plotting. So for example, mcmc_parcoord(x, pars_drop = "lp__") would plot all parameters in x except lp__.

  • Follow what rstan::extract does and add a TRUE/FALSE argument include which indicates whether the parameters named in pars and regex_pars should be included or excluded.

@tjmahr I don't love these options but I'm not sure if there's a better way. Any preference or other ideas?

tjmahr

tjmahr commented on Apr 11, 2019

@tjmahr
Collaborator

I don't mind pars_drop and regex_pars_drop.

My ideal design would be something like vars()/tidyselect, so that vars(x, y) would select x and y and vars(-x, -matches("beta")) would drop x and anything matching the regex "beta".

jgabry

jgabry commented on Apr 12, 2019

@jgabry
Member

@tjmahr good idea. Let's go with tidyselect.

jgabry

jgabry commented on Apr 15, 2019

@jgabry
Member

@tjmahr I think I have this working on the feature-tidyselect branch if you want to try it out. If you install from that branch and do help("tidy-params", "bayesplot") there are a bunch of examples you can run. I'll make a PR if this looks like the right approach to you, but you know the tidy stuff better than I do so maybe I went about this the wrong way.

jgabry

jgabry commented on Apr 15, 2019

@jgabry
Member

I opened a separate issue for adding 'tidy' parameter selection, which is broader than just excluding parameters: #183

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @tjmahr@jgabry@jtimonen@aornugent

      Issue actions

        Exclude parameters (eg: "lp__") · Issue #161 · stan-dev/bayesplot