Skip to content

Instantly share code, notes, and snippets.

@Gedevan-Aleksizde
Last active January 28, 2018 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Gedevan-Aleksizde/ad42d075729bc80374d9d6dc0ba761e3 to your computer and use it in GitHub Desktop.
Save Gedevan-Aleksizde/ad42d075729bc80374d9d6dc0ba761e3 to your computer and use it in GitHub Desktop.
# --------------------------------
# calculate DIC* from stanfit
#
# * Spiegelhalter et al. (2002)
# --------------------------------
DIC <- function(stanfit, df.input, dev ){
# stanfit: stanfit object
# df.input: input data.frame
# dev: function that calculate the dev. of post.mean;
# dev(<post. mean params vec.>, <input data.frame>)
array.stan <- as.array(stanfit)
for( i in 1:ncol(stanfit) ){
temp <- as.data.frame(array.stan[,i,])
temp$chain <- i
temp$iteration <- 1:nrow(temp)
if ( i==1 ) df.res <- temp
else df.res <- rbind(df.res, temp)
}
rm(temp, array.stan, i)
# mean Deviance
mean.Deviance <- -2*mean(df.res$loglike)
# Deviance of post. mean
Deviance.mean <- dev(colMeans(df.res), df.input)
# effective number of param.s
pD <- mean.Deviance - Deviance.mean
return(c(mean.Dev=mean.Deviance, Dev.mean=Deviance.mean, pD=mean.Deviance-Deviance.mean, DIC=pD+mean.Deviance) )
}
# devi <- function(parms, df){
# return(-2*sum(with(df, dnorm(y, params[1], params[2], log = T) )) )
# }
# DIC(stan.result, df.mort, devi)
@al00014
Copy link

al00014 commented Jan 28, 2018

what should we input for the df.input argument? In stan, the original data is a list not a data.frame object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment