@@ -57,7 +57,7 @@ Here is the Stan code for fitting the Poisson regression model, which
5757we will use for modelling the number of roaches.
5858
5959``` {r, stancode}
60- stancode = "
60+ stancode <- "
6161data {
6262 int<lower=1> K;
6363 int<lower=1> N;
@@ -167,32 +167,11 @@ default, it operates on all observations whose Pareto $k$ value is larger than
167167available in __ rstan__ > 2.19.3. Until then use
168168https://github.com/stan-dev/rstan/pull/743 .)
169169
170- ```
170+ ``` {r loo_moment_match}
171171loo2 <- loo(fit, moment_match = TRUE)
172172loo2
173173```
174174
175- ```
176- Computed from 4000 by 262 log-likelihood matrix
177-
178- Estimate SE
179- elpd_loo -5478.5 700.0
180- p_loo 258.1 59.5
181- looic 10957.0 1400.0
182- ------
183- Monte Carlo SE of elpd_loo is 0.3.
184-
185- Pareto k diagnostic values:
186- Count Pct. Min. n_eff
187- (-Inf, 0.5] (good) 250 95.4% 144
188- (0.5, 0.7] (ok) 12 4.6% 84
189- (0.7, 1] (bad) 0 0.0% <NA>
190- (1, Inf) (very bad) 0 0.0% <NA>
191-
192- All Pareto k estimates are ok (k < 0.7).
193- See help('pareto-k-diagnostic') for details.
194- ```
195-
196175After the moment matching, all observations have the diagnostic
197176Pareto $k$ less than 0.7, meaning that the estimates are now reliable.
198177The total ` elpd_loo ` estimate also changed from ` -5457.8 ` to ` -5478.5 ` , showing
@@ -239,8 +218,9 @@ functions are
239218 unconstrained posterior draws passed via ` upars ` .
240219
241220Next, we show how the helper functions look like for RStan objects, and show an
242- example of using ` loo_moment_match() ` directly. For __ rstan__ objects, the
243- functions look like this
221+ example of using ` loo_moment_match() ` directly. For stanfit objects from
222+ __ rstan__ objects, the functions look like this:
223+
244224``` {r, stanfitfuns}
245225# create a named list of draws for use with rstan methods
246226.rstan_relist <- function(x, skeleton) {
@@ -307,13 +287,16 @@ log_lik_i_upars_stanfit <- function(x, upars, i, parameter_name = "log_lik",
307287Using these function, we can call ` loo_moment_match() ` to update the existing
308288` loo ` object.
309289
310- ``` {r, message=FALSE}
311- loo3 <- loo::loo_moment_match.default(x = fit, loo = loo1,
312- post_draws = post_draws_stanfit,
313- log_lik_i = log_lik_i_stanfit,
314- unconstrain_pars = unconstrain_pars_stanfit,
315- log_prob_upars = log_prob_upars_stanfit,
316- log_lik_i_upars = log_lik_i_upars_stanfit)
290+ ``` {r loo_moment_match.default, message=FALSE}
291+ loo3 <- loo::loo_moment_match.default(
292+ x = fit,
293+ loo = loo1,
294+ post_draws = post_draws_stanfit,
295+ log_lik_i = log_lik_i_stanfit,
296+ unconstrain_pars = unconstrain_pars_stanfit,
297+ log_prob_upars = log_prob_upars_stanfit,
298+ log_lik_i_upars = log_lik_i_upars_stanfit
299+ )
317300loo3
318301```
319302
0 commit comments