Working with Shinystan-Output in ggplot2

244 views
Skip to first unread message

Tobias Wolfram

unread,
Sep 7, 2016, 4:58:06 AM9/7/16
to Stan users mailing list
Hi guys,

apologies if this is the wrong place to ask, but I built a hierarchical regression model in Stan using RStan and of course I putmy covariates in vectors, therefore they are named like beta[1], beta[2], etc. I'd like to change this and use the awesome parameter plots found in Shinystan. Fortunately Shinystan lets me download them as a ggplot2-object. Unfortunately I am quite unexperienced using ggplot2 and I have no idea how I could rename my parameters there. I already tried to change the rownames in the $data-dataframe of the ggplot2-object, but this does not produce any result. Do you have any ideas? 

Thanks
Tobias

Jonah Gabry

unread,
Sep 7, 2016, 10:26:29 AM9/7/16
to Stan users mailing list
Hi Tobias, 

This is definitely the right place to ask. It's not super easy to modify the names once the ggplot object is created (much easier to customize other things like the title, axes, background, etc), but it might be possible. However, before spending too much time fiddling with the ggplot object, the first thing I'd try is using the names method for stanfit objects to change the parameter names before using shinystan. For example: 

# using demo model as an example
> fit
<- stan_demo("eight_schools")
# maybe make a copy so it's safer to then mess with fit
> fit_copy <- fit  

> print(names(fit))
 [1] "mu"       "theta[1]" "theta[2]" "theta[3]" "theta[4]"
 [6] "theta[5]" "theta[6]" "theta[7]" "theta[8]" "tau"     
[11] "lp__"  

# change a few of the names
> names(fit)[2:4] <- c("monkey", "iguana", "tiger")
> print(names(fit))
 [1] "mu"       "monkey"   "iguana"   "tiger"    "theta[4]"
 [6] "theta[5]" "theta[6]" "theta[7]" "theta[8]" "tau"     
[11] "lp__"  

If you then launch shinystan using the object fit it should use the new names in the plots. Does that work in your case? 

Jonah


Tobias Wolfram

unread,
Sep 10, 2016, 7:00:40 AM9/10/16
to stan-...@googlegroups.com
Thank you for your answer! This indeed changes the names displayed in Shiny but for some reason it also causes Shiny to not find the parameter names any more in the "Select or enter parameter names"-space for the parameters plot so I'm unable to choose the parameter set which ought to be plotted. 



--
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/9GbOKCxJeFw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+unsubscribe@googlegroups.com.
To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonah Sol Gabry

unread,
Sep 12, 2016, 12:41:53 PM9/12/16
to stan-...@googlegroups.com
Ok I see what you mean. Here's another option that might work. If you have the ggplot object created by shinystan (it will be called something like shinystan_multiparam_gg when you load it) then you could just modify the axis labels using ggplot2's scale_y_continuous function: 

library("ggplot2")
p <- shinystan_multiparam_gg
p + scale_y_continuous(labels = my_labels)

where my_labels is a character vector of parameter names of the appropriate length. 

Tobias Wolfram

unread,
Sep 14, 2016, 4:27:57 PM9/14/16
to stan-...@googlegroups.com
Great, I was looking for something precisely like that. Thank you! 
Reply all
Reply to author
Forward
0 new messages