Feature Request: Keyword to Hide Output when Printing

261 views
Skip to first unread message

John Hall

unread,
Feb 18, 2015, 3:07:52 PM2/18/15
to stan-...@googlegroups.com
I often find that when I am using transformed parameters that are very large, I do not always wish to see them in the plot or print calls in rstan. Plot will usually truncate to only show the first few variables, but if you print a fit that includes a vector transformed parameter with 1000+ entries, you may not even be able to scroll back up to the original parameters. Given the various ways of using Stan, I think a keyword allowing the user to specify the parameters (or transformed parameters or generated quantities) to hide when using plot or print would give the most flexibility. The use of it would be something like

     hide vector[N] Y;

or

     nodisplay vector[N] Y;

or

     someotherkeyword vector[N] Y;

It might also be helpful if there is an ability to toggle this on and off, like with the verbose option when calling the stan function.

Juan Sebastián Casallas

unread,
Feb 18, 2015, 3:37:06 PM2/18/15
to stan-...@googlegroups.com
Hi John,

If you're using R, you can accomplish that using the "pars" argument in plot, print, and other methods.

e.g.

print(mdl, pars = c("a", "b")) # This only prints the summary of "a" and "b"

or

# This prints the summary of all parameters except "a" and "b"
print(mdl, pars = m@sim$pars_oi[!m@sim$pars_oi %in% c("y_rep")])

You can also specify the parameters you want using pars in the sampling and stan functions, which can be useful when you are generating lots of quantities that may take up too much memory like saving the log_lik, or replications per iteration

Hope that helps,
Juan Sebastian

--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Juan Sebastián Casallas

unread,
Feb 18, 2015, 3:37:50 PM2/18/15
to stan-...@googlegroups.com
Sorry the second example was supposed to be


# This prints the summary of all parameters except "a" and "b"
print(mdl, pars = m@sim$pars_oi[!m@sim$pars_oi %in% c("a", "b")])

John Hall

unread,
Feb 18, 2015, 4:13:50 PM2/18/15
to stan-...@googlegroups.com
Not sure I grasp the significance of where you get the m@sim in
m@sim$pars_oi
but thanks for the tips on that. I think I had been vaguely familiar with the pars=c("a", "b") version, but I hadn't seen anything like the other one. That one is much closer to my needs.

You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/QBo9UptkL0U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.

Bob Carpenter

unread,
Feb 18, 2015, 4:27:13 PM2/18/15
to stan-...@googlegroups.com
I believe the term of art is "nuisance".

Here are three ways to get rid of them.

Let's suppose you have transformed parameter alpha to
save and beta not to save.

transformed parameters {
real alpha;
real beta;
... define alpha and beta ...
...
}

0. As Juan Sebastián Casallas pointed out, you can
suppress the output from R.

1. If you don't need beta to compute alpha, it makes
most sense to make it a local variable in the model.

transformed parameters {
real alpha;
... define alpha ...
...
model {
real beta;
... define beta ...
...
}

The local variables in a model (including everything declared
at the top, are not printed.

2. The more general approach is to define beta as a local
within the transformed parameters block.

transformed parameters {
real alpha;
{
real beta; // local variable, won't be printed
... define alpha and beta
}
...
}

But that won't work if you actually need beta in the model
block. You need 1 for that. But 1 won't always work,
because sometimes you need beta to define alpha efficiently.

- Bob

John Hall

unread,
Feb 18, 2015, 6:33:38 PM2/18/15
to stan-...@googlegroups.com
Thanks, that's good advice as well.

You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/QBo9UptkL0U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.

Juan Sebastián Casallas

unread,
Feb 18, 2015, 7:20:49 PM2/18/15
to stan-...@googlegroups.com

Sorry, I miswrote that, it was supposed to be mdl@sim$pars_oi so again,

# This prints the summary of all parameters except "a" and "b"

print(mdl, pars = mdl@sim$pars_oi[!mdl@sim$pars_oi %in% c("a", "b")])

Sorry for the confusion,
JS

Jonah

unread,
Mar 4, 2015, 10:25:56 AM3/4/15
to stan-...@googlegroups.com
For the R functions, maybe a wrapper for print would be cool where the user could supply with_pars or without_pars (not both I guess, as that would be confusing) instead of just pars to make it easier to exclude a few parameters. A quick, crude, way of doing it would be 


stan_print <- function(x, with_pars, without_pars, ...) {
  wp <- !missing(with_pars)
  wop <- !missing(without_pars)
  
  if (wp & wop) stop("Specify only one of 'with_pars' and 'without_pars'")
  if (!(wp | wop)) return(print(x, ...))
  if (wp) return(print(x, pars = with_pars, ...))
  else print(x, pars = x@sim$pars_oi[!x@sim$pars_oi %in% without_pars], ...)
}

So then stan_print(stanfit) does the default print(stanfit), stan_print(stanfit, with_pars = c("a", "b")) does print(stanfit, pars = c("a", "b")) and stan_print(stanfit, without_pars = c("a","b")) does what Juan suggested for removing parameters from the output. 

Juan Sebastián Casallas

unread,
Mar 4, 2015, 11:28:06 AM3/4/15
to stan-...@googlegroups.com
My wish would actually be to be able to do something like what dplyr does:

dplyr::select(df, -a, -b) # selects all columns from df except 'a' and 'b'

So a similar idea in stan:

print(mdl, pars = -c("a", "b")) # should print everything except "a", "b"

but yeah that involves non-standard evaluation which can be tricky (I'd try it if I had more time right now)... if not Jonah's idea seems straightforward to implement!
JS

Ben Goodrich

unread,
Mar 4, 2015, 1:36:23 PM3/4/15
to stan-...@googlegroups.com
I think I am going to add an include = TRUE argument to the print.stanfit(). If you set include = FALSE, pars will be interpreted as the parameters to exclude from printing.

Ben
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

Juan Sebastián Casallas

unread,
Mar 4, 2015, 11:01:57 PM3/4/15
to stan-...@googlegroups.com
That's great! Do you think this approach could also be extended to rstan::stan and/or rstan::sampling? Sometimes its hard to make the "intermediate" variables local within the stan code and you can end up with huge stanfit objects.

Ben Goodrich

unread,
Mar 5, 2015, 12:18:35 AM3/5/15
to stan-...@googlegroups.com
That shouldn't be hard. You just have to create another local block within the transformed parameters block.

transformed parameters {
  real keep
;
 
{
    vector exclude
;
   
...
    keep
<- f(exclude);
 
}
}

Bob Carpenter

unread,
Mar 5, 2015, 12:49:14 AM3/5/15
to stan-...@googlegroups.com

> On Mar 5, 2015, at 4:18 PM, Ben Goodrich <goodri...@gmail.com> wrote:
>
> That shouldn't be hard. You just have to create another local block within the transformed parameters block.
>
> transformed parameters {
> real keep;
> {
> vector exclude;
> ...
> keep <- f(exclude);
> }
> }

That will work for some cases, but not the ones where you need
the exclude variables in the model block AND in the transformed
parameters block.

- Bob

Reply all
Reply to author
Forward
0 new messages