-
Notifications
You must be signed in to change notification settings - Fork 607
/
Copy pathWAIC.Rd
98 lines (85 loc) · 3.55 KB
/
WAIC.Rd
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
\name{WAIC}
\alias{WAIC}\alias{DIC}\alias{PSIS}\alias{LOO}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{Information Criteria and Pareto-Smoothed Importance Sampling Cross-Validation}
\description{
Computes WAIC, DIC, and PSIS cross validation for \code{quap}, \code{map2stan}, \code{ulam} model fits. In addition, WAIC and PSIS can be calculated for \code{stan} model fits (see details).
}
\usage{
WAIC( object , n=1000 , refresh=0.1 , pointwise=FALSE , ... )
PSIS( object , n=1000 , refresh=0.1 , pointwise=FALSE , ... )
DIC( object , ... )
}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{object}{Object of class \code{map} or \code{map2stan}}
\item{n}{Number of samples to use in computing WAIC. Set to \code{n=0} to use all samples in \code{map2stan} fit}
\item{refresh}{Refresh interval for progress display. Set to \code{refresh=0} to suppress display.}
\item{pointwise}{If \code{TRUE}, return a vector of WAIC values for each observation. Useful for computing standard errors.}
\item{...}{Other parameters to pass to specific methods}
}
\details{
These functions use the samples and model definition to compute the Widely Applicable Information Criterion (WAIC), Deviance Information Criterion (DIC), or Pareto-smoothed importance-sampling cross-validation estimate (PSIS).
WAIC is an estimate of out-of-sample relative K-L divergence (KLD), and it is defined as:
\deqn{WAIC = -2(lppd - pWAIC)}
Components \code{lppd} (log pointwise predictive density) and \code{pWAIC} (the effective number of parameters) are reported as attributes. See Gelman et al 2013 for definitions and formulas. This function uses the variance definition for \code{pWAIC}.
PSIS is another estimate of out-of-sample relative K-L divergence. It is computed by the \code{loo} package. See Vehtari et al 2015 for definitions and computation.
In practice, WAIC and PSIS are extremely similar estimates of KLD.
Both WAIC and PSIS have methods for \code{stanfit} models, provided the posterior contains a log-likelihood matrix (samples on rows, observations on columns) named \code{log_lik}. See example.
}
\value{
}
\references{
Watanabe, S. 2010. Asymptotic equivalence of Bayes cross validation and Widely Applicable Information Criterion in singular learning theory. Journal of Machine Learning Research 11:3571-3594.
Gelman, A., J. Hwang, and A. Vehtari. 2013. Understanding predictive information criteria for Bayesian models.
Vehtari, A., A. Gelman, and J. Gabry. 2015. Efficient implementation of leave-one-out cross-validation and WAIC for evaluating fitted Bayesian models.
}
\author{Richard McElreath}
\seealso{\code{\link{quap}}, \code{\link{ulam}}, \code{\link{link}}, \code{\link{loo}}}
\examples{
\dontrun{
library(rethinking)
data(chimpanzees)
d <- chimpanzees
dat <- list(
y = d$pulled_left,
prosoc = d$prosoc_left,
condition = d$condition,
N = nrow(d)
)
m1s_code <- '
data{
int<lower=1> N;
int y[N];
int prosoc[N];
}
parameters{
real a;
real bP;
}
model{
vector[N] p;
bP ~ normal( 0 , 1 );
a ~ normal( 0 , 10 );
for ( i in 1:N ) {
p[i] = a + bP * prosoc[i];
}
y ~ binomial_logit( 1 , p );
}
generated quantities{
vector[N] p;
vector[N] log_lik;
for ( i in 1:N ) {
p[i] = a + bP * prosoc[i];
log_lik[i] = binomial_logit_lpmf( y[i] | 1 , p[i] );
}
}
'
m1s <- stan( model_code=m1s_code , data=dat , chains=2 , iter=2000 )
WAIC(m1s)
PSIS(m1s)
}
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ }