Plotting a normal distribution in R

Apparently there are some unnecessarily complicated tutorials out there how to draw a normal distribution (or other probability distributions) in R. No, there is no need for a loop; in fact, a single line of code is enough:

curve(dnorm(x, 0, 1), from=-4, to=4)

That’s a normal probability distribution with mean 0 and a standard deviation 1, plotted from -4 to +4.

So it’s also easy to draw normal distributions with different means or different standard deviations. Here’s one without a box around:

curve(dnorm(x, 2, 1), from=1, to=7, bty="n", xlab="")

How about a beta distribution? It’s not more difficult (assuming you know your shape parameters):

curve(dbeta(x, 10, 2), from=0, to=1, bty="n", xlab="")

So from now on, if you need to visualize your priors to get a feel whether they constitute reasonable distributions, remember it’s just one line in R.

Getting started with Bayesian in R

Stan logo

There really is no excuse any more: getting started with Bayesian regression analysis in R is really simple.

Step 1: install rstanarm from CRAN

Step 2: replace lm() with stan_glm() in your code

Sure, you’ll probably want to learn about priors, and invest a little in understanding diagnostics such as those provided by ShinyStan. But rstanarm is really designed to work well out of the box (i.e. with your existing code).

What I really appreciate is that it has useful warnings and error messages, and extensive documentation. Sometimes the documentation shows that quantitative analysis has something to do with mathematics, but even those who skip the Greek letters and formulae will get enough guidance. You’ll get nudges to use your own priors rather than rely on the default priors, but in my experience for most simple applications the default priors work reasonably well. You’ll also get suggestions right on your screen what you can do when there are say divergent transitions.

Once you can handle rstanarm, you’ll find it easy to upgrade to brms, where you can still use your trusted syntax for regression models in base R.

The proper prior

From my modern take on Titterington: The proper prior

I paid an artist for these pictures, including the copyright so that I can now distribute them to the world. The full set is available on Figshare: https://figshare.com/articles/Bayesian_Priors/6432998

Titterington, Dr D. M. 1982. ‘Irreverent Bayes’. Journal of Applied Statistics 9 (1): 16–18. https://doi.org/10.1080/02664768200000003. This is available on Peter M. Lee’s webpage: https://web.archive.org/web/20170313222746/http://www-users.york.ac.uk/~pml1/bayes/irreverent.htm

Artist’s profile on Sketchmob: https://sketchmob.com/user-profile/Nakzumi/

The subjective prior

From my modern take on Titterington: The subjective prior

I paid an artist for these pictures, including the copyright so that I can now distribute them to the world. The full set is available on Figshare: https://figshare.com/articles/Bayesian_Priors/6432998

Titterington, Dr D. M. 1982. ‘Irreverent Bayes’. Journal of Applied Statistics 9 (1): 16–18. https://doi.org/10.1080/02664768200000003. This is available on Peter M. Lee’s webpage: https://web.archive.org/web/20170313222746/http://www-users.york.ac.uk/~pml1/bayes/irreverent.htm

Artist’s profile on Sketchmob: https://sketchmob.com/user-profile/Nakzumi/

brms and mice

The excellent R package brms works well with multiple imputations from the excellent R package mice. Today I ran across “Error: Argument ‘data’ must be coercible to a data.frame.” and I couldn’t find anything on the web. As is usually the case, this was a silly error on my side: I used brm(…) instead of brm_multiple(…).