Visualizing a Bayesian model
1. Visualizing a Bayesian model
So far we've learned how to estimate, customize, and evaluate a Bayesian model. The final step of the analysis pipeline is to present the results of your model. In this chapter, we'll learn how to make predictions using new data and how to create visualizations of the model using ggplot2. With these tools, you'll be ready to complete a fully Bayesian analysis from model creation to presentation.2. Saving model coefficients
We'll start by making a plot of our model predicting a kid's IQ score from their mom's IQ score. To do this, we need to save the values of the estimated parameters. Recall that we can view the parameter estimates by using the tidy() function from the broom package. To save the parameter values, we can first save the tidy summary, and then pull values from the estimate column of the data frame.3. Creating a plot
We can then create a plot using ggplot2 syntax that you are already likely already familiar with. We define our dataset, kidiq, and then specify that we want mom_iq on the x-axis, and the kid's score on the y-axis. We can then add a geom_point layer to create the scatter plot. Finally, we use geom_abline to add a straight line to the plot. In geom_abline, we specify the intercept and slope of the line to be the model parameters that we just saved. And just like that, we have a plot showing our data and the estimated regression line!4. Plotting uncertainty
We can do more than a simple plot though! Because we used a Bayesian estimation, we can use the posterior distributions to also plot the uncertainty of our regression line. Just like when we learned about posterior predictive checks, we can save the posterior draws for our intercept and mom_iq predictor using the spread_draws function from the tidybayes package. This gives us the value of the intercept and slope at each draw from the posterior. Using these draws, we can plot a regression line for each iteration.5. Plotting uncertainty
We start in the same way we did on the previous plot. We define our data, put mom_iq on the x-axis and kid_score on the y-axis, and then add a geom_point layer to create the scatter plot.6. Plotting uncertainty
We can then plot the lines for each iteration using geom_abline, just like in the last plot. The difference now is that we define a new dataset, `draws`, which we just created and contains the values of the parameters at each iteration. We then specify that the intercept should be the "(Intercept)" column, and the slope should be the mom_iq column. Finally, because there are 4,000 lines being drawn, we can make them small and add transparency so we can tell where there is more density from our posterior predictions. This results in plot with a line for each iteration, showing the range of plausible regression lines.7. Plotting uncertainty
The final step is to add our mean regression line. This can be done with the same geom_abline code that we used in our first graphic. We define the intercept and slope to be the saved model_intercept and model_slope objects respectively. Now we have a complete graphic showing our data, the estimated regression line, and the uncertainty around our estimated line.8. Let's practice
Now it's your turn! Let make some visualizations of our Spotify model, predicting the popularity of a song from it's age.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.