stan_scat within a for loop

53 views
Skip to first unread message

Kevin Van Horn

unread,
Nov 21, 2015, 1:32:30 PM11/21/15
to Stan users mailing list
Here's a tip for anyone using stan_scat. In attempting to run stan_scat on all pairs of parameters from a list of parameter, I ran into a bizarre and aggravating issue. Although

    stan_scat(fit, par_names[c(i,j)])

displays a posterior scatterplot of parameter par_names[i] versus par_names[j],

    for (i in 1:(n-1))
      for (j in (i+1):n)  {
        stan_scat(fit, par_names[c(i,j)])
        readline()
      }

displays absolutely nothing. Ditto for

    for (i in 1:1) stan_scat(fit, par_names[1:2])

This gave me considerable heartburn until I realized that stan_scat doesn't actually display anything -- it just returns a ggplot object, which you have to print() in order for it to be displayed. The command line automatically calls print() on the value of any expression entered, giving the illusion that stan_scat() itself is displaying the scatterplot when it is called in isolation.

Jonah

unread,
Nov 22, 2015, 2:03:15 PM11/22/15
to Stan users mailing list
Hi Kevin, 

You're right about the behavior, and it might indeed be bizarre, but it's not specific to rstan. It's how R and ggplot2 behave in general. For example:

library(ggplot2)

x
<- rnorm(100)
qplot
(x) # this will display a plot


for (j in 1:10) {
 x
<- rnorm(100)
 qplot
(x) # no plot displayed
 
print(qplot(x)) # plot displayed
}


But this isn't only with ggplot2. For example: 

2 + 2 # the number 4 is printed


for (j in 1:10) {
 
2 + 2 # nothing is printed
 
print(2 + 2) # 4 is printed
}



For the rstan plotting functions that return a single ggplot object this is how they will behave. I suppose we could have them always call print internally, but then this would deviate from the typical behavior you get when using ggplot2. 

Jonah

Bob Carpenter

unread,
Nov 23, 2015, 12:07:30 AM11/23/15
to stan-...@googlegroups.com

> On Nov 22, 2015, at 2:03 PM, Jonah <jga...@gmail.com> wrote:
>
> Hi Kevin,
>
> You're right about the behavior, and it might indeed be bizarre, but it's not specific to rstan. It's how R and ggplot2 behave in general.

Which is why I struggled in exactly the same way as Kevin
when I first tried to run ggplot inside a loop!

I do agree we should follow ggplot2 behavior. A warning in
the Stan function doc, perhaps?

- Bob

Jonah Sol Gabry

unread,
Nov 23, 2015, 12:15:29 AM11/23/15
to stan-...@googlegroups.com
Sure, I can add a note in the doc for the plotting functions. 
--
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/cl6UpoKvciA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.
To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kevin Van Horn

unread,
Nov 23, 2015, 12:46:01 PM11/23/15
to Stan users mailing list
Yeah, I understand that. I just thought that stan_scat worked like plot(): called for its side effect rather than producing a return value.

Jonah Sol Gabry

unread,
Dec 3, 2015, 4:29:08 PM12/3/15
to stan-...@googlegroups.com
To follow up on this, I've added a note to the rstan plotting documentation about the need to explicitly call print to display a plot when it's creating inside a loop or when the ggplot object is assigned to a name. 
Reply all
Reply to author
Forward
0 new messages