I’m a fan of interactive visualizations, if you couldn’t tell already by my tendency to explain things with rglwidgets
. I decided that since I use R pretty frequently it’s about time I picked up Shiny.
Previously, I would have used tkinter in Python like I did with my Character Creation project in my second year of undergrad. Tkinter helped me make a GUI to distance the tool I was trying to make from the script itself, because obviously I don’t want the things I make to only be useful to people who know how to type up the code themselves. It was rough around the edges for sure, but the real limitation of a tool like that was how difficult it was to share. I think people were much more likely to watch the gif I put in the projects README than they would ever be to actually download and run it. Now, armed with this blog, the most shareable projects are those that can be embedded in a webpage, like Shiny apps.
I got inspired for this little project while I was reading An Introduction to Generalized Linear Models by Annette J. Dobson after seeing it cited in the R documentation for glm
. I’m still planning my next few “Generalizing Linear Models” posts and wanted to see if I could find any tips for how to structure them in this book (basically, I want to see how much of it can be explained without matrix algebra… it’s not looking good, folks). In Section 3.2, Dobson describes the exponential family of distributions as a those that can be written in the form:
and gives a small table for the a, b, c, and d functions for a few distributions in the family.
I wrote an app that could make distributions based on Dobson’s description, but was limited to the three distributions whose components she described. I found a much more complete table on the Wikipedia page for the exponential family with the reformulation:
\[f(x;\theta)=h(x)e^{\eta(\theta)T(x)-A(\theta)}\]and rewrote the app to use this formulation. Here it is.
Now, you can take a stroll through that Wikipedia table I linked above and fill in the functions from it. It should work for every distibution with a scalar natural parameter. You can also assign your distribution’s parameters as vectors to show multiple distributions from the same subfamily. By unchecking the “New plot?” box, you can plot distributions from multiple subfamilies for comparison.
I’ve linked the code at the bottom of this page if you want to see how it works or modify it (or run it locally, which should be faster).
The way I chose to make this work was essentially to evaluate the entries in each text box as if they were R code, which makes it pretty fragile and, counterintuitively, usable only by people who know some R syntax. Despite that, I think it was a good introduction to using Shiny.
Anyway, I intended to make this a very brief post, but if you have any questions feel free to comment on the gist over on github.
Thanks for reading!
Blake