I have been reading Statistical Rethinking and I want to replicate code 10.23 and code 10.24 in Stan. For the moment, I only care about model m10.6
. So I have:
data{
int<lower=1> N;
int admit[N];
int applications[N];
real male[N];
}
parameters{
real a;
real bm;
}
model{
vector[N] p;
bm ~ normal( 0 , 10 );
a ~ normal( 0 , 10 );
for ( i in 1:N ) {
p[i] = a + bm * male[i];
}
admit ~ binomial_logit( applications , p );
}
generated quantities{
vector[N] p;
real dev;
dev = 0;
for ( i in 1:N ) {
p[i] = a + bm * male[i];
}
dev = dev + (-2)*binomial_logit_lpmf( admit | applications , p );
}
I have extracted this code from rethinking::stancode(m10.6)
. The command I am using is
fit_admit <- stan(file = 'admit.stan', data = d_copy, iter = 5000, chains = 4)
with d_copy
as
d_copy <- list( N = 12,
admit = c(512, 89, 353, 17, 120, 202, 138, 131, 53, 94, 22, 24),
applications = c(825, 108, 560, 25, 325, 593, 417, 375, 191, 393, 373, 341),
male = c(1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0))
Since I want WAIC, I use library(loo)
and I do the following
library(loo)
loo::waic(fit_admit)
and I get this error
Error in UseMethod("waic") :
no applicable method for 'waic' applied to an object of class "stanfit"
then I realize that I need loo::extract_log_lik()
, according to this, p.6. And this is my problem, it should be easy, but it is not. The document gives an example:
For example, the following is the generated quantities block for computing and saving the
log-likelihood for a linear regression model with N data points, outcome y, predictor matrix X,
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);
but I cannot translate that to my model m10.6
(because I don’t understand it). The examples that are shown here should help me, but…
Anyway, my question is simple: What is the expression for log_lik
in this case?
By the way, if you really want to know, the WAIC score for m10.6
shown in the book is presumably incorrect, it doesn’t matter if you use map()
o map2stan()
.