Skip to content

Instantly share code, notes, and snippets.

@mja
Last active August 29, 2015 14:19
Show Gist options
  • Save mja/249c820f5c97cdacafad to your computer and use it in GitHub Desktop.
Save mja/249c820f5c97cdacafad to your computer and use it in GitHub Desktop.
Zero-inflated gaussian model in MCMCglmm
dat <- data.frame(obs=c(rep(0, times=100), rnorm(100, 10, 1)))
# observed data looks something like
hist(dat$obs)
# break into the zero-part and the normal part of the distribution
# observed zeros get zero for the first part, otherwise 1
# observed zeros get NA for the second part, otherwise observed value
dat <- transform(dat, zero_part=ifelse(obs == 0, yes=0, no=1),
norm_part=ifelse(obs != 0, yes=obs, no=NA))
zero_part <- rep(c(0, 1), each=100)
norm_part <- rnorm(100, 10, 1)
library(MCMCglmm)
# 2 latent traits for gaussian + categorical
prior=list(R=list(V=diag(2), nu=1, fix=2))
m <- MCMCglmm(cbind(norm_part, zero_part) ~ trait - 1,
rcov=~idh(trait):units,
data=dat,
family=c('gaussian', 'categorical'),
prior=prior)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment