Skip to content

Commit 64ba936

Browse files
committed
use rstan's loo method with moment matching
1 parent 139bbc2 commit 64ba936

1 file changed

Lines changed: 15 additions & 32 deletions

File tree

vignettes/loo2-moment-matching.Rmd

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Here is the Stan code for fitting the Poisson regression model, which
5757
we will use for modelling the number of roaches.
5858

5959
```{r, stancode}
60-
stancode = "
60+
stancode <- "
6161
data {
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
167167
available in __rstan__ > 2.19.3. Until then use
168168
https://github.com/stan-dev/rstan/pull/743.)
169169

170-
```
170+
```{r loo_moment_match}
171171
loo2 <- loo(fit, moment_match = TRUE)
172172
loo2
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-
196175
After the moment matching, all observations have the diagnostic
197176
Pareto $k$ less than 0.7, meaning that the estimates are now reliable.
198177
The 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

241220
Next, 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",
307287
Using 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+
)
317300
loo3
318301
```
319302

0 commit comments

Comments
 (0)