You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Write up Andrew's WAIC example for the manual. See 12/28/13 e-mail to stan-dev and stan-users for the examples.
waic_example.R:
# R script for Waic example. Also needed in this directory: data file hibbs.dat and Stan file lm_waic.stan
# Little function to calculate posterior variances from simulation
colVars <- function (a){
diff <- a - matrix (colMeans(a), nrow(a), ncol(a), byrow=TRUE)
vars <- colMeans (diff^2)*nrow(a)/(nrow(a)-1)
return (vars)
}
# The calculation of Waic! Returns lppd, p_waic_1, p_waic_2, and waic, which we define
# as 2*(lppd - p_waic_2), as recommmended in BDA
waic <- function (stanfit){
log_lik <- extract (stanfit, "log_lik")$log_lik
lppd <- sum (log (colMeans(exp(log_lik))))
p_waic_1 <- 2*sum (log(colMeans(exp(log_lik))) - colMeans(log_lik))
p_waic_2 <- sum (colVars(log_lik))
waic_2 <- -2*lppd + 2*p_waic_2
return (list (waic=waic_2, p_waic=p_waic_2, lppd=lppd, p_waic_1=p_waic_1))
}
# Read in and prepare the data
hibbs <- read.table ("hibbs.dat", header=TRUE)
year <- hibbs[,1]
growth <- hibbs[,2]
vote <- hibbs[,3]
inc <- hibbs[,4]
other <- hibbs[,5]
y <- vote
N <- length (y)
X <- cbind (rep(1,N), growth)
K <- ncol (X)
# Fit the model in Stan!
library ("rstan")
lm_waic <- stan (file="lm_waic.stan", data=c("N","K","X","y"), iter = 2000, chains = 4)
print (lm_waic)
# Calculate and print Waic
print (waic (lm_waic))
lm_waic.stan:
# This is the simple linear regression model from p.165 of Bayesian Data Analysis (3rd edition)
# with Waic as calculated on p.177.
# This code illustrates the calculation of Waic in Stan: we define the log likelihood as a vector
# in the transformed parameters block, then sum it up in the model block. The output for the N # # # components of the vector "loglik" are combined in the waic() function in R.
data {
int N;
int K;
vector[N] y;
matrix[N,K] X;
}
parameters {
vector[K] b;
real<lower=0> sigma;
}
transformed parameters {
vector[N] log_lik;
for (n in 1:N){
log_lik[n] <- normal_log (y[n], X[n]*b, sigma);
}
}
model {
increment_log_prob (-log(sigma)); # log prior for p(sigma) proportional to 1/sigma
increment_log_prob (sum (log_lik)); # log likelihood
}
Write up Andrew's WAIC example for the manual. See 12/28/13 e-mail to stan-dev and stan-users for the examples.
waic_example.R:
lm_waic.stan:
hibbs.dat:
The text was updated successfully, but these errors were encountered: