@@ -355,7 +355,11 @@ variables. To include the Jacobian adjustment and obtain a maximum a posteriori
355355section of the CmdStan User's Guide for more details.
356356
357357``` {r optimize-map}
358- fit_map <- mod$optimize(data = data_list, jacobian = TRUE, seed = 123)
358+ fit_map <- mod$optimize(
359+ data = data_list,
360+ jacobian = TRUE,
361+ seed = 123
362+ )
359363```
360364
361365### Laplace Approximation
@@ -375,7 +379,13 @@ Here we pass in the `fit_map` object from above as the `mode` argument. If
375379from the normal approximation.
376380
377381``` {r laplace}
378- fit_laplace <- mod$laplace(mode = fit_map, draws = 4000, data = data_list, seed = 123)
382+ fit_laplace <- mod$laplace(
383+ mode = fit_map,
384+ draws = 4000,
385+ data = data_list,
386+ seed = 123,
387+ refresh = 1000
388+ )
379389fit_laplace$summary("theta")
380390mcmc_hist(fit_laplace$draws("theta"), binwidth = 0.025)
381391```
@@ -387,22 +397,32 @@ We can run Stan's experimental variational Bayes algorithm (ADVI) using the
387397method.
388398
389399``` {r variational}
390- fit_vb <- mod$variational(data = data_list, draws = 4000, seed = 123)
400+ fit_vb <- mod$variational(
401+ data = data_list,
402+ draws = 4000,
403+ seed = 123
404+ )
391405fit_vb$summary("theta")
392406```
393407
394408Let's extract the draws, make the same plot we made after MCMC and Laplace
395409approximation, and compare them all. In this simple example the distributions
396410are quite similar, but this will not always be the case.
397411
398- ``` {r plot-compare, message = FALSE, fig.width = 8, fig.cap="Comparing draws from the different algorithms"}
399- bayesplot_grid(
400- mcmc_hist(fit$draws("theta"), binwidth = 0.025),
401- mcmc_hist(fit_laplace$draws("theta"), binwidth = 0.025),
402- mcmc_hist(fit_vb$draws("theta"), binwidth = 0.025),
403- xlim = c(0, 1),
404- subtitles = c("MCMC", "Laplace", "Variational")
405- )
412+ ``` {r plot-compare-vb, message = FALSE}
413+ mcmc_hist(fit_vb$draws("theta"), binwidth = 0.025) +
414+ ggplot2::labs(subtitle = "Approximate posterior from variational") +
415+ ggplot2::xlim(0, 1)
416+ ```
417+ ``` {r plot-compare-laplace, message = FALSE}
418+ mcmc_hist(fit_laplace$draws("theta"), binwidth = 0.025) +
419+ ggplot2::labs(subtitle = "Approximate posterior from Laplace") +
420+ ggplot2::xlim(0, 1)
421+ ```
422+ ``` {r plot-compare-mcmc, message = FALSE}
423+ mcmc_hist(fit$draws("theta"), binwidth = 0.025) +
424+ ggplot2::labs(subtitle = "Posterior from MCMC") +
425+ ggplot2::xlim(0, 1)
406426```
407427
408428For more details on the ` $optimize() ` , ` $laplace() ` and ` $variational() `
0 commit comments