Description Usage Arguments Details Value References See Also Examples
A loo
method that is customized for stanfit objects.
The loo
method for stanfit objects —a wrapper around the
loo.array
(loo package)— computes PSISLOO CV,
approximate leaveoneout crossvalidation using Pareto smoothed importance
sampling (Vehtari, Gelman, and Gabry, 2017a,2017b).
1 2 3 4 
x 
An object of S4 class 
pars 
Name of transformed parameter or generated quantity in
the Stan program corresponding to the pointwise loglikelihood. If not
specified the default behavior is to look for 
cores 
Number of cores to use for parallelization. The default is 1 unless

save_psis 
Should the intermediate results from 
... 
Ignored. 
Stan does not automatically compute and store the loglikelihood. It is up to the user to incorporate it into the Stan program if it is to be extracted after fitting the model. In a Stan program, the pointwise log likelihood can be coded as a vector in the transformed parameters block (and then summed up in the model block) or it can be coded entirely in the generated quantities block. We recommend using the generated quantities block so that the computations are carried out only once per iteration rather than once per HMC leapfrog step.
For example, the following is the generated quantities
block for
computing and saving the loglikelihood for a linear regression model with
N
data points, outcome y
, predictor matrix X
(including
column of 1s for intercept), coefficients beta
,
and standard deviation sigma
:
vector[N] log_lik;
for (n in 1:N) log_lik[n] = normal_lpdf(y[n]  X[n, ] * beta, sigma);
A list with class c("psis_loo", "loo")
, as detailed in the
loo.array
documentation.
Vehtari, A., Gelman, A., and Gabry, J. (2017a).
Practical Bayesian model evaluation using leaveoneout crossvalidation and WAIC.
Statistics and Computing. 27(5), 14131432.
doi:10.1007/s1122201696964
.
http://arxiv.org/abs/1507.04544,
http://link.springer.com/article/10.1007%2Fs1122201696964
Vehtari, A., Gelman, A., and Gabry, J. (2017b). Pareto smoothed importance sampling. arXiv preprint: http://arxiv.org/abs/1507.02646/
Yao, Y., Vehtari, A., Simpson, D., and Gelman, A. (2018).
Using stacking to average Bayesian predictive distributions.
Bayesian Analysis, advance publication, doi:10.1214/17BA1091
.
https://projecteuclid.org/euclid.ba/1516093227.
The loo package documentation, including the vignettes for many examples (http://mcstan.org/loo).
loo_model_weights
for model averaging/weighting via
stacking or pseudoBMA weighting.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52  ## Not run:
# Generate a dataset from N(0,1)
N < 100
y < rnorm(N, 0, 1)
# Suppose we have three models for y:
# 1) y ~ N(1, sigma)
# 2) y ~ N(0.5, sigma)
# 3) y ~ N(0.6,sigma)
#
stan_code < "
data {
int N;
vector[N] y;
real mu_fixed;
}
parameters {
real<lower=0> sigma;
}
model {
sigma ~ exponential(1);
y ~ normal(mu_fixed, sigma);
}
generated quantities {
vector[N] log_lik;
for (n in 1:N) log_lik[n] = normal_lpdf(y[n] mu_fixed, sigma);
}"
mod < stan_model(model_code = stan_code)
fit1 < sampling(mod, data=list(N=N, y=y, mu_fixed=1))
fit2 < sampling(mod, data=list(N=N, y=y, mu_fixed=0.5))
fit3 < sampling(mod, data=list(N=N, y=y, mu_fixed=0.6))
# use the loo method for stanfit objects
loo1 < loo(fit1, pars = "log_lik")
print(loo1)
# which is equivalent to
LL < as.array(fit1, pars = "log_lik")
r_eff < loo::relative_eff(exp(LL))
loo1b < loo::loo.array(LL, r_eff = r_eff)
print(loo1b)
# compute loo for the other models
loo2 < loo(fit2)
loo3 < loo(fit3)
# stacking weights
wts < loo::loo_model_weights(list(loo1, loo2, loo3), method = "stacking")
print(wts)
## End(Not run)

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.