# An Introduction to Greta But `greta` lets users write TensorFlow-based Bayesian models directly in R! What could be more charming? `greta` removes the barrier of learning an intermediate modeling language while still promising to deliver high-performance MCMC models that run anywhere TensorFlow can go.

In this post, I’ll introduce you to `greta` with a simple model used by Richard McElreath in section 8.3 of his iconoclastic book: Statistical Rethinking: A Bayesian Course with Examples in R and Stan. This model seeks to explain the log of a country’s GDP based on a measure of terrain ruggedness while controlling for whether or not the country is in Africa. I am going to use it just to illustrate MCMC sampling with `greta`. The extended example in McElreath’s book, however, is a meditation on the subtleties of modeling interactions, and is well worth studying.

First, we load the required packages and fetch the data. `DiagrammeR` is for plotting the TensorFlow flow diagram of the model, and `bayesplot` is used to plot trace diagrams of the Markov chains. The rugged data set which provides 52 variables for 234 is fairly interesting, but we will use a trimmed-down data set with only 170 counties and three variables.

In this section of code, we set up the TensorFlow data structures. The first step is to move the data into `greta` arrays. These data structures behave similarly to R arrays in that they can be manipulated with functions. However, `greta` doesn’t immediately calculate values for new arrays. It works out the size and shape of the result and creates a place-holder data structure.

In this section, we set up the Bayesian model. All parameters need prior probability distributions. Note that the parameters `a``bR``bA``bAR``sigma`, and `mu` are all new `greta` arrays that don’t contain any data. `a` is 1 x 1 array and `mu` is a 170 x 1 array with one slot for each observation.

The `distribution()` function sets up the likelihood function for the model.

The `model()` function does all of the work. It fits the model and produces a fairly complicated object organized as three lists that contain, respectively, the R6 class, TensorFlow structures, and the various `greta` data arrays.

Plotting `mod` produces the TensorFlow flow diagram that shows the structure of the underlying TensorFlow model, which is simple for this model and easily interpretable.

Next, we use the `greta` function `mcmc()` to sample from the posterior distributions defined in the model.

Now that we have the samples of the posterior distributions of the parameters in the model, it is straightforward to examine them. Here, we plot the posterior distribution of the interaction term.

Finally, we examine the trace plots for the MCMC samples using the `greta` function `mcmc_trace()`. The plots for each parameter appear to be stationary (flat, i.e., centered on a constant value) and well-mixed (there is no obvious correlation between points). `mcmc_intervals()` plots the uncertainty intervals for each parameter computed from posterior draws with all chains merged.

So there it is - a Bayesian model using Hamiltonian Monte Carlo sampling built in R and evaluated by TensorFlow.

For an expert discussion of the model, have a look at McElreath’s book, described at the link above. For more on `greta`, see the package documentation. And please, do take the time to read about `greta`’s namesake: Greta Hermann, a remarkable woman - mathematician, philosopher, educator, social activist, and theoretical physicist who found the error in John von Neuman’s “proof” of the “No hidden variables theorem” of Quantum Mechanics.

0