Skip to content

Instantly share code, notes, and snippets.

@cbrown5
Last active November 25, 2021 01:12
Show Gist options
  • Save cbrown5/ddb0c0ed922267af65e3dc7a113dcf0e to your computer and use it in GitHub Desktop.
Save cbrown5/ddb0c0ed922267af65e3dc7a113dcf0e to your computer and use it in GitHub Desktop.
Choosing gamma priors for variance components as per Fong et al. 2010 "Bayesian inference for generalized linear mixed models" Biostatistics 11
#Visualise priors for a variance component selected using the method of Fong et al. 2010 (see page 400 of that paper)
R <- log(10) #+-range. Use log for GLMs that have log-link functions(e.g. poisson) or no logs for a gaussian model with identity link
d <- 1 #Degrees of freedom for student t
quant <-0.95 #quantile you want random effect estimates to vary between
#Parameters of Gamma
a1 <- d/2
qtemp <- 1-(1-quant)/2
a2 <- (d/2) * (R^2)/(qt(qtemp, df = 1)^2)
#Plot it
qlwr <- (1-quant)/2
x <- seq(0.01, 100, length.out = 10000)
tau <- rgamma(50000, a1, a2)
rgaus <- rnorm(50000, mean = 0, sd = 1/sqrt(tau))
quantile(rgaus, c(qlwr, quant+qlwr)) #should be approx +- log(10) = 2.30
hist(rgaus)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment